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 <u.h>
#include <libc.h>
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);
}