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: https://github.com/nodejs/node/pull/6511
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
Anna Henningsen committed 2016-05-04 22:13:30 +02:00
1 parent fc66e55396
commit 5defa0cbaa
2 files changed
+8 -2

No files matched your search

+2 -2
View File
@@ -995,9 +995,9 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
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) {
return args.GetReturnValue().Set(-1);