[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Tinycc-devel] Patch to correct using incomplete types in structures
From: |
Thomas Preud'homme |
Subject: |
Re: [Tinycc-devel] Patch to correct using incomplete types in structures/unions. |
Date: |
Tue, 01 Oct 2013 17:26:33 +0200 |
User-agent: |
KMail/4.10.5 (Linux/3.10-3-amd64; KDE/4.10.5; x86_64; ; ) |
Le lundi 30 septembre 2013 01:05:52 Amine Najahi a écrit :
> Hi tcc folks,
Hi Amine,
>
> I think I sent a premature version of this mail earlier. If so, please
> forgive me for it.
No problem :)
>
> In its current version tcc allows using incomplete types inside
> structures/unions.
>
> Therefore the following codes incorrectly compile in tcc:
>
>
> struct myFirstStruct; //incomplete type
>
> struct mySecondStruct{
> struct myFirstStruct field;
> };
>
> // or even the following circular structure
>
> struct mySecondStruct{
> struct mySecondStruct field;
> };
Indeed.
>
>
> Below is a tentative patch to correct this behaviour.
> Thanks and keep up the good work.
> --
> diff --git a/tccgen.c b/tccgen.c
> index 0f0aac5..af8140a 100644
> --- a/tccgen.c
> +++ b/tccgen.c
> @@ -2824,6 +2824,9 @@ static void struct_decl(CType *type, int u, int tdef)
> type_decl(&type1, &ad, &v, TYPE_DIRECT |
> TYPE_ABSTRACT);
> if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
> expect("identifier");
> + if (type_size(&type1, &align) < 0)
> + tcc_error("field '%s' has incomplete type",
> + get_tok_str(v, NULL));
> if ((type1.t & VT_BTYPE) == VT_FUNC ||
> (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN
>
> | VT_INLINE)))
>
> tcc_error("invalid type for '%s'»,
Thanks for reporting this issue and providing a fix for it. I looked at your
patch and it looked fine to me on the first look but unfortunately:
% make test
make: Dépendance circulaire Makefile <- Makefile abandonnée.
make -C tests test
make[1]: entrant dans le répertoire « /home/robotux/projects/perso/tcc/tests »
------------ hello-exe ------------
../tcc -B.. -I.. -I.. -I../include ../examples/ex1.c -o hello || (../tcc -vv;
exit 1) && ./hello
Hello World
------------ hello-run ------------
../tcc -B.. -I.. -I.. -I../include -run ../examples/ex1.c
Hello World
gcc -o libtcc_test libtcc_test.c ../libtcc.a -I.. -Wall -g -O2 -fno-strict-
aliasing -Wno-pointer-sign -Wno-sign-compare -Wno-unused-result -
DCONFIG_LDDIR="\"lib\"" -DCONFIG_MULTIARCHDIR="\"x86_64-linux-gnu\"" -
DTCC_TARGET_X86_64 -lm -ldl -I..
------------ libtest ------------
./libtcc_test lib_path=..
Hello World!
fib(32) = 2178309
add(32, 64) = 96
------------ test3 ------------
../tcc -B.. -I.. -I.. -I../include -DCONFIG_LDDIR="\"lib\"" -
DCONFIG_MULTIARCHDIR="\"x86_64-linux-gnu\"" -DTCC_TARGET_X86_64 -DONE_SOURCE -
run ../tcc.c -B.. -I.. -I.. -I../include -DCONFIG_LDDIR="\"lib\"" -
DCONFIG_MULTIARCHDIR="\"x86_64-linux-gnu\"" -DTCC_TARGET_X86_64 -DONE_SOURCE -
run ../tcc.c -B.. -I.. -I.. -I../include -DCONFIG_LDDIR="\"lib\"" -
DCONFIG_MULTIARCHDIR="\"x86_64-linux-gnu\"" -DTCC_TARGET_X86_64 -DONE_SOURCE -
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.
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.
Best regards,
Thomas
signature.asc
Description: This is a digitally signed message part.
- Re: [Tinycc-devel] Patch to correct using incomplete types in structures/unions.,
Thomas Preud'homme <=