https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79950

            Bug ID: 79950
           Summary: G++ cannot detect simple off by one error in STL
                    classes
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dcb314 at hotmail dot com
  Target Milestone: ---

Given the following C++ source code

# include <vector>

using namespace std;

extern void g( int);

void f1( const vector < int > & v)
{
        for (size_t i = 0; i <= v.size(); ++i)
        {
                g( v[i]);
        }
}

int a[ 10];

void f2()
{
        for (int i = 0; i <= 10; ++i)
        {
                g( a[i]);
        }
}

then current trunk gcc can detect a problem in f2, but not f1.

$ ~/gcc/results/bin/g++ -g -O2 -D_FORTIFY_SOURCE=2 -Wall -Wextra -c mar7a.cc
mar7a.cc: In function ‘void f2()’:
mar7a.cc:22:4: warning: iteration 10 invokes undefined behavior
[-Waggressive-loop-optimizations]
   g( a[i]);
   ~^~~~~~~
mar7a.cc:20:20: note: within this loop
  for (int i = 0; i <= 10; ++i)
                  ~~^~~~~
$

Reply via email to