Hi.
The following (attached) bad code compiles and
produces an obviously bad results:
---
#define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
struct x {
unsigned char b;
struct e c[0];
};
unsigned char r(unsigned char n)
{
return offsetof(struct x, c[n]);
}
unsigned char main()
{
return r(5);
}
---
$ sdcc --std-sdcc99 siz.c
[ no error ]
$ gcc siz.c
siz.c:5: error: array type has incomplete element type
test-case
Logged In: YES
user_id=888171
Stas,
Can you please explain what you think is wrong here?
SDCC generates no error because in C99 it is allowed to
create a struct ending in an array of zero or unspecified
length. This is called a "flexible array member". SDCC
will generate a warning when --std-sdcc89 or --std-c89 is
used.
Maarten
Logged In: YES
user_id=501371
The wrong thing is that the definition of "struct e"
is missing, but sdcc haven't noticed that.
You can't compel gcc to accept that code either - that's
important, I trust gcc. :)
Logged In: YES
user_id=965944
A reference cannot be generated to c[n] of a struct x
variable if sizeof struct e is unknown, which is true of the
example.
C99 allows the last element of a struct to be an "incomplete
array type", but it cannot be an incomplete array of an
incomplete structure type.
The code is a violation of the constraint in section 6.7.5.2
Array declarators: "The element type shall not be an
incomplete or function type." and must be diagnosed.
It is a compiler bug.
Logged In: YES
user_id=1610032
Don't mess around community with these academic (and
useless) problems.
Some people use SDCC to manage professional products....
Logged In: YES
user_id=501371
> Don't mess around community with these academic (and
> useless) problems.
> Some people use SDCC to manage professional products....
If you think that for your "prefessional products" it is
OK for the compiler to ignore an error and produce the
wrong code, then I don't think there is something to
discuss with you.
FYI, almost every sdcc user use it in an embedded
production and sometimes even the perople's life may
depend on the quality of the code.
This problem looks similar to #1474589.
Philipp
Fixed in SDCC 2.9.7 #5915.
It's not related to #1474589.