From 0f231a78cb69576e39cd50bed113de4fbbd69787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Martign=C3=A8ne?= Date: Fri, 26 Apr 2024 18:43:54 +0200 Subject: [PATCH] Support mismatched buffer sizes with truncation or zero-filling --- src/koffi/src/call.cc | 20 ++++++-------------- src/koffi/src/call.hh | 2 +- src/koffi/src/util.cc | 4 +--- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/koffi/src/call.cc b/src/koffi/src/call.cc index aef230c3a5..bdbe864fc8 100644 --- a/src/koffi/src/call.cc +++ b/src/koffi/src/call.cc @@ -593,9 +593,7 @@ bool CallData::PushObject(Napi::Object obj, const TypeInfo *type, uint8_t *origi return false; } else if (IsRawBuffer(value)) { Span buffer = GetRawBuffer(value); - - if (!PushBuffer(buffer, member.type->size, member.type, dest)) - return false; + PushBuffer(buffer, member.type->size, member.type, dest); } else if (value.IsString()) { if (!PushStringArray(value, member.type, dest)) return false; @@ -830,9 +828,7 @@ bool CallData::PushNormalArray(Napi::Array array, Size len, const TypeInfo *type return false; } else if (IsRawBuffer(value)) { Span buffer = GetRawBuffer(value); - - if (!PushBuffer(buffer, ref->size, ref, dest)) - return false; + PushBuffer(buffer, ref->size, ref, dest); } else if (value.IsString()) { if (!PushStringArray(value, ref, dest)) return false; @@ -896,15 +892,13 @@ bool CallData::PushNormalArray(Napi::Array array, Size len, const TypeInfo *type return true; } -bool CallData::PushBuffer(Span buffer, Size size, const TypeInfo *type, uint8_t *origin) +void CallData::PushBuffer(Span buffer, Size size, const TypeInfo *type, uint8_t *origin) { - if (buffer.len != size) [[unlikely]] { - ThrowError(env, "Expected array or buffer of size %1, got %2", size, buffer.len); - return false; - } + buffer.len = std::min(buffer.len, size); - // Go fast yeaaaah :) + // Go fast brrrrrrr :) memcpy_safe(origin, buffer.ptr, (size_t)buffer.len); + memset_safe(origin + buffer.len, 0, (size_t)(size - buffer.len)); #define SWAP(CType) \ do { \ @@ -929,8 +923,6 @@ bool CallData::PushBuffer(Span buffer, Size size, const TypeInfo } #undef SWAP - - return true; } bool CallData::PushStringArray(Napi::Value obj, const TypeInfo *type, uint8_t *origin) diff --git a/src/koffi/src/call.hh b/src/koffi/src/call.hh index 91b596aaa9..51e82cbc54 100644 --- a/src/koffi/src/call.hh +++ b/src/koffi/src/call.hh @@ -119,7 +119,7 @@ public: Size PushString16Value(Napi::Value value, const char16_t **out_str16); bool PushObject(Napi::Object obj, const TypeInfo *type, uint8_t *origin); bool PushNormalArray(Napi::Array array, Size len, const TypeInfo *type, uint8_t *origin); - bool PushBuffer(Span buffer, Size size, const TypeInfo *type, uint8_t *origin); + void PushBuffer(Span buffer, Size size, const TypeInfo *type, uint8_t *origin); bool PushStringArray(Napi::Value value, const TypeInfo *type, uint8_t *origin); bool PushPointer(Napi::Value value, const TypeInfo *type, int directions, void **out_ptr); Size PushIndirectString(Napi::Array array, const TypeInfo *ref, uint8_t **out_ptr); diff --git a/src/koffi/src/util.cc b/src/koffi/src/util.cc index a8cb6694cb..da49c8d3b0 100644 --- a/src/koffi/src/util.cc +++ b/src/koffi/src/util.cc @@ -1446,9 +1446,7 @@ bool Encode(Napi::Env env, uint8_t *origin, Napi::Value value, const TypeInfo *t return false; } else if (IsRawBuffer(value)) { Span buffer = GetRawBuffer(value); - - if (!call.PushBuffer(buffer, type->size, type, origin)) - return false; + call.PushBuffer(buffer, type->size, type, origin); } else if (value.IsString()) { if (!call.PushStringArray(value, type, origin)) return false;