ffi: throw on missing memory helper arguments - #65500
Open
soulee-dev wants to merge 1 commit into
Open
Conversation
ffi.getInt8() through ffi.getFloat64(), ffi.setInt8() through ffi.setFloat64(), ffi.toBuffer() and ffi.toArrayBuffer() return undefined instead of throwing when a required argument is omitted, so a call that read or wrote nothing cannot be told apart from one that read a zero byte. All 22 helpers behave this way. GetValidatedPointerAddress() and GetValidatedSize() already reject the same argument when it is passed explicitly as undefined. The args.Length() test in front of them short-circuits the call and returns Nothing without scheduling an exception. These six are the only tests in src/ where args.Length() can skip a call that throws; the only other Length() tests that guard a call at all guard Buffer::HasInstance(), which cannot throw. The remaining tests in this file guard an inline predicate and throw in the branch, which is why setUint8(ptr) reports "Expected an offset argument" while setUint8() reports nothing at all. Drop those tests. FunctionCallbackInfo::operator[] returns Undefined for an out-of-range index, which is exactly the value these validators reject, so each missing argument now produces the error its explicit undefined counterpart produces. The documentation already describes this behavior: the signatures are ffi.getInt8(pointer[, offset]), ffi.setInt8(pointer, offset, value) and ffi.toBuffer(pointer, length[, copy]), and the getters are documented to return a number or a bigint. ExportBytes() carried the same two tests. They are unreachable through the public API because exportBytes is not exported and its three callers all validate len in JavaScript first, but they are the same shape. Signed-off-by: Soul Lee <alus20x@gmail.com>
Collaborator
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65500 +/- ##
========================================
Coverage 90.12% 90.12%
========================================
Files 751 751
Lines 252425 252589 +164
Branches 47471 47535 +64
========================================
+ Hits 227504 227655 +151
- Misses 16216 16239 +23
+ Partials 8705 8695 -10
🚀 New features to boost your workflow:
|
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.
ffi.getInt8()throughffi.getFloat64(),ffi.setInt8()throughffi.setFloat64(),ffi.toBuffer()andffi.toArrayBuffer()returnundefinedinstead of throwing when a required argument is omitted, so a call that read or wrote nothing cannot be told apart from one that read a zero byte.GetValidatedPointerAddress()andGetValidatedSize()already reject the same argument when it is passed explicitly asundefined. Theargs.Length()test in front of them short-circuits the call and returnsNothingwithout scheduling an exception. These six are the only tests insrc/whereargs.Length()can skip a call that throws; the only otherLength()tests that guard a call at all guardBuffer::HasInstance(), which cannot throw.Drop those tests.
FunctionCallbackInfo::operator[]returnsUndefinedfor an out-of-range index, which is exactly the value these validators reject, so each missing argument now produces the error its explicitundefinedcounterpart produces. The tests that guard an explicit throw, such as the ones inToString()andGetRawPointer(), are left alone.ExportBytes()carried the same two tests. They are unreachable through the public API becauseexportBytesis not exported and its three callers all validatelenin JavaScript first, but they are the same shape.No documentation change is needed:
doc/api/ffi.mdalready lists these arguments as required.Fixes: #65499
Refs: #62072
Refs: #62858