Skip to content

Commit 589287b

Browse files
skomskijasnell
authored andcommitted
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: #3283 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Michael Dawson <[email protected]> PR-URL: #3295
1 parent 2314378 commit 589287b

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/node_buffer.cc

+21-5
Original file line numberDiff line numberDiff line change
@@ -842,11 +842,27 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
842842
return args.GetReturnValue().Set(-1);
843843
}
844844

845-
result = SearchString(reinterpret_cast<const uint16_t*>(haystack),
846-
haystack_length / 2,
847-
reinterpret_cast<const uint16_t*>(*needle_value),
848-
needle_value.length(),
849-
offset / 2);
845+
if (IsBigEndian()) {
846+
StringBytes::InlineDecoder decoder;
847+
decoder.Decode(Environment::GetCurrent(args), needle, args[3], UCS2);
848+
const uint16_t* decoded_string =
849+
reinterpret_cast<const uint16_t*>(decoder.out());
850+
851+
if (decoded_string == nullptr)
852+
return args.GetReturnValue().Set(-1);
853+
854+
result = SearchString(reinterpret_cast<const uint16_t*>(haystack),
855+
haystack_length / 2,
856+
decoded_string,
857+
decoder.size() / 2,
858+
offset / 2);
859+
} else {
860+
result = SearchString(reinterpret_cast<const uint16_t*>(haystack),
861+
haystack_length / 2,
862+
reinterpret_cast<const uint16_t*>(*needle_value),
863+
needle_value.length(),
864+
offset / 2);
865+
}
850866
result *= 2;
851867
} else if (enc == UTF8) {
852868
String::Utf8Value needle_value(needle);

0 commit comments

Comments
 (0)