]> git.kaiwu.me - njs.git/commit
Buffer: fix use-after-free on detach during argument coercion
authorbasavaraj-sm05 <basavaraj@digiscrypt.com>
Wed, 12 Aug 2026 15:07:30 +0000 (20:37 +0530)
committerDmitry Volyntsev <xeioexception@gmail.com>
Sat, 14 Nov 2026 00:04:03 +0000 (16:04 -0800)
commit640d5ab8154e45bf5db65d16c93a3ba202c651b2
tree9a425759163494bc1a30f6d31d977f7a1a26166a
parent548e5179b582e4701273c68d9be00e4627b0fd6b
Buffer: fix use-after-free on detach during argument coercion

Several Buffer.prototype methods fetched the data pointer of the
underlying ArrayBuffer before converting their offset, length or value
arguments.  The conversion may call user code (valueOf) which can
detach the buffer, for example with ArrayBuffer.prototype.transfer().
The bounds check and the memory access then used the stale pointer.

In QuickJS this affected readInt/readFloat, writeInt/writeFloat,
write(), toString(), indexOf(), compare() and copy().  In njs it
affected the range helper behind compare() and copy().

Now all arguments are converted first, and the buffer is fetched and
checked right before it is used, so a detach during the conversion is
reported as an ordinary "detached buffer" error.  The order in which
the arguments are converted is preserved.

In njs, copy() now makes the target writable before computing the
target range.  A target sharing external data is copied on write, so
pointers computed earlier pointed to the old data and the copy was
lost.

Similarly, toJSON() in QuickJS stored the bytes with
JS_SetPropertyUint32(), which may call a setter inherited from
Array.prototype and detach the buffer during the loop.  The elements
are now defined directly on the new array.
src/njs_buffer.c
src/qjs_buffer.c
src/test/njs_unit_test.c
test/buffer.t.js