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

            Bug ID: 124207
           Summary: Wrong warning: array subscript is outside array bounds
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gjl at gcc dot gnu.org
  Target Milestone: ---

Not sure if this is a dup of PR111750, PR105523, PR109215 or similar. So here
we go:

#include <array>
#include <algorithm>

#define NUM (1 << 3)
std::array<int, NUM> vec;

int fun (int num)
{
    std::sort (vec.begin(), vec.begin() + (1 << num),
               [] (int a, int b) -> bool
               {
                   return 2 * a < b;
               });
    return vec[0];
}

int main ()
{
    fun (3);
}

$ g++ x.cpp -Wall -O3 -S

In file included from
/home/gjl/gnu/install/gcc-host/include/c++/16.0.1/algorithm:63,
                 from warray.cpp:2:
In function ‘constexpr void std::__move_median_to_first(_Iterator, _Iterator,
_Iterator, _Iterator, _Compare) [with _Iterator = int*; _Compare =
fun(int)::<lambda(int, int)>]’,
    inlined from ‘constexpr _RandomAccessIterator
std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator,
_Compare) [with _RandomAccessIterator = int*; _Compare = fun(int)::<lambda(int,
int)>]’ at
/home/gjl/gnu/install/gcc-host/include/c++/16.0.1/bits/stl_algo.h:1863:34,
    inlined from ‘constexpr void std::__introsort_loop(_RandomAccessIterator,
_RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = int*;
_Size = long int; _Compare = fun(int)::<lambda(int, int)>]’ at
/home/gjl/gnu/install/gcc-host/include/c++/16.0.1/bits/stl_algo.h:1897:38,
    inlined from ‘constexpr void std::__sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator = int*; _Compare =
fun(int)::<lambda(int, int)>]’ at
/home/gjl/gnu/install/gcc-host/include/c++/16.0.1/bits/stl_algo.h:1913:25,
    inlined from ‘constexpr void std::sort(_RAIter, _RAIter, _Compare) [with
_RAIter = int*; _Compare = fun(int)::<lambda(int, int)>]’ at
/home/gjl/gnu/install/gcc-host/include/c++/16.0.1/bits/stl_algo.h:4860:18,
    inlined from ‘int fun(int)’ at warray.cpp:9:15:
/home/gjl/gnu/install/gcc-host/include/c++/16.0.1/bits/stl_algo.h:91:17:
warning: array subscript 8 is outside array bounds of ‘std::array<int, 8> [1]’
[-Warray-bounds=]
   91 |       if (__comp(*__a, *__b))
      |           ~~~~~~^~~~~~~~~~~~
warray.cpp: In function ‘int fun(int)’:
warray.cpp:5:22: note: at offset 32 into object ‘vec’ of size 32
    5 | std::array<int, NUM> vec;
      |                      ^~~

And more similar warnings.

$ g++ -v
Using built-in specs.
COLLECT_GCC=/home/gjl/gnu/install/gcc-host/bin/g++
COLLECT_LTO_WRAPPER=/home/gjl/gnu/install/gcc-host/libexec/gcc/x86_64-pc-linux-gnu/16.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../../source/gcc-master/configure --enable-languages=c,c++
--disable-bootstrap --prefix=/home/gjl/gnu/install/gcc-host
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.0.1 20260213 (experimental) (GCC) 

I also see this on v14 at least.

Reply via email to