Like the title suggest I am wondering if there is a way to turn on the GCC flag
-Waggressive-loop-optimizations
when the optimizations level is -O0
or without using -OX
at all.
Lets see the following example:
#include <stdio.h>
int main ( void ){
int arr[4] = { 0 };
for ( int i = 0 ; i < 5 ; i++ ){
arr[i] = i+1;
}
for ( int j = 0; j < 4 ; j++ ){
printf("Arr[%d] = %d\n", j, arr[j] );
}
}
as you can see, when i == 4
here:
for ( int i = 0 ; i < 5 ; i++ )
the program reads the Array outside of its bounds.
With the following GCC flags -O1
to -O3
The compiler informs me about this:
INPUT:
gcc-7 -Wpedantic -std=c11 -Wall -Wextra -Werror -O1
Output:
program.c: In function ‘main’:
program.c:6:16: error: iteration 4 invokes undefined behavior [-Werror=aggressive-loop-optimizations]
arr[i] = i+1;
~~~~~~~^~~~~
program.c:5:5: note: within this loop
for ( int i = 0 ; i < 5 ; i++ ){
^~~
cc1: all warnings being treated as errors
But of course it does not with -O0
.
I was trying the following:
gcc-7 -Wpedantic -std=c11 -Wall -Wextra -Werror -Waggressive-loop-optimizations -O0 program.c -o program
and even the following (without -O0
:
gcc-7 -Wpedantic -std=c11 -Wall -Wextra -Werror -Waggressive-loop-optimizations program.c -o program
And the compiler just ignores this flag:
-Waggressive-loop-optimizations
Does anyone know why it is happening that and if there is a possible way to turn this flag ON ?:
-Waggressive-loop-optimizations
EDIT:
I need this because for example when I compile the above program with:
gcc-7 -Wpedantic -std=c11 -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes -Wmisleading-indentation -Wduplicated-cond -Wold-style-definition -Wconversion -Wshadow -Winit-self -Wfloat-equal -Wwrite-strings -Wpointer-compare -Waggressive-loop-optimizations -g program.c -o program
And then I do:
valgrind --leak-check=full --track-origins=yes ./program
valgrind does not report something, because I think that the compiler Optimize that away ( I think):
michi@michael ~/Compile $ valgrind --leak-check=full --track-origins=yes ./program
==5818== Memcheck, a memory error detector
==5818== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==5818== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==5818== Command: ./program
==5818==
Arr[0] = 1
Arr[1] = 2
Arr[2] = 3
Arr[3] = 4
==5818==
==5818== HEAP SUMMARY:
==5818== in use at exit: 0 bytes in 0 blocks
==5818== total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==5818==
==5818== All heap blocks were freed -- no leaks are possible
==5818==
==5818== For counts of detected and suppressed errors, rerun with: -v
==5818== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Any way in the comments, there is some misunderstanding about the edit part and the Question itself.
No, I do not need a valgrind
fix here, the edit is just to explain one of the situations where there is no catch of accessing an Array out of its bounds because the flag -Waggressive-loop-optimizations
does not Work like when you use the optimizations level -O1
, -O2
or -O3
.