Skip to content

Commit

Permalink
buffer: fix needle length misestimation for UCS2
Browse files Browse the repository at this point in the history
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 <jasnell@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
addaleax authored and Myles Borins committed May 18, 2016
1 parent 7b60b8f commit 12a9699
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -827,9 +827,9 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
Local<String> needle = args[1].As<String>();
const char* haystack = ts_obj_data;
const size_t haystack_length = ts_obj_length;
// Extended latin-1 characters are 2 bytes in Utf8.

const size_t needle_length =
enc == BINARY ? needle->Length() : needle->Utf8Length();
StringBytes::Size(args.GetIsolate(), needle, enc);


if (needle_length == 0 || haystack_length == 0) {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-buffer-indexof.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2');
assert.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
assert.equal(-1, allCharsBufferUcs2.indexOf('notfound'));

// Needle is longer than haystack, but only because it's encoded as UTF-16
assert.strictEqual(new Buffer('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1);

assert.strictEqual(new Buffer('aaaa').indexOf('a'.repeat(4), 'utf8'), 0);
assert.strictEqual(new Buffer('aaaa').indexOf('你好', 'ucs2'), -1);

{
// Find substrings in Utf8.
const lengths = [1, 3, 15]; // Single char, simple and complex.
Expand Down

0 comments on commit 12a9699

Please sign in to comment.