ffi: reject unsafe integers as length or offset · nodejs/node@0558424 · GitHub
Skip to content

Commit 0558424

Browse files
ffi: reject unsafe integers as length or offset
GetValidatedSize() checks the value against static_cast<double>(SIZE_MAX), which rounds up to 2^64 on 64-bit platforms. A length or offset of 2 ** 64 gets through, and the cast to size_t after it is undefined behavior. With GCC on x64 it gives 0, so ffi.setUint8(ptr, 2 ** 64, 42) writes to ptr instead of throwing. Anything above Number.MAX_SAFE_INTEGER may already have been rounded by the time it gets here, so reject those values too. The export*() helpers already cap their length there, and so does setInt64() for number values. SIZE_MAX is still the limit on 32-bit platforms. When buffer.constants.MAX_LENGTH is Number.MAX_SAFE_INTEGER, as on 64-bit builds without the V8 sandbox, toBuffer() and toArrayBuffer() now throw ERR_OUT_OF_RANGE for MAX_LENGTH + 1 instead of ERR_BUFFER_TOO_LARGE. Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
1 parent 3cd2d6e commit 0558424

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎src/ffi/data.cc‎

Lines changed: 4 additions & 1 deletion

‎test/ffi/test-ffi-memory.js‎

Lines changed: 22 additions & 2 deletions

0 commit comments

Comments
 (0)