Skip to content

Commit

Permalink
Support mismatched buffer sizes with truncation or zero-filling
Browse files Browse the repository at this point in the history
  • Loading branch information
Koromix committed Apr 26, 2024
1 parent 7e81426 commit 0f231a7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
20 changes: 6 additions & 14 deletions src/koffi/src/call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,7 @@ bool CallData::PushObject(Napi::Object obj, const TypeInfo *type, uint8_t *origi
return false;
} else if (IsRawBuffer(value)) {
Span<const uint8_t> 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;
Expand Down Expand Up @@ -830,9 +828,7 @@ bool CallData::PushNormalArray(Napi::Array array, Size len, const TypeInfo *type
return false;
} else if (IsRawBuffer(value)) {
Span<const uint8_t> 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;
Expand Down Expand Up @@ -896,15 +892,13 @@ bool CallData::PushNormalArray(Napi::Array array, Size len, const TypeInfo *type
return true;
}

bool CallData::PushBuffer(Span<const uint8_t> buffer, Size size, const TypeInfo *type, uint8_t *origin)
void CallData::PushBuffer(Span<const uint8_t> buffer, Size size, const TypeInfo *type, uint8_t *origin)
{
if (buffer.len != size) [[unlikely]] {
ThrowError<Napi::Error>(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 { \
Expand All @@ -929,8 +923,6 @@ bool CallData::PushBuffer(Span<const uint8_t> buffer, Size size, const TypeInfo
}

#undef SWAP

return true;
}

bool CallData::PushStringArray(Napi::Value obj, const TypeInfo *type, uint8_t *origin)
Expand Down
2 changes: 1 addition & 1 deletion src/koffi/src/call.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<const uint8_t> buffer, Size size, const TypeInfo *type, uint8_t *origin);
void PushBuffer(Span<const uint8_t> 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);
Expand Down
4 changes: 1 addition & 3 deletions src/koffi/src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<const uint8_t> 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;
Expand Down

0 comments on commit 0f231a7

Please sign in to comment.