Repository navigation
Clamp JsFunction::length() to the range of size_t - #8625
Merged
Merged
Conversation
JsFunction::length() casts the function's `length` property straight from a double to size_t. A script can redefine `length` with Object.defineProperty() to any number, and converting a negative, NaN, infinite or too-large double to size_t is undefined behaviour. On x86-64 a length of -1 gives 18446744073709551615, and NaN, Infinity, -Infinity and 1e300 all give 9223372036854775808. In this patch NaN and lengths at or below 0 give 0, lengths of 2^64 or more give SIZE_MAX, and other values are truncated as before, the way Function.prototype.bind() clamps the length it reads. A new jsvalue-test case covers each of these and fails without the fix.
ryanking13
approved these changes
Nov 19, 2026
There was a problem hiding this comment.
✅ Approved — The clamping makes the double-to-size_t conversion defined for every possible length value, and the new test covers each edge case; no issues found.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
📋 Code Quality: ✅ LGTM
The clamping logic in src/workerd/jsg/jsvalue.c++ is correct.
- NaN, -Infinity, zero and negative values return 0.
- Values of 2^64 or more, including +Infinity, return
kj::maxValue. - All remaining values convert with a well-defined
static_cast<size_t>.
The new test in jsvalue-test.c++ covers each of these cases.
📝 Documentation: ✅ LGTM
The updated header comment in jsvalue.h and the new inline comment accurately describe the current behaviour.
Reviewed commit: 38581468 — same changes as dc5bb01b, so this is that commit's review (no new review was run; line numbers are as of dc5bb01b)
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.
JsFunction::length()casts the function'slengthproperty straight from a double tosize_t. A script can redefinelengthwithObject.defineProperty()to any number, and converting a negative, NaN, infinite or too-large double tosize_tis undefined behaviour. On x86-64 a length of-1gives18446744073709551615, andNaN,Infinity,-Infinityand1e300all give9223372036854775808.In this patch NaN and lengths at or below 0 give 0, lengths of 2^64 or more give
SIZE_MAX, and other values are truncated as before, the wayFunction.prototype.bind()clamps the length it reads. A new jsvalue-test case covers each of these and fails without the fix.