From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: From: erik quanstrom Date: Tue, 15 Sep 2009 18:14:29 -0400 To: 9fans@9fans.net MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Subject: [9fans] c compiler size bug fix Topicbox-Message-UUID: 6ec6722a-ead5-11e9-9d60-3106f5b1d025 unfortunately i let this one fester until it cost us some time. this problem has cropped up again http://9fans.net/archive/2009/07/305 (it appears that my worry about expandable arrays was unfounded.) the problem is that the size of an undefined struct as the last element of a struct is not counted because it is not known at the time. rather than diagnosing an incomplete reference, the program compiles and prints an unespected size, "sizeof A = 4 4". i don't believe it should compile at all. the fix is simply this: /n/dump/2009/0915/sys/src/cmd/cc/dcl.c:541,547 - dcl.c:541,547 l->offset = o; } else { if(l->width <= 0) - if(l->down != T) + if(l->down != T || l->width < 0) if(l->sym) diag(Z, "incomplete structure element: %s", l->sym->name); thanks to bwc for identifying the problem (again). patch submitted: /n/sources/patch/incmpltdiag - erik --- #include #include typedef struct A A; typedef struct B B; struct A { int a; B; }; struct B { uchar buf[512]; }; void main(void) { A a; B b; print("sizeof A = %d %d\n", sizeof(A), sizeof a); print("sizeof B = %d %d\n", sizeof(B), sizeof b); }