Repository navigation
buffer: stop clamping indexOf byteOffset to int32 - #67009
Open
vedchaudhari wants to merge 1 commit into
Open
vedchaudhari wants to merge 1 commit into
vedchaudhari wants to merge 1 commit into
Conversation
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 <vedc2853@gmail.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
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.
On buffers bigger than 2 GiB,
indexOf(),lastIndexOf()andincludes()change anybyteOffsetabove2 ** 31 - 1to2 ** 31 - 1, so the search starts in the wrong place:The int32 limit was added in 2016 (6c1e5ad), when buffers couldn't be bigger than 2 GiB.
MAX_LENGTHhas grown a lot since v14, but the limit was never updated. The C++ side already uses 64-bit offsets, so I raised the limit toNumber.MAX_SAFE_INTEGER, which still keeps values likeInfinityout. The negative return values from the issue were already fixed by #66325.The new cases in
test/pummel/test-buffer-indexof-large.jsfail onmainand 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.