Skip to content

Clamp JsFunction::length() to the range of size_t - #8625

Merged
littledivy merged 2 commits into
mainfrom
littledivy/2026-11-18-jsfunction-length-ub
Nov 19, 2026
Merged

littledivy merged 2 commits into
mainfrom
littledivy/2026-11-18-jsfunction-length-ub

Conversation

@littledivy

Copy link
Copy Markdown
Member

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.

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.
@littledivy
littledivy requested review from a team as code owners November 18, 2026 17:36

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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)

@littledivy
littledivy merged commit e3f67c7 into main Nov 19, 2026
23 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-18-jsfunction-length-ub branch November 19, 2026 05:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants