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
24 changes: 14 additions & 10 deletions src/jsc/bindings/JSBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1615,9 +1615,10 @@ static int64_t lastIndexOf(const uint8_t* thisPtr, int64_t thisLength, const uin
// upper bound of the search range. Returns true when a real search should run,
// with *offsetOut / *searchEndOut set; otherwise *immediateResult holds the
// value to return (the clamped offset for an empty needle, or -1).
static bool computeIndexOfRange(size_t haystackLength, double byteOffsetD, double endD, size_t needleLength, bool isForward, size_t* offsetOut, size_t* searchEndOut, int64_t* immediateResult)
static bool computeIndexOfRange(size_t haystackLength, double byteOffsetD, double endD, size_t needleLength, bool isForward, bool isUTF16, size_t* offsetOut, size_t* searchEndOut, int64_t* immediateResult)
{
size_t searchEnd = static_cast<size_t>(std::min<double>(std::max<double>(endD, 0), static_cast<double>(haystackLength)));
if (isUTF16) searchEnd &= ~static_cast<size_t>(1);
ssize_t optOffset = indexOfOffset(haystackLength, static_cast<ssize_t>(byteOffsetD), static_cast<ssize_t>(needleLength), isForward);
Comment thread
robobun marked this conversation as resolved.
Resolved

if (needleLength == 0) {
Expand Down Expand Up @@ -1655,7 +1656,7 @@ static int64_t indexOfNumber(JSC::JSGlobalObject* lexicalGlobalObject, bool last
size_t byteOffset = 0;
size_t searchEnd = 0;
int64_t immediateResult = -1;
if (!computeIndexOfRange(byteLength, byteOffsetD, endD, 1, !last, &byteOffset, &searchEnd, &immediateResult))
if (!computeIndexOfRange(byteLength, byteOffsetD, endD, 1, !last, false, &byteOffset, &searchEnd, &immediateResult))
return immediateResult;

auto span = std::span<const uint8_t>(typedVector, searchEnd);
Expand Down Expand Up @@ -1689,17 +1690,19 @@ static int64_t indexOfString(JSC::JSGlobalObject* lexicalGlobalObject, bool last
auto* arrayValue = uncheckedDowncast<JSC::JSUint8Array>(JSC::JSValue::decode(encodedBuffer));
size_t needleLength = arrayValue->byteLength();

size_t haystackLength = isUTF16Encoding(encoding) ? byteLength & ~static_cast<size_t>(1) : byteLength;
const bool isUTF16 = isUTF16Encoding(encoding);
// Node's IndexOfString rounds haystack_length down to even for UCS2 before
// IndexOfOffset (unlike IndexOfBuffer, which uses the raw byte length).
size_t haystackLength = isUTF16 ? byteLength & ~static_cast<size_t>(1) : byteLength;

size_t byteOffset = 0;
size_t searchEnd = 0;
int64_t immediateResult = -1;
if (!computeIndexOfRange(haystackLength, byteOffsetD, endD, needleLength, !last, &byteOffset, &searchEnd, &immediateResult))
if (!computeIndexOfRange(haystackLength, byteOffsetD, endD, needleLength, !last, isUTF16, &byteOffset, &searchEnd, &immediateResult))
return immediateResult;
if (isUTF16Encoding(encoding)) searchEnd &= ~static_cast<size_t>(1);

const uint8_t* typedVectorValue = arrayValue->typedVector();
if (isUTF16Encoding(encoding)) {
if (isUTF16) {
return last ? lastIndexOf16(typedVector, searchEnd, typedVectorValue, needleLength, byteOffset)
: indexOf16(typedVector, searchEnd, typedVectorValue, needleLength, byteOffset);
}
Expand All @@ -1710,17 +1713,18 @@ static int64_t indexOfString(JSC::JSGlobalObject* lexicalGlobalObject, bool last
static int64_t indexOfBuffer(JSC::JSGlobalObject* lexicalGlobalObject, bool last, const uint8_t* typedVector, size_t byteLength, double byteOffsetD, double endD, JSC::JSGenericTypedArrayView<JSC::Uint8Adaptor>* array, BufferEncodingType encoding)
{
size_t needleLength = array->byteLength();
size_t haystackLength = isUTF16Encoding(encoding) ? byteLength & ~static_cast<size_t>(1) : byteLength;
const bool isUTF16 = isUTF16Encoding(encoding);

size_t byteOffset = 0;
size_t searchEnd = 0;
int64_t immediateResult = -1;
if (!computeIndexOfRange(haystackLength, byteOffsetD, endD, needleLength, !last, &byteOffset, &searchEnd, &immediateResult))
// Node's IndexOfBuffer wraps negative offsets against the raw byte length,
// then floors to 16-bit units only for the search itself.
if (!computeIndexOfRange(byteLength, byteOffsetD, endD, needleLength, !last, isUTF16, &byteOffset, &searchEnd, &immediateResult))
return immediateResult;
Comment thread
robobun marked this conversation as resolved.
Resolved
if (isUTF16Encoding(encoding)) searchEnd &= ~static_cast<size_t>(1);

const uint8_t* typedVectorValue = array->typedVector();
if (isUTF16Encoding(encoding)) {
if (isUTF16) {
return last ? lastIndexOf16(typedVector, searchEnd, typedVectorValue, needleLength, byteOffset)
: indexOf16(typedVector, searchEnd, typedVectorValue, needleLength, byteOffset);
}
Expand Down
56 changes: 56 additions & 0 deletions test/js/node/buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,62 @@ for (let withOverridenBufferWrite of [false, true]) {
expect(b.lastIndexOf("b", [])).toBe(-1);
});

it("lastIndexOf/indexOf(Buffer, negativeOffset, 'ucs2') wraps against the raw byte length on odd-length haystacks", () => {
// Node's IndexOfBuffer wraps a negative byteOffset against the full
// byte length and only then floors to 16-bit units; truncating to an
// even length first makes `-byteLength` land before the start and
// return -1 for data that is present.
Comment thread
robobun marked this conversation as resolved.
Resolved
const h3 = Buffer.from("bbc", "latin1"); // <62 62 63>
const n = Buffer.from("bb", "latin1"); // <62 62>
for (const enc of ["ucs2", "utf16le"]) {
expect(h3.lastIndexOf(n, -3, enc)).toBe(0);
expect(h3.lastIndexOf(n, -2, enc)).toBe(0);
expect(h3.lastIndexOf(n, 0, enc)).toBe(0);
expect(h3.lastIndexOf(n, -4, enc)).toBe(-1);
expect(h3.indexOf(n, -3, enc)).toBe(0);
expect(h3.indexOf(n, -2, enc)).toBe(-1);
expect(h3.indexOf(n, -1, enc)).toBe(-1);
expect(h3.indexOf(n, 1, enc)).toBe(-1);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// Even-length haystack is unchanged.
expect(Buffer.from("bbcc", "latin1").lastIndexOf(n, -4, "ucs2")).toBe(0);

// 5-byte haystack: the wrapped offset must also floor to the correct
// 16-bit unit so the rightmost match is found.
const h5 = Buffer.from([0x62, 0x62, 0x62, 0x62, 0x63]);
expect(h5.lastIndexOf(n, -5, "ucs2")).toBe(0);
expect(h5.lastIndexOf(n, -3, "ucs2")).toBe(2);
expect(h5.lastIndexOf(n, -6, "ucs2")).toBe(-1);
// Odd-length needle: only the whole 16-bit unit participates.
const n3 = Buffer.from([0x62, 0x62, 0x62]);
expect(h5.lastIndexOf(n3, -5, "ucs2")).toBe(0);
expect(h5.lastIndexOf(n3, 0, "ucs2")).toBe(0);

// Empty Buffer needle on an odd-length haystack: the clamped result
// must reflect the raw-byte wrap, bounded by the even search end.
const empty = Buffer.alloc(0);
expect(h3.lastIndexOf(empty, -3, "ucs2")).toBe(0);
expect(h3.lastIndexOf(empty, -1, "ucs2")).toBe(2);
expect(h3.lastIndexOf(empty, 3, "ucs2")).toBe(2);
expect(h3.lastIndexOf(empty, undefined, "ucs2")).toBe(2);

// 1-byte haystack has no 16-bit units.
const h1 = Buffer.from([0x62]);
expect(h1.lastIndexOf(n, 0, "ucs2")).toBe(-1);
expect(h1.lastIndexOf(n, -1, "ucs2")).toBe(-1);
expect(h1.lastIndexOf(empty, 0, "ucs2")).toBe(0);

// String needles: Node's IndexOfString truncates the haystack length to
// even BEFORE wrapping (unlike IndexOfBuffer), so -byteLength on an
// odd-length haystack is before the start.
const sn = "\u6262"; // encodes as <62 62> in ucs2
expect(h3.lastIndexOf(sn, -3, "ucs2")).toBe(-1);
expect(h3.lastIndexOf(sn, -2, "ucs2")).toBe(0);
expect(h5.lastIndexOf(sn, -5, "ucs2")).toBe(-1);
expect(h5.lastIndexOf(sn, -3, "ucs2")).toBe(0);
});

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.
Expand Down
Loading