scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 33787455afaea0e22d89176ede170941766a9bd4
parent ea6d8cf4e6d83ca32ce98443dc939ec897daf6b7
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Mon, 11 May 2026 20:55:40 +0200

cc1: Check type of array designator index

The expression between [ ] must an integer constant expression,
and the function iconstexpr() returns NULL in that case and
it has to be checked to give the proper error message.

Diffstat:
Msrc/cmd/scc-cc/cc1/init.c | 11+++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/cmd/scc-cc/cc1/init.c b/src/cmd/scc-cc/cc1/init.c @@ -31,14 +31,21 @@ int disstring; static long long arydesig(Type *tp, Init *ip) { + long long npos; Node *np; if (tp->op != ARY) errorp("array index in non-array initializer"); + next(); - np = iconstexpr(); - npos = np->sym->u.i; + if ((np = iconstexpr()) == NULL) { + errorp("array index in initializer not of integer type"); + npos = 0; + } else { + npos = np->sym->u.i; + } + if (npos < 0 || (tp->prop & TDEFINED) && npos >= tp->n.elem) { errorp("array index in initializer exceeds array bounds"); npos = 0;