public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cuzdav at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/110620] spurious array-bounds
Date: Mon, 10 Jul 2023 19:24:45 +0000	[thread overview]
Message-ID: <bug-110620-4-DJ07BjV58K@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-110620-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #1 from Chris Uzdavinis <cuzdav at gmail dot com> ---
Starting with Gcc12.1 (at least on x86) and through all versions up to the
trunk (post 13.1)
This warning hits with optimization -O2 or higher.

https://godbolt.org/z/q3T39Wf8c


    #include <vector>

    void foo(std::vector<std::pair<int,int>>& vec) {
        if (vec.size() > 5) {
            vec[3].first = 0;
        }
    }

    int main() {
        auto v1 = std::vector{std::pair(1, 2)};
        foo(v1);
    }

What is interesting to me is that the analyzer is missing the fact that size is
explicitly tested, and the index is a constant expression that is lower.

There are a lot of array-bounds bugs reported, and all have similarities and
I'm not sure if this is a dupe or not, though I went through the open ones in 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56456
And did not see an example quite like this.


In function 'void foo(std::vector<std::pair<int, int> >&)',
    inlined from 'int main()' at <source>:11:8:
<source>:5:22: error: array subscript 3 is outside array bounds of
'std::pair<int, int> [1]' [-Werror=array-bounds=]
    5 |         vec[3].first = 0;
In file included from
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/x86_64-linux-gnu/bits/c++allocator.h:33,
                 from
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/allocator.h:46,
                 from
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/vector:63,
                 from <source>:1:
In member function '_Tp* std::__new_allocator<_Tp>::allocate(size_type, const
void*) [with _Tp = std::pair<int, int>]',
    inlined from 'constexpr _Tp* std::allocator< <template-parameter-1-1>
>::allocate(std::size_t) [with _Tp = std::pair<int, int>]' at
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/allocator.h:198:40,
    inlined from 'static constexpr _Tp*
std::allocator_traits<std::allocator<_Up> >::allocate(allocator_type&,
size_type) [with _Tp = std::pair<int, int>]' at
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/alloc_traits.h:482:28,
    inlined from 'constexpr std::_Vector_base<_Tp, _Alloc>::pointer
std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp =
std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >]' at
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/stl_vector.h:378:33,
    inlined from 'constexpr void std::vector<_Tp,
_Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator,
std::forward_iterator_tag) [with _ForwardIterator = const std::pair<int, int>*;
_Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >]' at
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/stl_vector.h:1687:25,
    inlined from 'constexpr std::vector<_Tp,
_Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp =
std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >]' at
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/stl_vector.h:677:21,
    inlined from 'int main()' at <source>:10:42:
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/new_allocator.h:147:55:
note: at offset 24 into object of size 8 allocated by 'operator new'
  147 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n *
sizeof(_Tp)));
      |                                                       ^
cc1plus: all warnings being treated as errors
Compiler returned: 1

  reply	other threads:[~2023-07-10 19:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-10 19:16 [Bug c++/110620] New: " cuzdav at gmail dot com
2023-07-10 19:24 ` cuzdav at gmail dot com [this message]
2023-07-10 19:27 ` [Bug tree-optimization/110620] " cuzdav at gmail dot com
2023-07-10 19:28 ` cuzdav at gmail dot com
2023-07-10 19:30 ` cuzdav at gmail dot com
2023-07-10 19:39 ` pinskia at gcc dot gnu.org
2023-07-11  8:01 ` rguenth at gcc dot gnu.org

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-110620-4-DJ07BjV58K@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).