From: Wolfgang (Blub) Bumiller Date: Fri, 30 Nov 2012 15:23:34 +0000 (+0100) Subject: out-of-bounds indexing check on static array indexing X-Git-Tag: 0.1.9~169 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=9f1aa1490aef78bb92f15f5f0da547bae3480375;p=xonotic%2Fgmqcc.git out-of-bounds indexing check on static array indexing --- diff --git a/ast.c b/ast.c index 5a10154..7e59f03 100644 --- a/ast.c +++ b/ast.c @@ -2059,10 +2059,24 @@ bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lva return true; } - if (idx->expression.vtype == TYPE_FLOAT) - *out = arr->ir_values[(int)idx->constval.vfloat]; - else if (idx->expression.vtype == TYPE_INTEGER) - *out = arr->ir_values[idx->constval.vint]; + if (idx->expression.vtype == TYPE_FLOAT) { + unsigned int arridx = idx->constval.vfloat; + if (arridx >= self->array->expression.count) + { + compile_error(ast_ctx(self), "array index out of bounds: %i", arridx); + return false; + } + *out = arr->ir_values[arridx]; + } + else if (idx->expression.vtype == TYPE_INTEGER) { + unsigned int arridx = idx->constval.vint; + if (arridx >= self->array->expression.count) + { + compile_error(ast_ctx(self), "array index out of bounds: %i", arridx); + return false; + } + *out = arr->ir_values[arridx]; + } else { compile_error(ast_ctx(self), "array indexing here needs an integer constant"); return false;