Skip to content

Commit

Permalink
src: convert BE-utf16-string to LE before search
Browse files Browse the repository at this point in the history
On Big Endian platforms v8 strings are need to converted
to Little Endian before searching in utf16le buffer

Fixes: nodejs#3283
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Michael Dawson <mhdawson@ca.ibm.com>
PR-URL: nodejs#3295
  • Loading branch information
skomski authored and Trott committed Oct 12, 2015
1 parent ed8eb8f commit dfe6788
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -842,11 +842,27 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
return args.GetReturnValue().Set(-1);
}

result = SearchString(reinterpret_cast<const uint16_t*>(haystack),
haystack_length / 2,
reinterpret_cast<const uint16_t*>(*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<const uint16_t*>(decoder.out());

if (decoded_string == nullptr)
return args.GetReturnValue().Set(-1);

result = SearchString(reinterpret_cast<const uint16_t*>(haystack),
haystack_length / 2,
decoded_string,
decoder.size() / 2,
offset / 2);
} else {
result = SearchString(reinterpret_cast<const uint16_t*>(haystack),
haystack_length / 2,
reinterpret_cast<const uint16_t*>(*needle_value),
needle_value.length(),
offset / 2);
}
result *= 2;
} else if (enc == UTF8) {
String::Utf8Value needle_value(needle);
Expand Down

0 comments on commit dfe6788

Please sign in to comment.