Skip to content

Commit 64b2b89

Browse files
trevnorrisMyles Borins
authored and
Myles Borins
committed
buffer: properly retrieve binary length of needle
If the needle contains an extended latin-1 character then using String::Utf8Length() will be too large and the search will return early. Instead use String::Length() when encoding is BINARY. PR-URL: #4803 Reviewed-By: James M Snell <[email protected]>
1 parent 313ac20 commit 64b2b89

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/node_buffer.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,9 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
827827
Local<String> needle = args[1].As<String>();
828828
const char* haystack = ts_obj_data;
829829
const size_t haystack_length = ts_obj_length;
830-
const size_t needle_length = needle->Utf8Length();
830+
// Extended latin-1 characters are 2 bytes in Utf8.
831+
const size_t needle_length =
832+
enc == BINARY ? needle->Length() : needle->Utf8Length();
831833

832834

833835
if (needle_length == 0 || haystack_length == 0) {

test/parallel/test-buffer-indexof.js

+9
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ assert.equal(
109109
assert.equal(
110110
Buffer(b.toString('binary'), 'binary')
111111
.indexOf(Buffer('d', 'binary'), 0, 'binary'), 3);
112+
assert.equal(
113+
Buffer('aa\u00e8aa', 'binary')
114+
.indexOf('\u00e8', 'binary'), 2);
115+
assert.equal(
116+
Buffer('\u00e8', 'binary')
117+
.indexOf('\u00e8', 'binary'), 0);
118+
assert.equal(
119+
Buffer('\u00e8', 'binary')
120+
.indexOf(Buffer('\u00e8', 'binary'), 'binary'), 0);
112121

113122
{
114123
// test usc2 encoding

0 commit comments

Comments
 (0)