File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed
Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff 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
170170Value 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 ) {
You can’t perform that action at this time.
0 commit comments