Skip to content

Commit a550ddb

Browse files
addaleaxevanlucas
authored andcommitted
buffer: fix needle length misestimation for UCS2
Use `StringBytes::Size` to determine the needle string length instead of assuming latin-1 or UTF-8. Previously, `Buffer.indexOf` could fail with an assertion failure when the needle's byte length, but not its character count, exceeded the haystack's byte length. PR-URL: #6511 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 6fc20c5 commit a550ddb

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/node_buffer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,9 +995,9 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
995995

996996
const char* haystack = ts_obj_data;
997997
const size_t haystack_length = ts_obj_length;
998-
// Extended latin-1 characters are 2 bytes in Utf8.
998+
999999
const size_t needle_length =
1000-
enc == BINARY ? needle->Length() : needle->Utf8Length();
1000+
StringBytes::Size(args.GetIsolate(), needle, enc);
10011001

10021002
if (needle_length == 0 || haystack_length == 0) {
10031003
return args.GetReturnValue().Set(-1);

test/parallel/test-buffer-indexof.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ var allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2');
222222
assert.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
223223
assert.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
224224

225+
// Needle is longer than haystack, but only because it's encoded as UTF-16
226+
assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1);
227+
228+
assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'utf8'), 0);
229+
assert.strictEqual(Buffer.from('aaaa').indexOf('你好', 'ucs2'), -1);
230+
225231
{
226232
// Find substrings in Utf8.
227233
const lengths = [1, 3, 15]; // Single char, simple and complex.

0 commit comments

Comments
 (0)