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 · nodejs/node@1e1266b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • buffer.js
  • test/pummel
    • test-buffer-indexof-large.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/buffer.js‎

Copy file name to clipboardExpand all lines: lib/buffer.js
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ Buffer.prototype.compare = function compare(target,
11141114
// Arguments:
11151115
// - buffer - a Buffer to search
11161116
// - val - a string, Buffer, or number
1117-
// - byteOffset - an index into `buffer`; will be clamped to an int32
1117+
// - byteOffset - an index into `buffer`; will be clamped to a safe integer
11181118
// - end - absolute exclusive end of the search range
11191119
// - encoding - an optional encoding, relevant if val is a string
11201120
// - dir - true for indexOf, false for lastIndexOf
@@ -1124,10 +1124,10 @@ function bidirectionalIndexOf(buffer, val, byteOffset, end, encoding, dir) {
11241124
if (typeof byteOffset === 'string') {
11251125
encoding = byteOffset;
11261126
byteOffset = undefined;
1127-
} else if (byteOffset > 0x7fffffff) {
1128-
byteOffset = 0x7fffffff;
1129-
} else if (byteOffset < -0x80000000) {
1130-
byteOffset = -0x80000000;
1127+
} else if (byteOffset > NumberMAX_SAFE_INTEGER) {
1128+
byteOffset = NumberMAX_SAFE_INTEGER;
1129+
} else if (byteOffset < NumberMIN_SAFE_INTEGER) {
1130+
byteOffset = NumberMIN_SAFE_INTEGER;
11311131
}
11321132
// Coerce to Number. Values like null and [] become 0.
11331133
byteOffset = +byteOffset;
Collapse file

‎test/pummel/test-buffer-indexof-large.js‎

Copy file name to clipboardExpand all lines: test/pummel/test-buffer-indexof-large.js
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,22 @@ assert.strictEqual(buffer.indexOf('\n', -1), match);
2525
assert.strictEqual(buffer.lastIndexOf(0x0a, -1), match);
2626
assert.strictEqual(buffer.lastIndexOf(Buffer.from([0x0a]), -1), match);
2727
assert.strictEqual(buffer.lastIndexOf('\n', -1), match);
28+
29+
// Offsets outside the int32 range must not be clamped.
30+
// Clear the ranges searched below, as allocUnsafe() does not zero memory.
31+
const before = 2 ** 31 + 10;
32+
const offset = 2 ** 31 + 50;
33+
buffer.fill(0, 0, 100);
34+
buffer.fill(0, 2 ** 31, match);
35+
buffer[60] = 0x0a;
36+
buffer[before] = 0x0a;
37+
38+
for (const value of [0x0a, Buffer.from([0x0a]), '\n']) {
39+
assert.strictEqual(buffer.indexOf(value, offset), match);
40+
assert.strictEqual(buffer.lastIndexOf(value, offset), before);
41+
assert.strictEqual(buffer.includes(value, match + 1), false);
42+
assert.strictEqual(buffer.indexOf(value, Infinity), -1);
43+
// buffer.length is 2 ** 31 + 101, so -offset resolves to index 51.
44+
assert.strictEqual(buffer.indexOf(value, -offset), 60);
45+
assert.strictEqual(buffer.lastIndexOf(value, -offset), -1);
46+
}

0 commit comments

Comments
0 (0)
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.