mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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:
2 files changed
+8
-2
No files matched your search
+2
-2
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user