ffi: avoid memcpy() with a null pointer - #66200
Open
christianaurichzm wants to merge 1 commit into
Open
christianaurichzm wants to merge 1 commit into
christianaurichzm wants to merge 1 commit into
Conversation
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>
Collaborator
|
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #66200 +/- ##
==========================================
- Coverage 90.29% 90.27% -0.02%
==========================================
Files 790 790
Lines 272529 272531 +2
Branches 52037 52034 -3
==========================================
- Hits 246074 246037 -37
- Misses 16908 16954 +46
+ Partials 9547 9540 -7
🚀 New features to boost your workflow:
|
Collaborator
trivikr
approved these changes
Sep 22, 2026
RafaelGSS
approved these changes
Sep 22, 2026
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ToArrayBuffer()andExportBytes()accept zero-length operations with a nullpointer, but still call
memcpy(). Passing a null pointer tomemcpy()isundefined behavior even when the size is zero.
Skip the copy when the length is zero, matching
Buffer::Copy().Tests cover the zero-length FFI memory entry points. Full configured test suite,
C++ lint/format, and ESLint pass.