Skip to content

Commit 4524014

Browse files
committedOct 28, 2024
fixes from node-ffi-napi#82
1 parent b8cb3da commit 4524014

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed
 

‎src/binding.cc

+9-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class InstanceData final : public RefNapi::Instance {
140140
if (it != pointer_to_orig_buffer.end())
141141
ab = it->second.ab.Value();
142142

143-
if (ab.IsEmpty()) {
143+
if (ab.IsEmpty() || (ab.ByteLength() < length)) {
144144
// this ALWAYS creates an arraybuffer of 1Gbyte?
145145
//length = std::max<size_t>(length, kMaxLength);
146146
length = std::min<size_t>(length, kMaxLength);
@@ -168,8 +168,15 @@ class InstanceData final : public RefNapi::Instance {
168168
*/
169169

170170
Value WrapPointer(Env env, char* ptr, size_t length) {
171-
if (ptr == nullptr)
171+
if (ptr == nullptr) {
172172
length = 0;
173+
} else if (length == 0) {
174+
// If length is 0 N-API doesn't guarantee it will save/restore ptr normally.
175+
// For example, see https://nodejs.org/api/n-api.html#napi_get_typedarray_info
176+
// "[out] data: ... If the length of the array is 0, this may be NULL or any
177+
// other pointer value."
178+
length = 1;
179+
}
173180

174181
InstanceData* data;
175182
if (ptr != nullptr && (data = InstanceData::Get(env)) != nullptr) {

0 commit comments

Comments
 (0)
Please sign in to comment.