public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "kbrabml at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/104165] New: Incorrectly identifying array bounds with -O2 -Werror=array-bounds
Date: Fri, 21 Jan 2022 15:40:24 +0000	[thread overview]
Message-ID: <bug-104165-4@http.gcc.gnu.org/bugzilla/> (raw)

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.

             reply	other threads:[~2022-01-21 15:40 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-21 15:40 kbrabml at gmail dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-104165-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).