[Bug c++/83239] New: False positive from -Wstringop-overflow on simple std::vector code

TonyELewis at hotmail dot com gcc-bugzilla@gcc.gnu.org
Fri Dec 1 11:01:00 GMT 2017


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

            Bug ID: 83239
           Summary: False positive from -Wstringop-overflow on simple
                    std::vector code
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: TonyELewis at hotmail dot com
  Target Milestone: ---

Created attachment 42765
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=42765&action=edit
Pre-processed (-save-temps) on GCC 7.2.0 [Ubuntu 17.10]

Compiling this:


#include <vector>

void fn() {
  std::vector<int> a;

  int num = 2;
  while ( num > 0 ) {
    const auto a_size = a.size();
    if ( a_size < 3 ) {
      a.assign( 1, 0 );
    }
    else {
      a.resize( a_size - 2 ); // <-- I think problem is here
    }
    --num;
  }
}

...with `g++ -O3 -Wall -Werror a.cpp` results in:


In function ‘void fn()’:
cc1plus: error: ‘void* __builtin_memset(void*, int, long unsigned int)’:
specified size 18446744073709551608 exceeds maximum object size
9223372036854775807 [-Werror=stringop-overflow=]
cc1plus: all warnings being treated as errors


I think this is a problem for three reasons:
 1. the warning doesn't tell me the location of the problem
 2. worse, the warning name "stringop-overflow" is actively misleading because
the code containing the problem isn't using strings
 3. the warning is wrong: AFAIU, it's complaining about `a_size - 2`
potentially being a huge unsigned integer due to wrapping below 0 but it's in
an else clause that only executes if `a_size >= 3`.

I'm seeing this problem on both GCC 8.0.0 20171130 (Godbolt) and GCC 7.2.0 (my
Ubuntu).


Though there are other open bugs relating to this warning:

 * bug 79929
 * bug 82076
 * bug 82103
 * bug 82646

...I'm not sure any cover this issue (eg the first one is about Fortran).

Thanks very much.


More information about the Gcc-bugs mailing list