GeezerVison is showing github.com — buffer: stop clamping indexOf byteOffset to int32 · nodejs/node@1e1266b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/buffer.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1114,7 +1114,7 @@ Buffer.prototype.compare = function compare(target, | 1114 | 1114 | // Arguments: | 1115 | 1115 | // - buffer - a Buffer to search | 1116 | 1116 | // - 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 | 1118 | 1118 | // - end - absolute exclusive end of the search range | 1119 | 1119 | // - encoding - an optional encoding, relevant if val is a string | 1120 | 1120 | // - dir - true for indexOf, false for lastIndexOf | @@ -1124,10 +1124,10 @@ function bidirectionalIndexOf(buffer, val, byteOffset, end, encoding, dir) { | 1124 | 1124 | if (typeof byteOffset === 'string') { | 1125 | 1125 | encoding = byteOffset; | 1126 | 1126 | 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; | 1131 | 1131 | } | 1132 | 1132 | // Coerce to Number. Values like null and [] become 0. | 1133 | 1133 | byteOffset = +byteOffset; | |
Collapse file Copy file name to clipboardExpand all lines: test/pummel/test-buffer-indexof-large.js +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,3 +25,22 @@ assert.strictEqual(buffer.indexOf('\n', -1), match); | 25 | 25 | assert.strictEqual(buffer.lastIndexOf(0x0a, -1), match); | 26 | 26 | assert.strictEqual(buffer.lastIndexOf(Buffer.from([0x0a]), -1), match); | 27 | 27 | 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 | +} |
|