commit 5bf9f36c0f236dd5429beb16c1c2b73cf939a4d4
parent 999096d35f452f1594c10b4b2822d6c3e7f08813
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 2 Jan 2023 17:13:15 +0100
cc1: Avoid index invalid field array
When the field in the initializer is not valid we should
not try to use the array fields[] because it is likely
that it will drive to a segfault.
Diffstat:
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/cmd/cc/cc1/init.c b/src/cmd/cc/cc1/init.c
@@ -61,7 +61,7 @@ fielddesig(Type *tp, Init *ip)
if ((sym->flags & SDECLARED) == 0) {
errorp("unknown field '%s' specified in initializer",
sym->name);
- return 0;
+ return -1;
}
for (p = tp->p.fields; *p != sym; ++p)
;
@@ -233,7 +233,7 @@ initlist_helper(Type *tp)
goto desig_list;
case '.':
in.pos = fielddesig(tp, &in);
- if (in.pos < nelem)
+ if (in.pos >= 0 && in.pos < nelem)
curtp = tp->p.fields[in.pos]->type;
desig_list:
if (yytoken == '[' || yytoken == '.') {