public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104165] New: Incorrectly identifying array bounds with -O2 -Werror=array-bounds
@ 2022-01-21 15:40 kbrabml at gmail dot com
  2022-01-21 17:04 ` [Bug tree-optimization/104165] [12 Regression] " pinskia at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: kbrabml at gmail dot com @ 2022-01-21 15:40 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104165
           Summary: Incorrectly identifying array bounds with -O2
                    -Werror=array-bounds
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kbrabml at gmail dot com
  Target Milestone: ---

In file included from /home/install/include/c++/12.0.1/algorithm:61,
                 from repro.cpp:1:
In function ‘void std::__final_insertion_sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator = int*; _Compare =
__gnu_cxx::__ops::_Iter_comp_iter<bar(int, int)::<lambda(int, int)> >]’,
    inlined from ‘void std::__sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator = int*; _Compare =
__gnu_cxx::__ops::_Iter_comp_iter<bar(int, int)::<lambda(int, int)> >]’ at
/home/install/include/c++/12.0.1/bits/stl_algo.h:1940:31,
    inlined from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter =
int*; _Compare = bar(int, int)::<lambda(int, int)>]’ at
/home/install/include/c++/12.0.1/bits/stl_algo.h:4853:18,
    inlined from ‘int bar(int, int)’ at repro.cpp:17:14,
    inlined from ‘int foo(int)’ at repro.cpp:25:13:
/home/install/include/c++/12.0.1/bits/stl_algo.h:1849:32: error: array
subscript 16 is outside array bounds of ‘unsigned char [16]’
[-Werror=array-bounds]
 1849 |           std::__insertion_sort(__first, __first + int(_S_threshold),
__comp);
      |          
~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In function ‘int bar(int, int)’,
    inlined from ‘int foo(int)’ at repro.cpp:25:13:
repro.cpp:4:7: note: at offset 64 into object ‘f.140’ of size 16
    4 |   int f[l];
      |       ^
cc1plus: all warnings being treated as errors
make: *** [Makefile:5: all] Error 1

Getting the above bogus warning when compiling following reproducer

#include <algorithm>

static int bar(int n, int l) { // make function non-static and warning goes
away
        int f[l];
        int x = 0;
        int r = n;

        for (; x < l;) {
                if (r) {
                        x = l;
                } else { // Take out this else and the warning goes away
                        r = 1;
                }
        }

        if (r == 1) { // Take out this branch and the warning goes away
                std::sort(f, f + x,
                                [](int a, int b) { return a > b; });
        }
        return 1;

}

int foo(int n) {
        return bar(n, 4);
}


Compiled with HEAD of GCC (as of 20/01/2021)
g++ -c -march=armv8-a -Werror -Wall -O2  repro.cpp

The compiler sees that the size of the array is 4 ints (16-bytes), but doesn't
see that x <= 4, so that no array out of bounds should occur. Workaround is to
use malloc for the array.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-09-27 12:36 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-21 15:40 [Bug c++/104165] New: Incorrectly identifying array bounds with -O2 -Werror=array-bounds kbrabml at gmail dot com
2022-01-21 17:04 ` [Bug tree-optimization/104165] [12 Regression] " pinskia at gcc dot gnu.org
2022-01-21 17:16 ` [Bug tree-optimization/104165] [12 Regression] -Warray-bounds for unreachable code inlined from std::sort() msebor at gcc dot gnu.org
2022-02-08 21:07 ` msebor at gcc dot gnu.org
2022-05-06  8:32 ` [Bug tree-optimization/104165] [12/13 " jakub at gcc dot gnu.org
2022-07-26 13:03 ` rguenth at gcc dot gnu.org
2022-12-05 15:39 ` [Bug tree-optimization/104165] [12 " rguenth at gcc dot gnu.org
2022-12-05 16:05 ` jakub at gcc dot gnu.org
2022-12-06  7:24 ` cvs-commit at gcc dot gnu.org
2022-12-06  7:29 ` rguenth at gcc dot gnu.org
2023-05-08 12:23 ` rguenth at gcc dot gnu.org
2023-09-27 12:36 ` fchelnokov at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).