Skip to content

Commit 427fce7

Browse files
committed
src: fix check for accepting Buffers into Node’s allocator
This condition was incorrect. We currently take the fallback path in default Node builds, which always works, but may come with some overhead, whereas the intention was that we use the fast path in this condition. This is causing issues for embedders, because we would erroneously try to take the fast path when they don’t provide a Node.js-style `ArrayBufferAlloactor`, and crash as a consequence of that. This also requires us to relax the check in the debugging ArrayBuffer allocator a bit, because since d117e41, 0-sized ArrayBuffers may actually point to allocations of size 1. Previously, that wasn’t caught because the fallback path circumvented our ArrayBufferAllocator. Refs: 84e02b1#r33116006 PR-URL: #27174 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gus Caplan <[email protected]>
1 parent d4e7431 commit 427fce7

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/api/environment.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ void DebuggingArrayBufferAllocator::UnregisterPointerInternal(void* data,
139139
if (data == nullptr) return;
140140
auto it = allocations_.find(data);
141141
CHECK_NE(it, allocations_.end());
142-
CHECK_EQ(it->second, size);
142+
if (size > 0) {
143+
// We allow allocations with size 1 for 0-length buffers to avoid having
144+
// to deal with nullptr values.
145+
CHECK_EQ(it->second, size);
146+
}
143147
allocations_.erase(it);
144148
}
145149

src/node_buffer.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ MaybeLocal<Object> New(Environment* env,
420420
}
421421

422422
if (uses_malloc) {
423-
if (env->isolate_data()->uses_node_allocator()) {
423+
if (!env->isolate_data()->uses_node_allocator()) {
424424
// We don't know for sure that the allocator is malloc()-based, so we need
425425
// to fall back to the FreeCallback variant.
426426
auto free_callback = [](char* data, void* hint) { free(data); };

0 commit comments

Comments
 (0)