public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/110764] New: [12/13/14 Regression] False positive -Warray-bounds warning swapping std::thread::id
Date: Fri, 21 Jul 2023 10:43:07 +0000	[thread overview]
Message-ID: <bug-110764-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 110764
           Summary: [12/13/14 Regression] False positive -Warray-bounds
                    warning swapping std::thread::id
           Product: gcc
           Version: 12.3.1
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

#include <iostream>
#include <thread>
#include <vector>

void g(int n);

void g(int n) {
    std::cout << "consume: " << n << std::endl;
}

int main(void) {
    std::vector<std::thread> threads(1);
    // The compiler error comes from the two threads below.
    threads[0] = std::thread(g, 42);
    threads[1] = std::thread(g, 42);
    threads[0].join();
    threads[1].join();
    return (0);
}

Compiled with -Warray-bounds -O2 this gives a false positive:

In file included from
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/exception_ptr.h:43,
                 from
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/exception:168,
                 from /home/jwakely/gcc/12.1.0/include/c++/12.1.0/ios:39,
                 from /home/jwakely/gcc/12.1.0/include/c++/12.1.0/ostream:38,
                 from /home/jwakely/gcc/12.1.0/include/c++/12.1.0/iostream:39,
                 from max.cc:1:
In function ‘std::_Require<std::__not_<std::__is_tuple_like<_Tp> >,
std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > std::swap(_Tp&,
_Tp&) [with _Tp = thread::id]’,
    inlined from ‘void std::thread::swap(std::thread&)’ at
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/std_thread.h:171:16,
    inlined from ‘std::thread& std::thread::operator=(std::thread&&)’ at
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/std_thread.h:165:11,
    inlined from ‘int main()’ at max.cc:15:35:
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/move.h:205:7: warning: array
subscript 1 is outside array bounds of ‘std::thread [1]’ [-Warray-bounds]
  205 |       __a = _GLIBCXX_MOVE(__b);
      |       ^~~
In file included from
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/allocator.h:46,
                 from /home/jwakely/gcc/12.1.0/include/c++/12.1.0/string:41,
                 from
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/locale_classes.h:40,
                 from
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/ios_base.h:41,
                 from /home/jwakely/gcc/12.1.0/include/c++/12.1.0/ios:42:
In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const
void*) [with _Tp = std::thread]’,
    inlined from ‘static _Tp* std::allocator_traits<std::allocator<_CharT>
>::allocate(allocator_type&, size_type) [with _Tp = std::thread]’ at
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/alloc_traits.h:464:28,
    inlined from ‘std::_Vector_base<_Tp, _Alloc>::pointer
std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp =
std::thread; _Alloc = std::allocator<std::thread>]’ at
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/stl_vector.h:378:33,
    inlined from ‘void std::_Vector_base<_Tp,
_Alloc>::_M_create_storage(std::size_t) [with _Tp = std::thread; _Alloc =
std::allocator<std::thread>]’ at
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/stl_vector.h:395:44,
    inlined from ‘std::_Vector_base<_Tp, _Alloc>::_Vector_base(std::size_t,
const allocator_type&) [with _Tp = std::thread; _Alloc =
std::allocator<std::thread>]’ at
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/stl_vector.h:332:26,
    inlined from ‘std::vector<_Tp, _Alloc>::vector(size_type, const
allocator_type&) [with _Tp = std::thread; _Alloc =
std::allocator<std::thread>]’ at
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/stl_vector.h:552:47,
    inlined from ‘int main()’ at max.cc:12:39:
/home/jwakely/gcc/12.1.0/include/c++/12.1.0/bits/new_allocator.h:137:55: note:
at offset 8 into object of size 8 allocated by ‘operator new’
  137 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n *
sizeof(_Tp)));
      |                                                       ^


This started with r12-1992

             reply	other threads:[~2023-07-21 10:43 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-21 10:43 redi at gcc dot gnu.org [this message]
2023-07-21 21:19 ` [Bug middle-end/110764] " pinskia at gcc dot gnu.org
2023-07-21 21:21 ` pinskia at gcc dot gnu.org
2023-07-21 22:07 ` maxim.yegorushkin at gmail dot com
2023-07-21 22:11 ` maxim.yegorushkin at gmail dot com
2023-07-21 22:25 ` pinskia at gcc dot gnu.org
2023-07-21 22:34 ` maxim.yegorushkin at gmail dot com
2023-07-21 22:35 ` maxim.yegorushkin at gmail dot com
2023-07-21 22:38 ` maxim.yegorushkin at gmail dot com
2023-07-21 23:08 ` redi at gcc dot gnu.org
2023-10-17 10:27 ` rguenth at gcc dot gnu.org
2024-03-07 23:28 ` law 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-110764-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).