summaryrefslogtreecommitdiff
path: root/src/node_buffer.cc
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2016-05-02 07:51:33 +0200
committerAnna Henningsen <anna@addaleax.net>2016-05-04 22:14:15 +0200
commit4d49bcb73edb12992a0a01c10c9d961a59b6fee4 (patch)
tree76de519fe2b295d069a6e902d77133e1a0c106ca /src/node_buffer.cc
parent5defa0cbaa9c6d72420df32f6a67fe55edc8160a (diff)
downloadandroid-node-v8-4d49bcb73edb12992a0a01c10c9d961a59b6fee4.tar.gz
android-node-v8-4d49bcb73edb12992a0a01c10c9d961a59b6fee4.tar.bz2
android-node-v8-4d49bcb73edb12992a0a01c10c9d961a59b6fee4.zip
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>
Diffstat (limited to 'src/node_buffer.cc')
-rw-r--r--src/node_buffer.cc4
1 files changed, 3 insertions, 1 deletions
diff --git a/src/node_buffer.cc b/src/node_buffer.cc
index 30618719d2..44020d91a1 100644
--- a/src/node_buffer.cc
+++ b/src/node_buffer.cc
@@ -994,7 +994,9 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
bool is_forward = args[4]->IsTrue();
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);