Added an out-of-bounds check

This commit is contained in:
2022-09-09 19:52:36 +01:00
parent f2443fbde0
commit d3c085c300
3 changed files with 32 additions and 2 deletions

View File

@@ -1606,7 +1606,23 @@ static bool execIndexAssign(Interpreter* interpreter) {
//call the function
NativeFn fn = (NativeFn)AS_FUNCTION(func).bytecode;
fn(interpreter, &arguments);
if (fn(interpreter, &arguments) == -1) {
//clean up
freeLiteral(assign);
freeLiteral(third);
freeLiteral(second);
freeLiteral(first);
freeLiteral(compound);
if (freeIdn) {
freeLiteral(idn);
}
freeLiteral(func);
freeLiteral(key);
freeLiteral(op);
freeLiteralArray(&arguments);
return false;
}
//save the result (assume top of the interpreter stack is the new compound value)
Literal result = popLiteralArray(&interpreter->stack);

View File

@@ -495,7 +495,19 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) {
if (IS_NULL(second)) {
//set the "first" within the array, then skip out
setLiteralArray(AS_ARRAY(compound), first, assign);
if (!setLiteralArray(AS_ARRAY(compound), first, assign)) {
interpreter->errorOutput("Index assignment out of bounds\n");
freeLiteral(op);
freeLiteral(assign);
freeLiteral(third);
freeLiteral(second);
freeLiteral(first);
freeLiteral(compound);
freeLiteral(value);
return -1;
}
pushLiteralArray(&interpreter->stack, compound);

View File

@@ -77,6 +77,8 @@ bool setLiteralArray(LiteralArray* array, Literal index, Literal value) {
return false;
}
//TODO: implicit push when referencing one-past-the-end?
freeLiteral(array->literals[idx]);
array->literals[idx] = copyLiteral(value);