Skip to content

node:buffer: accept write* offsets ending at the last byte of a 2**32 buffer - #35867

Closed
robobun wants to merge 1 commit into
mainfrom
farm/21a3cc76/buffer-write-4gib-last-byte
Closed

robobun wants to merge 1 commit into
mainfrom
farm/21a3cc76/buffer-write-4gib-last-byte

Conversation

@robobun

@robobun robobun commented Jul 26, 2026 •

Copy link
Copy Markdown
Collaborator

Repro

const b = Buffer.alloc(2 ** 32);
b.writeUInt8(1, 4294967295);
// RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range.
// It must be >= 0 and <= 4294967295. Received 4294967295

The message contradicts itself. readUInt8(4294967295) on the same buffer already works, as does DataView.setUint8. Affects every write* whose last byte lands at index 2**32 - 1: write[U]Int8/16/32{LE,BE}, writeFloat/Double{LE,BE}, write[U]Int{LE,BE}.

Cause

The slow-path bounds checks in internal/buffer.ts (checkBounds, writeU_Int8) decide in/out of range by probing buf[offset] === undefined. In JSC a TypedArray indexed get at 2**32 - 1 currently returns undefined even when the view has 2**32 elements (V8 returns the element), so the probe false-positives at exactly the last byte of a kMaxLength buffer and boundsError throws on a valid offset. The read side's native $checkBufferRead already compares offset <= length - byteLength numerically, which is why readUInt8 works.

Fix

Switch checkBounds and writeU_Int8 to the same numeric comparison the read side uses, with an explicit Math.floor(offset) !== offset guard so fractional offsets still report "an integer" like Node. The inline fast-path probes in JSBufferPrototype.ts stay as-is: at the boundary index they fall through to the now-correct slow path, and DataView performs the store.

The engine-level indexed-get gap is being tracked separately; this change makes the Buffer write family correct regardless of that behaviour.

Verification

New test in test/js/node/buffer.test.js allocates a 2**32 buffer in a subprocess, writes to the final byte(s) with each affected write*, reads the values back, and checks that one-past-end / fractional / negative offsets still throw ERR_OUT_OF_RANGE. test-buffer-write*.js / test-buffer-read*.js vendored Node tests pass; the full buffer.test.js suite is green.


[review] gate passed · iteration 0 · 2 files touched

fails on main (without fix)
ASAN without fix: 1 failed, 1 skipped
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/js/node/buffer.test.js
bun test v1.4.0 (c90c7ed50)

test/js/node/buffer.test.js:
(pass) with native Buffer.write > #9120 fill [91.62ms]
(pass) with native Buffer.write > #9120 alloc [70.40ms]
(pass) with native Buffer.write > isAscii [69.91ms]
(pass) with native Buffer.write > isUtf8 [91.75ms]
(pass) with native Buffer.write > Buffer global is settable [68.01ms]
(pass) with native Buffer.write > length overflow [75.46ms]
(pass) with native Buffer.write > truncate input values [187.97ms]
(pass) with native Buffer.write > Buffer.allocUnsafe() [70.82ms]
(pass) with native Buffer.write > Buffer.from() [73.82ms]
(pass) with native Buffer.write > offset properties [65.95ms]
(pass) with native Buffer.write > creating a Buffer from a Uint32Array [92.65ms]
(pass) with native Buffer.write > creating a Buffer from a Uint32Array (old constructor) [75.05ms]
(pass) with native Buffer.write > invalid encoding [76.60ms]
(pass) with native Buffer.write > create 0-length buffers [69.19ms]
(pass) with native Buffer.write > write() beyond end of buff
... (truncated)

release without fix: 1 FAILED
bun test v1.4.0-canary.1 (c90c7ed50)

test/js/node/buffer.test.js:
(pass) with native Buffer.write > #9120 fill [6.12ms]
(pass) with native Buffer.write > #9120 alloc [3.60ms]
(pass) with native Buffer.write > isAscii [3.59ms]
(pass) with native Buffer.write > isUtf8 [4.14ms]
(pass) with native Buffer.write > Buffer global is settable [3.89ms]
(pass) with native Buffer.write > length overflow [5.16ms]
(pass) with native Buffer.write > truncate input values [4.08ms]
(pass) with native Buffer.write > Buffer.allocUnsafe() [3.89ms]
(pass) with native Buffer.write > Buffer.from() [3.52ms]
(pass) with native Buffer.write > offset properties [3.74ms]
(pass) with native Buffer.write > creating a Buffer from a Uint32Array [4.87ms]
(pass) with native Buffer.write > creating a Buffer from a Uint32Array (old constructor) [3.22ms]
(pass) with native Buffer.write > invalid encoding [3.58ms]
(pass) with native Buffer.write > create 0-length buffers [5.21ms]
(pass) with native Buffer.write > write() beyond end of buffer [3.94ms]
(pass) with native Buffer.write > write BigInt beyond 64-bit range [3.76ms]
(pass) with native Buffer.write > write BigInt64 with insufficient buffer space
... (truncated)
passes on PR (with fix)
ASAN with fix: 1 skipped
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/js/node/buffer.test.js
bun test v1.4.0 (c90c7ed50)

