public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "Hannes Mühleisen" <hannes@duckdblabs.com>
To: gcc-bugs@gcc.gnu.org
Cc: Tom Ebergen <tom@duckdblabs.com>
Subject: C++ undefined behaviour sanitiser bug when compiled with optimization
Date: Fri, 1 Sep 2023 10:02:12 +0200	[thread overview]
Message-ID: <CAMf67Cv6+DA2fa1M67+33gzOUZMi-qBdsxSWfwci9MtB6mM4wQ@mail.gmail.com> (raw)

Hello,

we ran into an issue with the following (rather benign) C++ snippet:

#include <atomic>
#include <mutex>

struct Foo {
  void Bar() ;

  std::mutex some_lock;
  std::atomic<uint8_t> some_number;
};

void Foo::Bar() {
        some_lock.lock();
        some_number++;
        some_lock.unlock();
}

When compiling this with a recent GCC on Linux (!) with the following flags

g++ -fsanitize=undefined  -O2 -Werror -c test.cpp -o test.o

The following error is thrown:

In member function ‘std::__atomic_base<_IntTp>::__int_type
std::__atomic_base<_IntTp>::fetch_add(__int_type, std::memory_order)
[with _ITp = unsigned char]’,
    inlined from ‘std::__atomic_base<_IntTp>::__int_type
std::__atomic_base<_IntTp>::operator++(int) [with _ITp = unsigned
char]’ at /usr/include/c++/12/bits/atomic_base.h:369:25,
    inlined from ‘void Foo::Bar()’ at test.cpp:13:13:
/usr/include/c++/12/bits/atomic_base.h:618:34: error: ‘unsigned char
__atomic_fetch_add_1(volatile void*, unsigned char, int)’ writing 1
byte into a region of size 0 overflows the destination
[-Werror=stringop-overflow=]
  618 |       { return __atomic_fetch_add(&_M_i, __i, int(__m)); }
      |                ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~

Interestingly, the error disappears with -O0 or -O1 which makes me
think it's a bug related to optimization.

Thanks

Hannes

             reply	other threads:[~2023-09-01  8:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-01  8:02 Hannes Mühleisen [this message]
2023-09-01  9:38 ` Jonathan Wakely

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=CAMf67Cv6+DA2fa1M67+33gzOUZMi-qBdsxSWfwci9MtB6mM4wQ@mail.gmail.com \
    --to=hannes@duckdblabs.com \
    --cc=gcc-bugs@gcc.gnu.org \
    --cc=tom@duckdblabs.com \
    /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).