aboutsummaryrefslogtreecommitdiff
path: root/src/check_expr.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-09-17 11:01:56 +0100
committergingerBill <bill@gingerbill.org>2022-09-17 11:01:56 +0100
commit99a1a102864689bd8904f10e1a7fe0f79363399f (patch)
tree7038c8efc79d8b7299b243fc7d76d75aa17fe2f9 /src/check_expr.cpp
parent9640b493191e2ec0ff1338751a053a5ef559728d (diff)
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
Diffstat (limited to 'src/check_expr.cpp')
-rw-r--r--src/check_expr.cpp8
1 files changed, 7 insertions, 1 deletions
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);