test/js/node/buffer.test.js:
(pass) with native Buffer.write > #9120 fill [107.17ms]
(pass) with native Buffer.write > #9120 alloc [80.34ms]
(pass) with native Buffer.write > isAscii [69.60ms]
(pass) with native Buffer.write > isUtf8 [76.20ms]
(pass) with native Buffer.write > Buffer global is settable [64.41ms]
(pass) with native Buffer.write > length overflow [81.32ms]
(pass) with native Buffer.write > truncate input values [198.40ms]
(pass) with native Buffer.write > Buffer.allocUnsafe() [100.38ms]
(pass) with native Buffer.write > Buffer.from() [72.41ms]
(pass) with native Buffer.write > offset properties [71.68ms]
(pass) with native Buffer.write > creating a Buffer from a Uint32Array [69.19ms]
(pass) with native Buffer.write > creating a Buffer from a Uint32Array (old constructor) [68.48ms]
(pass) with native Buffer.write > invalid encoding [111.14ms]
(pass) with native Buffer.write > create 0-length buffers [63.90ms]
(pass) with native Buffer.write > write() beyond end of b
... (truncated)

release with fix: all passed
$ bun scripts/build.ts --profile=release
[configured] bun-profile → bun (stripped) in 841ms (unchanged)
ninja: Entering directory `/workspace/bun/build/release'
[1/21] gen JS modules (bundle-modules)
Preprocess modules (9839ms)
Bundle modules (200ms)
Postprocesss modules (332ms)
Bundle Functions (988ms)
Generate Code (36ms)

[11.41s] Bundled "src/js" for production
  2570 kb
  193 internal modules
  13 native modules
  90 internal functions across 19 files
[1/7] cargo bun_bin → libbun_rust.a (--target x86_64-unknown-linux-gnu)

  nightly-2026-07-20-x86_64-unknown-linux-gnu unchanged - rustc 1.99.0-nightly (9f36de775 2026-07-19)

�[1m�[92m    Finished�[0m `release` profile [optimized + debuginfo] target(s) in 0.64s
[2/7] cc obj/codegen/InternalModuleRegistryConstants.S.o
[4/7] cxx obj/unified/UnifiedSource-src_jsc_bindings-1.cpp.o
[4/7] link bun-profile
[5/7] bun-profile --revision
1.4.0-canary.1+c90c7ed50
[7/7] strip bun
[build] done
bun test v1.4.0-canary.1 (c90c7ed50)

test/js/node/buffer.test.js:
(pass) with native Buffer.write > #9120 fill [4.57ms]
(pass) with native Buffer.write > #9120 alloc [2.73ms]
(pass) with native Buffer.write > isAscii [2.17ms]
(pass
... (truncated)
diff hotspot
src/js/internal/buffer.ts   |  10 +++--
 test/js/node/buffer.test.js | 107 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 114 insertions(+), 3 deletions(-)

gate history · 1 passed · 0 rejected · iteration 0

evidence per changed file
file                         reads  edits  tests
src/js/internal/buffer.ts        1      2      0
test/js/node/buffer.test.js      1      1      0

self-review · no surviving concerns

34 concerns were raised and did not survive verification.

… buffer

The write-side bounds checks in internal/buffer (checkBounds, writeU_Int8)
probed buf[offset] === undefined to decide in/out of range. JSC's
TypedArray indexed get returns undefined for index 2**32 - 1 even when
the view's length is 2**32, so every write* whose span touched the final
byte of a kMaxLength buffer was rejected with a self-contradicting
'must be <= 4294967295. Received 4294967295'. The read side already
compared numerically via $checkBufferRead, so readUInt8 at the same
offset worked.

Switch the slow-path checks to the same 0 <= offset <= length - byteLength
comparison the native read check uses (with an explicit floor(offset) !=
offset guard so fractional offsets still report 'an integer'). The
inline fast-path probes in JSBufferPrototype are left as-is: at the
boundary index they fall through to the now-correct slow path, and
DataView performs the store.
@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 2 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: ff273845-2ea5-4826-9646-0ed1b0de4499

📥 Commits

Reviewing files that changed from the base of the PR and between 44f6469 and c90c7ed.

📒 Files selected for processing (2)
  • src/js/internal/buffer.ts
  • test/js/node/buffer.test.js

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Jul 26, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 9:36 PM PT - Jul 25th, 2026

❌ @robobun, your commit c90c7ed has 1 failures in Build #82036 (All Failures):

  • 📦 Binary size — 12 over 0.50 MB
  • targetthis build canary: main #79916
    sizeΔ
    ❌ bun-darwin-aarch6458.13 MB57.58 MB+564.9 KB
    ❌ bun-darwin-x6463.48 MB62.95 MB+544.5 KB
    ❌ bun-linux-aarch6470.98 MB70.42 MB+576.0 KB
    ❌ bun-linux-x6472.47 MB71.95 MB+528.0 KB
    ❌ bun-linux-aarch64-musl64.88 MB64.32 MB+576.0 KB
    ❌ bun-linux-x64-musl66.98 MB66.45 MB+544.0 KB
    ❌ bun-linux-aarch64-android78.47 MB77.97 MB+512.0 KB
    ❌ bun-linux-x64-android80.62 MB80.10 MB+529.2 KB
    ❌ bun-freebsd-x6483.07 MB82.56 MB+528.0 KB
    ❌ bun-freebsd-aarch6484.84 MB84.31 MB+544.0 KB
    ❌ bun-windows-x6480.26 MB79.70 MB+570.5 KB
    ❌ bun-windows-aarch6470.86 MB70.34 MB+533.0 KB

    Add [skip size check] to the commit message if this increase is intentional.


🧪   To try this PR locally:

bunx bun-pr 35867

That installs a local version of the PR into your bun-35867 executable, so you can run:

bun-35867 --bun

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — straightforward switch from an indexed-get probe to a numeric range check in the write* slow path.

What was reviewed:

  • New checkBounds/writeU_Int8 predicate against fractional, negative, NaN, ±Infinity, -0, and detached buffers (buf.length === 0 → negative last) — all still route to boundsError with the same message as before.
  • Confirmed the fast-path this[offset] === undefined probes in JSBufferPrototype.ts fall through to the now-correct slow path at the boundary index, and DataView performs the actual store.
  • Math.floor is not primordial-guarded, but boundsError in the same file already uses it — consistent with existing convention.
  • Test mirrors the neighbouring 4GB subprocess test's skipIf/env pattern and covers the accept/reject matrix.
Extended reasoning...

Overview

Two functions in src/js/internal/buffer.ts — checkBounds and writeU_Int8 — previously decided "in range" by testing buf[offset] === undefined. JSC currently returns undefined for a TypedArray indexed get at exactly 2**32 - 1 even when the element exists, so writing to the last byte of a kMaxLength buffer spuriously threw ERR_OUT_OF_RANGE. The fix replaces the probe with the same numeric offset >= 0 && offset <= buf.length - byteLength comparison the read side ($checkBufferRead) and the readIntLE/BE slow paths in JSBufferPrototype.ts already use, plus an explicit Math.floor(offset) !== offset guard so fractional offsets still get the "an integer" message via boundsError. A subprocess test in test/js/node/buffer.test.js allocates a 4 GiB buffer, exercises every affected write* at the boundary with read-back, and asserts one-past-end / fractional / negative offsets still throw ERR_OUT_OF_RANGE.

Security risks

None. This is a bounds-check relaxation only at the exact index the buffer already owns; every out-of-range, fractional, negative, NaN, and Infinity case still hits boundsError. The actual memory write is delegated to DataView.set*, which independently bounds-checks.

Level of scrutiny

Medium — Buffer write paths are hot and correctness-sensitive, but the change is tiny (two ~2-line predicates), mirrors an established pattern already in the file's callers, and only affects the slow path (fast path in JSBufferPrototype.ts is untouched; at the boundary it falls through to the now-correct slow path). I traced the predicate against every input class the old undefined-probe caught: fractional → Math.floor guard; negative/NaN/±Infinity → !(offset >= 0 && offset <= last); detached buffer → buf.length is 0 so last is negative; -0 → passes as 0 (matches Node). No behavior regressions.

Other factors

The test follows the neighbouring 4 GiB test's exact harness pattern (same skipIf(os.totalmem() < 10 GiB), subprocess with BUN_GARBAGE_COLLECTOR_LEVEL: "0", drains stdout/stderr/exited concurrently, asserts stderr empty and exit code last). It covers the full write* variant matrix including writeIntLE/BE and writeUIntLE/BE with byteLength 1 and 6, and the reject side (OOB, fractional, negative). Math.floor isn't a $-intrinsic, but boundsError two lines up already uses plain Math.floor, so this matches the file's existing tamper-safety level rather than regressing it.

@robobun

robobun commented Jul 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

Verified locally:

  • fail-before: USE_SYSTEM_BUN=1 bun test test/js/node/buffer.test.js -t 'accept offsets ending at the last byte' throws ERR_OUT_OF_RANGE in the subprocess
  • pass-after: bun bd test test/js/node/buffer.test.js -t 'accept offsets ending at the last byte' passes
  • full buffer.test.js (618 tests) and vendored test-buffer-{read,write}*.js all pass
  • ERR_OUT_OF_RANGE / ERR_BUFFER_OUT_OF_BOUNDS messages for fractional / NaN / Infinity / negative / too-short-buffer offsets match Node byte-for-byte

Self-review probed detached buffers, -0, non-zero byteOffset subarrays, writeIntLE always-slow-path perf, and the "fix this in JSC instead" alternative; none survived.

CI: all test lanes pass. The package-binary-size check is red because it compares against canary #79916 while this branch is cut from main #81770; the same ~550 KB delta appears on unrelated PRs (e.g. #35865). This diff bundles a 3-line comment plus two rewritten comparisons in internal/buffer.ts; the test file is not in the binary.

@github-actions

Copy link
Copy Markdown
Contributor

Closing this PR because it has been inactive for more than 90 days.

@github-actions github-actions Bot closed this Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant