Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/jsc/bindings/JSBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,7 @@ static int64_t indexOf(JSC::JSGlobalObject* lexicalGlobalObject, ThrowScope& sco
if (byteOffsetValue.isString()) {
encodingValue = byteOffsetValue;
byteOffsetValue = jsUndefined();
byteOffsetD = 0;
byteOffsetD = std::numeric_limits<double>::quiet_NaN();
} else {
// toNumber() can trigger JavaScript execution (valueOf/Symbol.toPrimitive),
// which could detach the underlying ArrayBuffer. We must re-fetch the
Expand Down
26 changes: 26 additions & 0 deletions test/js/node/buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2256,6 +2256,32 @@ for (let withOverridenBufferWrite of [false, true]) {
expect(b.lastIndexOf("b", [])).toBe(-1);
});

it("lastIndexOf(value, encoding) defaults to searching from the end", () => {
// When the second argument is an encoding string (no byteOffset), the
// search must start from the end of the buffer, matching Node.js.
const b = Buffer.from("ello hello hello");
expect(b.lastIndexOf("hello", "utf8")).toBe(11);
expect(b.lastIndexOf("hello", "latin1")).toBe(11);
expect(b.lastIndexOf("hello", "binary")).toBe(11);

const b16 = Buffer.from("ello hello hello", "utf16le");
expect(b16.lastIndexOf("hello", "utf16le")).toBe(22);
expect(b16.lastIndexOf("hello", "ucs2")).toBe(22);

const bhex = Buffer.from("aabbccaabbcc", "hex");
expect(bhex.lastIndexOf("aabb", "hex")).toBe(3);

const bb64 = Buffer.from("Zm9vYmFyZm9v", "base64");
expect(bb64.lastIndexOf("Zm9v", "base64")).toBe(6);

// Forward indexOf with the same overload must remain 0-based.
expect(b.indexOf("hello", "utf8")).toBe(5);
expect(b.includes("hello", "utf8")).toBe(true);

// Explicit byteOffset still works.
expect(b.lastIndexOf("hello", 8, "utf8")).toBe(5);
});

for (let fn of [Buffer.prototype.slice, Buffer.prototype.subarray]) {
it(`Buffer.${fn.name}`, () => {
const buf = new Buffer("buffer");
Expand Down
Loading