ffi: avoid memcpy() with a null pointer · nodejs/node@ee84a36 · GitHub
Skip to content

Commit ee84a36

Browse files
christianaurichzmaduh95
authored andcommitted
ffi: avoid memcpy() with a null pointer
Zero-length FFI memory operations accept a null pointer, but ToArrayBuffer() and ExportBytes() call memcpy() unconditionally. Passing a null pointer to memcpy() is undefined behavior even when the size is zero. Skip the copy when the length is zero, matching what Buffer::Copy() already does for the same case. An isolated reproduction compiled with -fsanitize=undefined reports "null pointer passed as argument 1, which is declared to never be null". The added test covers the reachable zero-length paths, but it passes without the fix: libc does not fault on memcpy(NULL, NULL, 0), and --enable-ubsan does not set -fno-sanitize-recover, so UBSan reports the call without failing the process. Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com> PR-URL: #66200 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 1f4a8fb commit ee84a36

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎src/ffi/data.cc‎

Lines changed: 4 additions & 2 deletions

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

Lines changed: 8 additions & 0 deletions

0 commit comments

Comments
 (0)