]> de.git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
out-of-bounds indexing check on static array indexing
authorWolfgang (Blub) Bumiller <blub@speed.at>
Fri, 30 Nov 2012 15:23:34 +0000 (16:23 +0100)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Fri, 30 Nov 2012 15:23:34 +0000 (16:23 +0100)
ast.c

diff --git a/ast.c b/ast.c
index 5a10154c44e1b237be43afc9cd9f36abe66bbebc..7e59f0331aca46fda668dc1cfb73bf12a756e511 100644 (file)
--- 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;