-Warray-bounds option to GCC compiler


 
Thread Tools Search this Thread
Top Forums Programming -Warray-bounds option to GCC compiler
# 1  
Old 02-13-2018
-Warray-bounds option to GCC compiler

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.
Code:
~ g++ --version
g++ (GCC) 7.3.1 20180130 (Red Hat 7.3.1-2)
Copyright (C) 2017 Free Software Foundation, Inc.

Thank you.
# 2  
Old 02-14-2018
Quote:
Originally Posted by milhan
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:

Code:
int myvar

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.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 02-14-2018
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.
Code:
int main()
{
	const int index=1000;
	int numbers[8] = {1,2,3,4,5,6,7,8};

	numbers[index] = 999;
	cout << numbers[index] << endl;

	return 0;
}

# 4  
Old 02-14-2018
What level of optimization are you using?

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.

Last edited by hicksd8; 02-15-2018 at 09:15 AM..
# 5  
Old 02-14-2018
Quote:
Originally Posted by milhan
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:

Code:
#include <iostream>
using namespace std;

int main()
{
        const int index=1000;
        int numbers[8] = {1,2,3,4,5,6,7,8};

        numbers[index] = 999;
        cout << numbers[index] << endl;

        return 0;
}

and used this compiler version:

Code:
# g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.6) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.

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").

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 6  
Old 02-20-2018
Quote:
Originally Posted by hicksd8
What level of optimization are you using?

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.
Code:
~ alias g++                                                                                                                                                              
alias g++='g++ -O2 -Wall -Wextra -Wchkp -Wmissing-include-dirs -Wswitch-default -Wunused \
  -Wduplicated-branches -Wduplicated-cond -Wshadow -Wpointer-arith -Wundef -Wunused-macros \
  -Wcast-qual -Wzero-as-null-pointer-constant -Wparentheses -Wuseless-cast -Wsign-conversion \
  -Wlogical-op -Wredundant-decls -Wrestrict -Winvalid-pch -Warray-bounds=2'
~ g++ t2.cpp && a.out 
999

# 7  
Old 02-20-2018
I'm disappointed at that. Have you tried -O3?

Have you seen this:
c - Why do compilers not warn about out-of-bounds static array indices? - Stack Overflow
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

A question regarding the gcc compiler ...

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)
Discussion started by: Praveen_218
11 Replies

2. UNIX for Dummies Questions & Answers

cc compiler and gcc compiler

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)
Discussion started by: ajaysahoo
3 Replies

3. Programming

gcc compiler

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)
Discussion started by: sujith4u87
0 Replies

4. Shell Programming and Scripting

gcc compiler

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)
Discussion started by: pritish.sas
4 Replies

5. Ubuntu

gcc compiler

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)
Discussion started by: villanarun
5 Replies

6. AIX

AIX 5.3 gcc compiler

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)
Discussion started by: cipher#1
1 Replies

7. UNIX for Dummies Questions & Answers

xl C/C++ compiler to GCC compiler

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)
Discussion started by: pbattu1
0 Replies

8. Programming

gcc compiler

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)
Discussion started by: munnu
5 Replies

9. UNIX for Dummies Questions & Answers

cc compiler / gcc

: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)
Discussion started by: florian.turck
2 Replies
Login or Register to Ask a Question