public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* C++ undefined behaviour sanitiser bug when compiled with optimization
@ 2023-09-01 8:02 Hannes Mühleisen
2023-09-01 9:38 ` Jonathan Wakely
0 siblings, 1 reply; 2+ messages in thread
From: Hannes Mühleisen @ 2023-09-01 8:02 UTC (permalink / raw)
To: gcc-bugs; +Cc: Tom Ebergen
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-09-01 9:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-01 8:02 C++ undefined behaviour sanitiser bug when compiled with optimization Hannes Mühleisen
2023-09-01 9:38 ` Jonathan Wakely
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).