From 99a1a102864689bd8904f10e1a7fe0f79363399f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 17 Sep 2022 11:01:56 +0100 Subject: [PATCH] Fixed #2044 Uninitialised constant struct member values can cause crash Foo :: struct { x: i32, data: sa.Small_Array(10, i32), } defaultFoo :: Foo{ x = 1, // The 'data' value is not set! } fmt.println(defaultFoo.data) // caused the bug --- src/check_expr.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 38d17c131..891a9ebcb 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -4022,6 +4022,7 @@ ExactValue get_constant_field_single(CheckerContext *c, ExactValue value, i32 in if (cl->elems[0]->kind == Ast_FieldValue) { if (is_type_struct(node->tav.type)) { + bool found = false; for_array(i, cl->elems) { Ast *elem = cl->elems[i]; if (elem->kind != Ast_FieldValue) { @@ -4033,9 +4034,14 @@ ExactValue get_constant_field_single(CheckerContext *c, ExactValue value, i32 in defer (array_free(&sub_sel.index)); if (sub_sel.index[0] == index) { value = fv->value->tav.value; + found = true; break; } } + if (!found) { + // Use the zero value if it is not found + value = {}; + } } else if (is_type_array(node->tav.type) || is_type_enumerated_array(node->tav.type)) { for_array(i, cl->elems) { Ast *elem = cl->elems[i]; @@ -4677,7 +4683,7 @@ Entity *check_selector(CheckerContext *c, Operand *operand, Ast *node, Type *typ switch (entity->kind) { case Entity_Constant: - operand->value = entity->Constant.value; + operand->value = entity->Constant.value; operand->mode = Addressing_Constant; if (operand->value.kind == ExactValue_Procedure) { Entity *proc = strip_entity_wrapping(operand->value.value_procedure);