Skip to content

Commit

Permalink
Fixed Buffer.prototype.indexOf() and friends.
Browse files Browse the repository at this point in the history
With empty buffers in both the self and search cases.
  • Loading branch information
xeioex committed Oct 10, 2024
1 parent 3a808d7 commit b4b40a3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 62 deletions.
39 changes: 8 additions & 31 deletions src/njs_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2117,10 +2117,6 @@ njs_buffer_prototype_index_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,

index = -1;

if (njs_slow_path(array->byte_length == 0)) {
goto done;
}

length = array->byte_length;

if (last) {
Expand All @@ -2145,30 +2141,11 @@ njs_buffer_prototype_index_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
return ret;
}

if (last) {
if (from >= 0) {
from = njs_min(from, length - 1);

} else if (from < 0) {
from += length;
}

if (from <= to) {
goto done;
}
if (from >= 0) {
from = njs_min(from, length);

} else {
if (from < 0) {
from += length;

if (from < 0) {
from = 0;
}
}

if (from >= to) {
goto done;
}
from = njs_max(0, length + from);
}
}

Expand Down Expand Up @@ -2213,11 +2190,6 @@ njs_buffer_prototype_index_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
str.length = src->byte_length;
}

if (njs_slow_path(str.length == 0)) {
index = (last) ? length : 0;
goto done;
}

if (last) {
from = njs_min(from, length - (int64_t) str.length);

Expand All @@ -2233,6 +2205,11 @@ njs_buffer_prototype_index_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
}
}

if (from == to && str.length == 0) {
index = 0;
goto done;
}

for (i = from; i != to; i += increment) {
if (memcmp(&u8[i], str.start, str.length) == 0) {
index = i;
Expand Down
39 changes: 8 additions & 31 deletions src/qjs_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -988,10 +988,6 @@ qjs_buffer_prototype_index_of(JSContext *ctx, JSValueConst this_val, int argc,

length = self.length;

if (length == 0) {
return JS_NewInt32(ctx, -1);
}

if (last) {
from = length - 1;
to = -1;
Expand All @@ -1015,30 +1011,11 @@ qjs_buffer_prototype_index_of(JSContext *ctx, JSValueConst this_val, int argc,
return JS_EXCEPTION;
}

if (last) {
if (from >= 0) {
from = njs_min(from, length - 1);

} else if (from < 0) {
from += length;
}

if (from <= to) {
return JS_NewInt32(ctx, -1);
}
if (from >= 0) {
from = njs_min(from, length);

} else {
if (from < 0) {
from += length;

if (from < 0) {
from = 0;
}
}

if (from >= to) {
return JS_NewInt32(ctx, -1);
}
from = njs_max(0, length + from);
}
}

Expand Down Expand Up @@ -1086,11 +1063,6 @@ qjs_buffer_prototype_index_of(JSContext *ctx, JSValueConst this_val, int argc,
"or Buffer-like object");
}

if (str.length == 0) {
JS_FreeValue(ctx, buffer);
return JS_NewInt32(ctx, (last) ? length : 0);
}

if (last) {
from = njs_min(from, length - (int64_t) str.length);

Expand All @@ -1106,6 +1078,11 @@ qjs_buffer_prototype_index_of(JSContext *ctx, JSValueConst this_val, int argc,
}
}

if (from == to && str.length == 0) {
JS_FreeValue(ctx, buffer);
return JS_NewInt32(ctx, 0);
}

for (i = from; i != to; i += increment) {
if (memcmp(&self.start[i], str.start, str.length) == 0) {
JS_FreeValue(ctx, buffer);
Expand Down
26 changes: 26 additions & 0 deletions test/buffer.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,19 @@ let indexOf_tsuite = {
{ buf: Buffer.from('abcdef'), value: 'def', offset: 1, expected: 3 },
{ buf: Buffer.from('abcdef'), value: 'def', offset: -3, expected: 3 },
{ buf: Buffer.from('abcdef'), value: 'efgh', offset: 4, expected: -1 },
{ buf: Buffer.from(''), value: '', expected: 0 },
{ buf: Buffer.from(''), value: '', offset: -1, expected: 0 },
{ buf: Buffer.from(''), value: '', offset: 0, expected: 0 },
{ buf: Buffer.from(''), value: '', offset: 1, expected: 0 },
{ buf: Buffer.from('abc'), value: '', offset: -4, expected: 0 },
{ buf: Buffer.from('abc'), value: '', offset: -3, expected: 0 },
{ buf: Buffer.from('abc'), value: '', offset: -2, expected: 1 },
{ buf: Buffer.from('abc'), value: '', offset: -1, expected: 2 },
{ buf: Buffer.from('abc'), value: '', offset: 0, expected: 0 },
{ buf: Buffer.from('abc'), value: '', offset: 1, expected: 1 },
{ buf: Buffer.from('abc'), value: '', offset: 2, expected: 2 },
{ buf: Buffer.from('abc'), value: '', offset: 3, expected: 3 },
{ buf: Buffer.from('abc'), value: '', offset: 4, expected: 3 },
{ buf: Buffer.from('abcdef'), value: '626364', encoding: 'hex', expected: 1 },
{ buf: Buffer.from('abcdef'), value: '626364', encoding: 'utf-128',
exception: 'TypeError: "utf-128" encoding is not supported' },
Expand Down Expand Up @@ -584,6 +597,19 @@ let lastIndexOf_tsuite = {
{ buf: Buffer.from('abcdef'), value: 'abc', offset: 1, expected: 0 },
{ buf: Buffer.from('abcdef'), value: 'def', offset: 1, expected: -1 },
{ buf: Buffer.from('xxxABCx'), value: 'ABC', offset: 3, expected: 3 },
{ buf: Buffer.from(''), value: '', expected: 0 },
{ buf: Buffer.from(''), value: '', offset: -1, expected: 0 },
{ buf: Buffer.from(''), value: '', offset: 0, expected: 0 },
{ buf: Buffer.from(''), value: '', offset: 1, expected: 0 },
{ buf: Buffer.from('abc'), value: '', offset: -4, expected: 0 },
{ buf: Buffer.from('abc'), value: '', offset: -3, expected: 0 },
{ buf: Buffer.from('abc'), value: '', offset: -2, expected: 1 },
{ buf: Buffer.from('abc'), value: '', offset: -1, expected: 2 },
{ buf: Buffer.from('abc'), value: '', offset: 0, expected: 0 },
{ buf: Buffer.from('abc'), value: '', offset: 1, expected: 1 },
{ buf: Buffer.from('abc'), value: '', offset: 2, expected: 2 },
{ buf: Buffer.from('abc'), value: '', offset: 3, expected: 3 },
{ buf: Buffer.from('abc'), value: '', offset: 4, expected: 3 },
{ buf: Buffer.from(Buffer.alloc(7).fill('Zabcdef').buffer, 1), value: 'abcdef', expected: 0 },
{ buf: Buffer.from(Buffer.alloc(7).fill('Zabcdef').buffer, 1), value: 'abcdefg', expected: -1 },
{ buf: Buffer.from('abcdef'), value: '626364', encoding: 'hex', expected: 1 },
Expand Down

0 comments on commit b4b40a3

Please sign in to comment.