GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — buffer: stop clamping indexOf byteOffset to int32 by vedchaudhari · Pull Request #67009 · nodejs/node

Conversation

@vedchaudhari

@vedchaudhari vedchaudhari commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

On buffers bigger than 2 GiB, indexOf(), lastIndexOf() and includes() change any byteOffset above 2 ** 31 - 1 to 2 ** 31 - 1, so the search starts in the wrong place:

const G = 2 ** 30;
const buf = Buffer.alloc(3 * G);
buf[2.25 * G] = 0x0a;
buf[2.75 * G] = 0x0a;

buf.indexOf(0x0a, 2.5 * G);     // main: 2415919104 (before the offset), this PR: 2952790016
buf.includes(0x0a, 2.9 * G);    // main: true, this PR: false
buf.lastIndexOf(0x0a, 2.6 * G); // main: -1, this PR: 2415919104

The int32 limit was added in 2016 (6c1e5ad), when buffers couldn't be bigger than 2 GiB. MAX_LENGTH has grown a lot since v14, but the limit was never updated. The C++ side already uses 64-bit offsets, so I raised the limit to Number.MAX_SAFE_INTEGER, which still keeps values like Infinity out. The negative return values from the issue were already fixed by #66325.

The new cases in test/pummel/test-buffer-indexof-large.js fail on main and pass with this change. Results on normal-size buffers are unchanged.

Tested on macOS arm64.

Note: #66590 edits the same comment, so whichever lands second will need a small rebase.

Fixes: #67008

Assisted-by: Claude. I reproduced the bug, reviewed the change and ran the tests myself.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vedchaudhari
buffer: stop clamping indexOf byteOffset to int32
4fb3d72
bidirectionalIndexOf() clamped byteOffset to the int32 range, so on
buffers larger than 2 GiB an offset above 2 ** 31 - 1 started the
search at 2 ** 31 - 1 instead. indexOf() and includes() could then
match before the requested offset, and lastIndexOf() missed matches.
Offsets below -(2 ** 31) were clamped the same way.

The binding already takes an int64 offset, so clamp to the safe
integer range instead. This still keeps Infinity and other values
that int64 can't represent from reaching the binding.

Fixes: nodejs#67008
Refs: nodejs#66325
Assisted-by: claude:opus-5.5
Signed-off-by: vedchaudhari <[email protected]>
@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. needs-ci PRs that need a full CI run. labels Oct 26, 2026
@codecov

codecov Bot commented Oct 26, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.66%. Comparing base (3983092) to head (4fb3d72).
⚠️ Report is 280 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67009      +/-   ##
==========================================
- Coverage   92.96%   90.66%   -2.30%     
==========================================
  Files         428      797     +369     
  Lines      195232   278262   +83030     
  Branches    30184    53512   +23328     
==========================================
+ Hits       181492   252286   +70794     
- Misses      13413    16340    +2927     
- Partials      327     9636    +9309     
Files with missing lines Coverage Δ
lib/buffer.js 99.74% <100.00%> (+2.90%) ⬆️

... and 500 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.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vedchaudhari

vedchaudhari commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@addaleax since you reviewed #66325, would you have a moment to take a look at this one? It's the other half of #67008. Offsets past 2 GiB still get clamped to int32, so indexOf(), lastIndexOf() and includes() search from the wrong spot. Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vedchaudhari

vedchaudhari commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

@ronag since you added the end parameter, would you have a moment to take a look? start is still clamped to int32, so on buffers over 2 GiB indexOf(0x0a, 2.2G, 2.25G) can return 2.1G, outside [start, end). Uint8Array.prototype.indexOf on the same buffer returns -1. Still reproduces on current main.

cc @panva (reviewed #66325 and #62711)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ronag
ronag approved these changes Nov 16, 2026
View reviewed changes
@ronag ronag added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 2026
@nodejs-github-bot

nodejs-github-bot commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/79156/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@panva panva added author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue PRs queued for automated landing through the Commit Queue. labels Nov 16, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 924250d into nodejs:main Nov 16, 2026
80 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot

nodejs-github-bot commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Landed in 924250d

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 16, 2026
aduh95 pushed a commit that referenced this pull request Nov 19, 2026
@vedchaudhari @aduh95
buffer: stop clamping indexOf byteOffset to int32
9a8fe74
bidirectionalIndexOf() clamped byteOffset to the int32 range, so on
buffers larger than 2 GiB an offset above 2 ** 31 - 1 started the
search at 2 ** 31 - 1 instead. indexOf() and includes() could then
match before the requested offset, and lastIndexOf() missed matches.
Offsets below -(2 ** 31) were clamped the same way.

The binding already takes an int64 offset, so clamp to the safe
integer range instead. This still keeps Infinity and other values
that int64 can't represent from reaching the binding.

Fixes: #67008
Refs: #66325
Assisted-by: claude:opus-5.5
Signed-off-by: vedchaudhari <[email protected]>
PR-URL: #67009
Reviewed-By: Robert Nagy <[email protected]>
@github-actions github-actions Bot mentioned this pull request Nov 24, 2026
2026-11-25, Version 27.0.0-alpha.0.2.0 (Alpha) #67757
Merged
gabylb pushed a commit to gabylb/node that referenced this pull request Nov 27, 2026
@vedchaudhari @gabylb
buffer: stop clamping indexOf byteOffset to int32
1b15ef5
bidirectionalIndexOf() clamped byteOffset to the int32 range, so on
buffers larger than 2 GiB an offset above 2 ** 31 - 1 started the
search at 2 ** 31 - 1 instead. indexOf() and includes() could then
match before the requested offset, and lastIndexOf() missed matches.
Offsets below -(2 ** 31) were clamped the same way.

The binding already takes an int64 offset, so clamp to the safe
integer range instead. This still keeps Infinity and other values
that int64 can't represent from reaching the binding.

Fixes: nodejs#67008
Refs: nodejs#66325
Assisted-by: claude:opus-5.5
Signed-off-by: vedchaudhari <[email protected]>
PR-URL: nodejs#67009
Reviewed-By: Robert Nagy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ronag ronag ronag approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. buffer Issues and PRs related to the buffer subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

buffer: indexOf/includes clamp byteOffset to 2^31-1 and return a negative index on buffers over 2 GiB

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@vedchaudhari @nodejs-github-bot @ronag @panva
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.