I am trying to find border problems in my code using the -fsanitize=bounds option, but have -fsanitize=bounds into strange behavior:
For example, in the following code:
#include <cstdlib>
compiled with parameters: $ g++ -std=c++11 -fsanitize=bounds -O0 main.cpp -o main .
If I try to access an element with an index greater than 1, errors will be reported: /usr/include/c++/5/array:53:36: runtime error: index 2 out of bounds for type 'char [1]' .
But if I try to access the element with index 1 - everything is OK and error messages are not reported.
Is behavior expected, and maybe I missed something?
An example is tested on:
- $ g++ --version g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609 ;
- $ g++ --version g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005 .
UPD
I tried -fsanitize=bounds-stric in GCC 6 and got the same.
source share