From 9744fca2417aa737bc5ac0bfcd5dad5700cd508b Mon Sep 17 00:00:00 2001 From: Zeex Date: Fri, 4 Apr 2014 20:46:04 +0700 Subject: [PATCH] Fix trailing comma breaking array init vectors This patch fixes a bug in commit d23e03e "Allow trailing comma in array initializers" where initarray() did an extra loop iteration hoping that initvector() would fail nicely and ultimately inserted an extra cell into the literal queue. --------- test code -------- native printf(const format[], ...); new const a[][] = { {"hello"}, {"world"} }; main() { printf("%s", a[0]); // hello printf("%d", sizeof(a)); // 2 printf("%d", sizeof(a[])); // 6 printf("%d", sizeof(a[][])); // 1 } ----- end of test code ----- --- source/compiler/sc1.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 080f296..590b7e6 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -2472,6 +2472,11 @@ static cell initarray(int ident,int tag,int dim[],int numdim,int cur, totalsize=0; needtoken('{'); for (idx=0,abortparse=FALSE; !abortparse; idx++) { + /* check if there was a trailing comma */ + if (matchtoken('}')) { + lexpush(); + break; + } /* In case the major dimension is zero, we need to store the offset * to the newly detected sub-array into the indirection table; i.e. * this table needs to be expanded and updated. @@ -2493,19 +2498,6 @@ static cell initarray(int ident,int tag,int dim[],int numdim,int cur, */ append_constval(lastdim,itoh(idx),dsize,0); } /* if */ - /* In a declaration like: - * new a[2][2] = { - * {1, 2}, - * {3, 4}, - * } - * the final trailing comma (after the "4}") makes this loop attempt to - * parse one more initialization vector, but initvector() (and a recursive - * call to initarray()) quits with a size of zero while not setting - * "errorfound". Then, we must exit this loop without incrementing the - * dimension count. - */ - if (dsize==0 && !*errorfound) - break; if (dim[cur]!=0 && idx>=dim[cur]) { assert(dsize>0 || *errorfound); error(18); /* initialization data exceeds array size */