[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Tinycc-devel] Patch to correct using incomplete types in structures
From: |
Amine Najahi |
Subject: |
Re: [Tinycc-devel] Patch to correct using incomplete types in structures/unions. |
Date: |
Wed, 02 Oct 2013 11:10:04 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5 |
Hi Thomas,
run ../tcc.c -B.. -I.. -I.. -I../include -run ../tests/tcctest.c > test.out3
tcctest.c:1309: error: field 'b' has incomplete type
make[1]: *** [test3] Erreur 1
make[1]: quittant le répertoire « /home/robotux/projects/perso/tcc/tests »
make: *** [test] Erreur 2
zsh: exit 2 make test
For information, line 1309 is the line with "a1" in:
struct bar {
char *s;
int len;
} sinit17[] = {
"a1", 4,
"a2", 1
};
However, it refers to the following structure:
struct complexinit2 {
int a;
int b[];
};
This kind of construct is used to allow for an undefined size array. It's then
allocated with a malloc with the value given as parameter being bigger than
sizeof(struct complexinit2). I'm not sure though that it's a valid C99
construct.
My fault, I didn't notice the tests so didn't run them (I am new to tcc).
Yes, this is a C99 addition called flexible array member.
Here is what the c11 draft says about it:
"A structure or union shall not contain a member with incomplete or
function type (hence,
a structure shall not contain an instance of itself, but may contain a
pointer to an instance
of itself), except that the last member of a structure with more than
one named member
may have incomplete array type; such a structure (and any union
containing, possibly
recursively, a member that is such a structure) shall not be a member of
a structure or an
element of an array."
For more reading on the matter:
http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
It would be nice of you to check if it is. Anyway, it's frequent enough that
we probably want to support it. To avoid the error, check wether the field
with unknown size is of array type and is the last field of the structure.
Otherwise it's an error.
You will find the new patch attached. However, it does not handle the
" such a structure (and any union containing, possibly
recursively, a member that is such a structure) shall not be a member of
a structure or an
element of an array." part.
The right way to do this is to tag structures containing flexible arrays
to avoid them being used in
arrays and being embedded in other structures.
gcc and clang give a warning for this if you use the -pedantic switch.
Do you think it is worth implementing?
Thanks,
Amine
incomplete_type.patch
Description: Text Data