GeezerVison is showing github.com — ffi: validate the value passed to the float setters by soulee-dev · Pull Request #65342 · nodejs/node
Conversation
soulee-dev
commented
Aug 17, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Problem
Every other setter in SetValue<T>() requires IsNumber() before it writes, so setInt8(ptr, 0, '5') throws. The floating-point branch calls ToNumber() instead, so setFloat64(ptr, 0, '1.5') writes 1.5, {} writes NaN and null writes 0. The same double is type-checked on the argument path, where ToFFIArgument() throws Argument %s must be a double, and the documentation already says the setters "validate the supplied JavaScript value against the target native type before writing it into memory".
ToNumber() also discards a pending exception. When the value has a valueOf() that throws, the branch calls THROW_ERR_INVALID_ARG_VALUE on top of the exception V8 has already scheduled, so the caller sees Value must be a number instead of their own error. DataView.prototype.setFloat64() and Buffer.prototype.writeDoubleLE() both propagate it, and src/README.md asks for an early return instead.
Fix
Check IsNumber() instead. It runs before any conversion, so valueOf() is never invoked and there is no pending exception left to discard. The messages follow the integer setters and ToFFIArgument() rather than the old generic Value must be a number.
Compatibility
NaN and Infinity still pass, since both are number values; only the type check is new. This makes input that is accepted today throw, but node:ffi is experimental and the recent fixes in this area went the same way without a semver-major label.
Tests
Six assertions are added to the setter block in test/ffi/test-ffi-memory.js, which covered only the integer setters. The last one passes { valueOf: common.mustNotCall() }, so the test fails if the setter ever converts the value again.
Fixes: #65341
Refs: #62858
Refs: #64614
Refs: #64691
Refs: #65032
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
setInt8() through setUint64() require IsNumber() before any range check
and reject anything else with ERR_INVALID_ARG_VALUE, but setFloat32()
and setFloat64() call ToNumber() and write whatever it returns, so a
string, a boolean or a plain object is converted instead of rejected
and a typo such as '1,5' stores NaN in native memory with no error at
the call site. The same double is type-checked when it is passed as a
call argument: ToFFIArgument() requires IsNumber() and otherwise throws
"Argument %s must be a double".
The coercion also discards a pending exception. When ToNumber() fails
because the value has a valueOf() that throws, the branch throws
ERR_INVALID_ARG_VALUE on top of the exception V8 has already scheduled,
so the original error never reaches the caller, whereas
DataView.prototype.setFloat64() and Buffer.prototype.writeDoubleLE()
both propagate it.
Check IsNumber() instead, matching the wording of the integer setters
and of ToFFIArgument(). The check runs before any conversion, so
valueOf() is never invoked and there is no pending exception left to
discard. This was the only ToNumber(context) call in src/.
Signed-off-by: Soul Lee <[email protected]>
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
codecov
Bot
commented
Aug 17, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.12%. Comparing base (ad7a5b8) to head (f26b00e).
⚠️ Report is 43 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #65342 +/- ##
==========================================
+ Coverage 90.11% 90.12% +0.01%
==========================================
Files 752 752
Lines 251569 251567 -2
Branches 47268 47266 -2
==========================================
+ Hits 226701 226725 +24
+ Misses 16233 16189 -44
- Partials 8635 8653 +18
... and 38 files with indirect coverage changes
🚀 New features to boost your workflow:
- ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
trivikr
added
ffi
Issues and PRs related to experimental Foreign Function Interface support.
request-ci
Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review.
labels
Aug 19, 2026
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
aduh95
pushed a commit
that referenced
this pull request
Aug 25, 2026
setInt8() through setUint64() require IsNumber() before any range check
and reject anything else with ERR_INVALID_ARG_VALUE, but setFloat32()
and setFloat64() call ToNumber() and write whatever it returns, so a
string, a boolean or a plain object is converted instead of rejected
and a typo such as '1,5' stores NaN in native memory with no error at
the call site. The same double is type-checked when it is passed as a
call argument: ToFFIArgument() requires IsNumber() and otherwise throws
"Argument %s must be a double".
The coercion also discards a pending exception. When ToNumber() fails
because the value has a valueOf() that throws, the branch throws
ERR_INVALID_ARG_VALUE on top of the exception V8 has already scheduled,
so the original error never reaches the caller, whereas
DataView.prototype.setFloat64() and Buffer.prototype.writeDoubleLE()
both propagate it.
Check IsNumber() instead, matching the wording of the integer setters
and of ToFFIArgument(). The check runs before any conversion, so
valueOf() is never invoked and there is no pending exception left to
discard. This was the only ToNumber(context) call in src/.
Signed-off-by: Soul Lee <[email protected]>
PR-URL: #65342
Fixes: #65341
Refs: #62858
Refs: #64614
Refs: #64691
Refs: #65032
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
aduh95
pushed a commit
that referenced
this pull request
Aug 25, 2026
setInt8() through setUint64() require IsNumber() before any range check
and reject anything else with ERR_INVALID_ARG_VALUE, but setFloat32()
and setFloat64() call ToNumber() and write whatever it returns, so a
string, a boolean or a plain object is converted instead of rejected
and a typo such as '1,5' stores NaN in native memory with no error at
the call site. The same double is type-checked when it is passed as a
call argument: ToFFIArgument() requires IsNumber() and otherwise throws
"Argument %s must be a double".
The coercion also discards a pending exception. When ToNumber() fails
because the value has a valueOf() that throws, the branch throws
ERR_INVALID_ARG_VALUE on top of the exception V8 has already scheduled,
so the original error never reaches the caller, whereas
DataView.prototype.setFloat64() and Buffer.prototype.writeDoubleLE()
both propagate it.
Check IsNumber() instead, matching the wording of the integer setters
and of ToFFIArgument(). The check runs before any conversion, so
valueOf() is never invoked and there is no pending exception left to
discard. This was the only ToNumber(context) call in src/.
Signed-off-by: Soul Lee <[email protected]>
PR-URL: #65342
Fixes: #65341
Refs: #62858
Refs: #64614
Refs: #64691
Refs: #65032
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
Reviewers
trivikr
trivikr approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Labels
c++
Issues and PRs that require attention from people who are familiar with C++.
ffi
Issues and PRs related to experimental Foreign Function Interface support.
needs-ci
PRs that need a full CI run.
Milestone
No milestone
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.
|