From dfe6788b882eb322723105bcf233246d03972d8f Mon Sep 17 00:00:00 2001 From: Karl Skomski Date: Fri, 9 Oct 2015 18:09:52 +0200 Subject: [PATCH] src: convert BE-utf16-string to LE before search On Big Endian platforms v8 strings are need to converted to Little Endian before searching in utf16le buffer Fixes: https://github.com/nodejs/node/issues/3283 Reviewed-By: James M Snell Reviewed-By: Trevor Norris Reviewed-By: Michael Dawson PR-URL: https://github.com/nodejs/node/pull/3295 --- src/node_buffer.cc | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 0492987570ea2d..4df7a9f1b4d065 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -842,11 +842,27 @@ void IndexOfString(const FunctionCallbackInfo& args) { return args.GetReturnValue().Set(-1); } - result = SearchString(reinterpret_cast(haystack), - haystack_length / 2, - reinterpret_cast(*needle_value), - needle_value.length(), - offset / 2); + if (IsBigEndian()) { + StringBytes::InlineDecoder decoder; + decoder.Decode(Environment::GetCurrent(args), needle, args[3], UCS2); + const uint16_t* decoded_string = + reinterpret_cast(decoder.out()); + + if (decoded_string == nullptr) + return args.GetReturnValue().Set(-1); + + result = SearchString(reinterpret_cast(haystack), + haystack_length / 2, + decoded_string, + decoder.size() / 2, + offset / 2); + } else { + result = SearchString(reinterpret_cast(haystack), + haystack_length / 2, + reinterpret_cast(*needle_value), + needle_value.length(), + offset / 2); + } result *= 2; } else if (enc == UTF8) { String::Utf8Value needle_value(needle);