mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
buffer: fix UCS2 indexOf for odd buffer length
Fix `buffer.indexOf` for the case that the haystack has odd length and the needle is not found in it. `StringSearch()` would return the length of the buffer in multiples of `sizeof(uint16_t)`, but checking that against `haystack_length` would not work if the latter one was odd. 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:
1 parent
12a9699fcf
commit
8b077faa82
2 files changed
+6
-1
No files matched your search
+3
-1
@@ -826,7 +826,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;
|
||||
// Round down to the nearest multiple of 2 in case of UCS2.
|
||||
const size_t haystack_length = (enc == UCS2) ?
|
||||
ts_obj_length &~ 1 : ts_obj_length; // NOLINT(whitespace/operators)
|
||||
|
||||
const size_t needle_length =
|
||||
StringBytes::Size(args.GetIsolate(), needle, enc);
|
||||
|
||||
@@ -239,6 +239,9 @@ 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);
|
||||
|
||||
// Haystack has odd length, but the needle is UCS2.
|
||||
assert.strictEqual(new Buffer('aaaaa').indexOf('b', 'ucs2'), -1);
|
||||
|
||||
{
|
||||
// Find substrings in Utf8.
|
||||
const lengths = [1, 3, 15]; // Single char, simple and complex.
|
||||
|
||||
Reference in new issue
Block a user