Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ Buffer.prototype.compare = function compare(target,
// Arguments:
// - buffer - a Buffer to search
// - val - a string, Buffer, or number
// - byteOffset - an index into `buffer`; will be clamped to an int32
// - byteOffset - an index into `buffer`; will be clamped to a safe integer
// - end - absolute exclusive end of the search range
// - encoding - an optional encoding, relevant if val is a string
// - dir - true for indexOf, false for lastIndexOf
Expand All @@ -1118,10 +1118,10 @@ function bidirectionalIndexOf(buffer, val, byteOffset, end, encoding, dir) {
if (typeof byteOffset === 'string') {
encoding = byteOffset;
byteOffset = undefined;
} else if (byteOffset > 0x7fffffff) {
byteOffset = 0x7fffffff;
} else if (byteOffset < -0x80000000) {
byteOffset = -0x80000000;
} else if (byteOffset > NumberMAX_SAFE_INTEGER) {
byteOffset = NumberMAX_SAFE_INTEGER;
} else if (byteOffset < NumberMIN_SAFE_INTEGER) {
byteOffset = NumberMIN_SAFE_INTEGER;
}
// Coerce to Number. Values like null and [] become 0.
byteOffset = +byteOffset;
Expand Down
19 changes: 19 additions & 0 deletions test/pummel/test-buffer-indexof-large.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,22 @@ assert.strictEqual(buffer.indexOf('\n', -1), match);
assert.strictEqual(buffer.lastIndexOf(0x0a, -1), match);
assert.strictEqual(buffer.lastIndexOf(Buffer.from([0x0a]), -1), match);
assert.strictEqual(buffer.lastIndexOf('\n', -1), match);

// Offsets outside the int32 range must not be clamped.
// Clear the ranges searched below, as allocUnsafe() does not zero memory.
const before = 2 ** 31 + 10;
const offset = 2 ** 31 + 50;
buffer.fill(0, 0, 100);
buffer.fill(0, 2 ** 31, match);
buffer[60] = 0x0a;
buffer[before] = 0x0a;

for (const value of [0x0a, Buffer.from([0x0a]), '\n']) {
assert.strictEqual(buffer.indexOf(value, offset), match);
assert.strictEqual(buffer.lastIndexOf(value, offset), before);
assert.strictEqual(buffer.includes(value, match + 1), false);
assert.strictEqual(buffer.indexOf(value, Infinity), -1);
// buffer.length is 2 ** 31 + 101, so -offset resolves to index 51.
assert.strictEqual(buffer.indexOf(value, -offset), 60);
assert.strictEqual(buffer.lastIndexOf(value, -offset), -1);
}
Loading