3 ms·
These kinds of warnings need optimizations enabled. $ gcc -O2 -Wall -c t.c t.c: In function ‘main’: t.c:3:14: warning: array subscript 20 is outsi
by fweimer 2y ago
These kinds of warnings need optimizations enabled.
$ gcc -O2 -Wall -c t.c
t.c: In function ‘main’:
t.c:3:14: warning: array subscript 20 is outside array bounds of ‘int[10]’ [-Warray-bounds]
3 | return *(x+20);
| ^~~~~~~
t.c:2:11: note: at offset 80 into object ‘x’ of size 40
2 | int x[10];
| ^
Not sure how to get Clang to warn. It clearly recognizes the undefined behavior.
- dzaima 2y agoClang appears to have -Warray-bounds-pointer-arithmetic for this, though not enabled on -Wall nor -Wextra. (fwiw, clang has -Weverything to turn on literally every warning, including conflicting ones, for finding what flags there are)
- lisper 2y agoYeah, Clang warns on x[20] but not *(x+20) even with -Wall and -O2. It's kinda weird.
- dzaima 2y agoAh, the -Warray-bounds-pointer-arithmetic warning is actually just about the pointer addition; thus even that doesn't warn on *(x+10) on the 10-element array, as the construction of the past-the-end pointer is still valid, and seemingly no warning checks bounds validity for actual dereference.