Segmentation fault in default Optimization and O1, but works well in O2 ~ Os
| Bugzilla Link | 45122 |
| Resolution | INVALID |
| Resolved on | Jun 15, 2020 21:10 |
| Version | 6.0 |
| OS | Linux |
| CC | @DougGregor,@zygoloid |
Extended Description
Hi, this code test.c
#include <stdio.h> #include <string.h>
void foo(int* a ) { a[-1]=1; } int main (int argc, char* argv[]) { int size= 10; printf("%d\n", size); int array[size]; memset(array,0,size*sizeof(int)); foo(array); return 0; }
get "Segmentation fault" when I use clang-6.0 test.c to compiler and then ./a.out to execute (-O1 is also fault, in ubuntu 16.04).
I know that the index of an array is -1 and not suitable, but when I add -O2, -O3, or -Os to compile it, then the fault is gone. I also test clang-3.8, and it has the same results.
This is not a bug. a[-1] is an out-of-bounds array indexing, so has undefined behavior.
Hi, is this a bug or not?