What exactly is the -Warray-bounds option to the GCC compiler supposed to warn about?
the man page states:
Quote:
-Warray-bounds
-Warray-bounds=n
This option is only active when -ftree-vrp is active (default for -O2 and above). It warns
about subscripts to arrays that are always out of bounds. This warning is enabled by -Wall.
-Warray-bounds=1
This is the warning level of -Warray-bounds and is enabled by -Wall; higher levels are
not, and must be explicitly requested.
-Warray-bounds=2
This warning level also warns about out of bounds access for arrays at the end of a
struct and for arrays accessed through pointers. This warning level may give a larger
number of false positives and is deactivated by default.
What exactly is the -Warray-bounds option to the GCC compiler supposed to warn about?
I am not sure about how basic you need an explanation because the man page is pretty self-explanatory. So just give me some feedback if this doesn't make you get it:
Consider a "normal" variable first: a variable is of a certain data type and hence represents a certain amount of (allocated) memory somewhere in RAM. I.e. when you write a declaration like:
then the compiler sets aside a certain amount of bytes (depends on how "int" is defined, but nowadays usually 4 bytes ^= 32 bits) and you can address this 4-byte space by using the name "myvar".
Now arrays: arrays are basically lists of elements where each element is a variable like above. If you i.e. create an array of 7 elements of the type int the compiler will set aside the same 4 bytes as above - not once but seven times. These seven 4-byte-spaces will be placed one after the other. If you now adress the third element with some operation the compiler "knows" that the elements are 4-byte-blocks and therefore will translate that to byte 9-12 following the base address.
So far so good, but what would happen if you would request the eighth element, hmm? If the compiler doesn't "remember" that your element only has seven elements it would eventually give you the content of byte 29-32 - which holds some other data! What would happen if you'd requested the -2nd element? You might get data which aren't even part of the program (but maybe some OS data!).
These method of accessing array elements which don't even exist have been used historically to get access to memory parts the program would normally not have access to. Modern OSes have all sorts of security measures to make this impossible, but you can still accidentally access (and hence modify) your own program in memory for some probably unwanted effects.
To avoid this there is bounds checking: basically it is the complier making sure whatever array elements you access are being defined before.
bakunin,
Thanks for the explanation. I know what array bounds checking means. That's why I posted this question. Have you tried turning on the -Warray-bounds on GCC and accessing an illegal array position to see if it works as you described? I was expecting the compiler will give me warning for accessing an array element beyond its declared size, but it didn't give me any warnings at all. The below program runs just fine on my Linux box. I gave the gcc version on my post above. If I assign 10000 to index, program give segmentation fault, but still compiles with no warnings. -Warray-bounds switch is on.
Try at least level -O2 and it will probably catch it. Without at least that level the compiler doesn't do enough work to detect out-of-bounds but it should still detect it at run-time.
If I assign 10000 to index, program give segmentation fault, but still compiles with no warnings. -Warray-bounds switch is on.
OK, now i understand better what your question is about. For reference i modified your program this way:
and used this compiler version:
with the same results.
The problem IMHO is a misunderstanding of runtime-bounds-checking and compile-time bounds-checking. Runtime bounds-checking is included in the frontend (if at all) and this is only done for C but not for C++ according to this source. The link also mentions other options (mudflap, MIRO, valgrind, ...), which i haven't tested at all (i haven't programmed anything worthwhile in a HLL for a long time).
As far as i have understood the GNU compiler suite allows for switching on runtime bounds-checking, which the C-frontend does provide but the g++-frontend does not.
None of the frontends do compile-time bounds-checking (which you seem to be after) and, frankly, i doubt this is possible to do in a general way. The compiler would have to guess at compile-time which values any expression could evaluate to, which not only would need arbitrary computing time but also would need to decide the "Entscheidungsproblem - Wikipedia", which is proven to be undecidable (see also "halting problem in Turing-machines").
Try at least level -O2 and it will probably catch it. Without at least that level the compiler doesn't do enough work to detect out-of-bounds but it should still detect it at run-time.
Hi,
I turned on -O2 on linux fedora (g++ GCC 7.3.1 20180130 Red Hat 7.3.1-2) and also tried the same code on a FreeBSD unix (g++ FreeBSD Ports Collection 6.4.0) and they both didn't catch the array out-of-bounds when both -O2 and -Warray-bounds=2 are enabled.
It might be a simple one but I have this question bothering me for sometime.
When we do a symbol search inside the library directory (i.e. /usr/lib/*) via tools like nm; it takes a while to give us the results. However, its very quick when gcc is invoked to compile a program with the very same... (11 Replies)
hi,
can we install gcc compiler in unix based OS(sun solar,IBM AIX,HP,etc) and also
can we install sun cc compiler in AIX environment and vice versa.
and more ..is linux support cc compiler
regards
Ajay (3 Replies)
Which gcc compiler release had the Arm 9 multicore support?Whether the compiler that used for the single Arm 9 core can be used for its multicore systems ?
If gcc not support,please tell me which are the compilers that are available for Arm 9 multicore systems (including commerical).Whether... (0 Replies)
I am using open suse linux. I want to install gcc compiler in my machine. I ahve checked man gcc and man cc. But it's not there. Can someone help me (4 Replies)
where to download gcc compiler for ubuntu?
how to install?
how to build and run "c programs"?
screen shots if possible.....:b::D
tutorials too:cool: (5 Replies)
Hi there
I've got a problem getting my mysql libraries to work.
every time I compile my source code it gives my a compiler error.
Cannot find a rule to create target /usr/include/mysql/mysql.h
AND
/usr/include/mysql/mysql.h: Permission denied
Is anyone fimiliar with this error, and can... (1 Reply)
Hi,
we are converting from IBM-AIX(xl c/c++ compiler) to Linux(GCC complier).
As a part of this i need to change the CFLAGS.
The xl c/c++ complier CFLAGS is
CFLAGS := $(CDEBUG) $(PROJECT_INCLUDE_DIRS) $(COBJECT_MODE) -qcpluscmt -qmakedep -qcheck=all \
-qalign=bit_packed $(LINT_FLAGS)... (0 Replies)
i write c++ code it run perfectely with g++ compiler but same code when i compile with
GCC compiler it gives linker error , followed these linker error
/tmp/ccfZtXOQ.o(.text+0x22): In function `main':
conf_system.cpp: undefined reference to `operator new(unsigned int)'... (5 Replies)
:confused: I have a question concerning gcc. IŽd like to install the gcc on my Mac OS X, but when I try to run the configure command I get the following message:
floriant% ./configure
./configure: read-only variable: PWD
Configuring for a powerpc-apple-darwin5.4 host.
*** This configuration... (2 Replies)