Closed stefanos82 closed 1 year ago
From what I understand, chibicc assumes that the array myarr[SIZE]
was already created because of the extern
and is happy to assign values in the line int myarr[] = { 11, 34, };
. In reality, the definition of myarr
was actually done during initialization and so is actually myarr[2]
, hence the remaining values are garbage. After a quick check, I saw that there is no boundary checking yet, so that is why we can read outside the array.
If instead of
extern int myarr[SIZE];
int myarr[] = { 11, 34, };
you write
int myarr[SIZE];
chibicc successfully zero-initializes the array and outputs:
0: 0
1: 0
2: 0
3: 0
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0
Although I'm not certain, I think that chibicc also doesn't check for redeclarations, because running
int myarr[SIZE];
int myarr[] = {11, 34};
again doesn't give the desired result.
Just a minor edit, I haven't been able to look at the code responsible for this yet, but I suspect that this is not related to extern
, but rather that chibicc indeed doesn't have re-declaration checks and that the second statement just 'overwrites' the first one.
The statements
int myarr[SIZE];
int myarr[] = {11, 34};
compile to garbage, but
int myarr[SIZE] = {11, 34};
does indeed give the desired output, so this is possibly not because of a not implemented feature but just a lack of re-declaration checks.
The reason I reported this is because I saw this bug in TinyCC and decided to give it a try in chibicc
; I replicated the exact tcc
buggy behavior but of course with different output results.
Maybe @rui314 plans to fix this at the cleanup procedure when the majority of compiler is nearly complete?
The following code
should produce the following output:
Instead of the above, I get the following with
chibicc
:I guess this feature has not being implemented yet?