Skip to content

Commit 5ca238a

Browse files
committed
buffer: revert GetBackingStore optimization in API
In an earlier PR, I replaced a lot of instances of `GetBackingStore()->Data()` with `Data()`. Apparently these two are not equivalent in the case of zero-length buffers: the former returns a "valid" address, while the latter returns `NULL`. At least one library in the ecosystem (see the referenced issue) abuses zero-length buffers to wrap arbitrary pointers, which is broken by this difference. It is unfortunate that every library needs to take a performance hit because of this edge-case, and somebody should figure out if this is actually a reasonable contract to uphold long-term. I have not traced down exactly why this divergence occurs, but I have verified that reverting this line fixes the referenced issue. Refs: nodejs#44080 Fixes: nodejs#44554
1 parent e06384c commit 5ca238a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/node_buffer.cc

+14-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,20 @@ bool HasInstance(Local<Object> obj) {
244244
char* Data(Local<Value> val) {
245245
CHECK(val->IsArrayBufferView());
246246
Local<ArrayBufferView> ui = val.As<ArrayBufferView>();
247-
return static_cast<char*>(ui->Buffer()->Data()) + ui->ByteOffset();
247+
// GetBackingStore() is slow, and this would be faster if we just did
248+
// ui->Buffer()->Data(). However these two are not equivalent in the case of
249+
// zero-length buffers: the former returns a "valid" address, while the
250+
// latter returns `NULL`. At least one library in the ecosystem (see the
251+
// referenced issue) abuses zero-length buffers to wrap arbitrary pointers,
252+
// which is broken by this difference. It is unfortunate that every library
253+
// needs to take a performance hit because of this edge-case, and somebody
254+
// should figure out if this is actually a reasonable contract to uphold
255+
// long-term.
256+
//
257+
// See: https://github.com/nodejs/node/issues/44554
258+
return static_cast<char*>(ui->Buffer()->GetBackingStore()->Data()) +
259+
260+
ui->ByteOffset();
248261
}
249262

250263

0 commit comments

Comments
 (0)