llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Segmentation fault in default Optimization and O1, but works well in O2 ~ Os

Open haoxintu opened this issue 6 years ago • 2 comments

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.

haoxintu avatar Mar 05 '20 16:03 haoxintu

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?

haoxintu avatar Apr 07 '20 02:04 haoxintu