From 844e84fc16ac61404f66c042e5af09f00d68dcc4 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Tue, 27 Aug 2013 10:09:31 +0200 Subject: [PATCH 1/2] bail out when encountering an invalid array size in the ast as it's likely to cause breakage when trying to generate them anyway --- ast.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ast.c b/ast.c index 32ab1a2..1d13d8b 100644 --- a/ast.c +++ b/ast.c @@ -1396,8 +1396,10 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield) } /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */ - if (!array->expression.count || array->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) + if (!array->expression.count || array->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) { compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count); + return false; + } elemtype = array->expression.next; vtype = elemtype->vtype; @@ -1463,8 +1465,10 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield) } /* same as with field arrays */ - if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) + if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) { compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count); + return false; + } v = ir_builder_create_global(ir, self->name, vtype); if (!v) { @@ -1603,6 +1607,7 @@ static bool ast_local_codegen(ast_value *self, ir_function *func, bool param) /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */ if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) { compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count); + return false; } self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count); From b26f53125db25fc5c2dcd354c4945a9ace843c83 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Tue, 27 Aug 2013 10:19:05 +0200 Subject: [PATCH 2/2] error on uninitialized arrays of unspecified size; otherwise it errors about a size overflow --- ast.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/ast.c b/ast.c index 1d13d8b..2a8caf2 100644 --- a/ast.c +++ b/ast.c @@ -1348,6 +1348,20 @@ static bool ast_global_array_set(ast_value *self) return true; } +static bool check_array(ast_value *self, ast_value *array) +{ + if (array->expression.flags & AST_FLAG_ARRAY_INIT && !array->initlist) { + compile_error(ast_ctx(self), "array without size: %s", self->name); + return false; + } + /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */ + if (!array->expression.count || array->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) { + compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count); + return false; + } + return true; +} + bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield) { ir_value *v = NULL; @@ -1395,11 +1409,8 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield) return false; } - /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */ - if (!array->expression.count || array->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) { - compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count); + if (!check_array(self, array)) return false; - } elemtype = array->expression.next; vtype = elemtype->vtype; @@ -1465,10 +1476,8 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield) } /* same as with field arrays */ - if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) { - compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count); + if (!check_array(self, self)) return false; - } v = ir_builder_create_global(ir, self->name, vtype); if (!v) { @@ -1605,10 +1614,8 @@ static bool ast_local_codegen(ast_value *self, ir_function *func, bool param) } /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */ - if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) { - compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count); + if (!check_array(self, self)) return false; - } self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count); if (!self->ir_values) {