public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106901] New: False positive -Warray-bounds with -O2 or higher?
@ 2022-09-10 17:06 carlosgalvezp at gmail dot com
  2022-09-10 17:15 ` [Bug c++/106901] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: carlosgalvezp at gmail dot com @ 2022-09-10 17:06 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106901
           Summary: False positive -Warray-bounds with -O2 or higher?
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carlosgalvezp at gmail dot com
  Target Milestone: ---

Hi!

Here's some code snippet that I believe leads to a false positive for
Warray-bounds. It happens in trunk but not on GCC 12.2.

#include <array>

bool bar(std::array<int, 5> const& vec,
         std::size_t const size,
         std::size_t const expected_size)
{
    if (size < expected_size)
    {
        return false;
    }
    for (std::size_t i{expected_size}; i < size; ++i)
    {
        if (vec[i] != 0)
        {
            return false;
        }
    }
    return true;
}

bool foo(std::array<int, 5> const& vec, std::size_t const size)
{
    return bar(vec, size, 5);
}

In file included from <source>:1:
In member function 'constexpr const std::array<_Tp, _Nm>::value_type&
std::array<_Tp, _Nm>::operator[](size_type) const [with _Tp = int; long
unsigned int _Nm = 5]',
    inlined from 'bool bar(const std::array<int, 5>&, std::size_t,
std::size_t)' at <source>:13:18,
    inlined from 'bool foo(const std::array<int, 5>&, std::size_t)' at
<source>:23:15:
/opt/compiler-explorer/gcc-trunk-20220910/include/c++/13.0.0/array:213:24:
warning: array subscript 5 is above array bounds of 'std::__array_traits<int,
5>::_Type' {aka 'const int [5]'} [-Warray-bounds]
  213 |         return _M_elems[__n];
      |                ~~~~~~~~^
/opt/compiler-explorer/gcc-trunk-20220910/include/c++/13.0.0/array: In function
'bool foo(const std::array<int, 5>&, std::size_t)':
/opt/compiler-explorer/gcc-trunk-20220910/include/c++/13.0.0/array:109:55:
note: while referencing 'std::array<int, 5>::_M_elems'
  109 |       typename __array_traits<_Tp, _Nm>::_Type        _M_elems;
      |                                                       ^~~~~~~~

Example on Compiler Explorer:
https://godbolt.org/z/dKWdKrsTa

Since "size" is unknown to the compiler, it shouldn't be possible to tell with
certainty whether the loop is actually executed and therefore out-of-bounds
invoked.

Thanks!

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

end of thread, other threads:[~2022-10-11 12:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-10 17:06 [Bug c++/106901] New: False positive -Warray-bounds with -O2 or higher? carlosgalvezp at gmail dot com
2022-09-10 17:15 ` [Bug c++/106901] " pinskia at gcc dot gnu.org
2022-09-10 17:28 ` carlosgalvezp at gmail dot com
2022-09-10 17:32 ` pinskia at gcc dot gnu.org
2022-09-10 18:27 ` carlosgalvezp at gmail dot com
2022-09-11  8:31 ` carlosgalvezp at gmail dot com
2022-09-12  7:57 ` [Bug tree-optimization/106901] [13 Regression] " rguenth at gcc dot gnu.org
2022-10-11 12:28 ` carlosgalvezp 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).