Skip to content

Commit

Permalink
Fix error handling in array__remove() in cnex
Browse files Browse the repository at this point in the history
  • Loading branch information
gitlarryf committed Dec 18, 2023
1 parent e98b114 commit 6759ea4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions exec/cnex/global.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,15 +633,22 @@ void array__remove(TExecutor *exec)
Number index = top(exec->stack)->number; pop(exec->stack);
Cell *addr = top(exec->stack)->address; pop(exec->stack);

if (!number_is_integer(index)) {
if (!number_is_integer(index) || number_is_negative(index)) {
char buf[100];
snprintf(buf, sizeof(buf), "Array index not an integer: %s", number_to_string(index));
snprintf(buf, sizeof(buf), "Invalid array index: %s", number_to_string(index));
exec->rtl_raise(exec, "PANIC", buf);
return;
}

cell_ensureArray(addr);
size_t i = number_to_uint64(index);
if (i >= addr->array->size) {
char buf[100];
snprintf(buf, sizeof(buf), "Array index exceeds size %zd: %s", addr->array->size, number_to_string(index));
exec->rtl_raise(exec, "PANIC", buf);
return;
}

cell_clearCell(&addr->array->data[i]);
array_removeItem(addr->array, i);
}
Expand Down
5 changes: 5 additions & 0 deletions t/array-remove.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ a.remove(1)
TESTCASE a.size() = 2
TESTCASE a[0] = 1
TESTCASE a[1] = 3
TESTCASE a.remove(-1) EXPECT PANIC "Invalid array index: -1"
a.remove(0)
TESTCASE a[0] = 3
a.remove(0)
TESTCASE a.remove(0) EXPECT PANIC "Array index exceeds size 0: 0"

0 comments on commit 6759ea4

Please sign in to comment.