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

            Bug ID: 121814
           Summary: Optimization has changed the code behavior
           Product: gcc
           Version: 13.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: liweifriends at gmail dot com
  Target Milestone: ---

Created attachment 62318
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62318&action=edit
The file that can re-pro the issue

I have attached a cpp file with this report. The cpp file:

1. dump a pointer (po) into a string
2. get the address and save it into another pointer (pi)
3. compare the 2 pointers, throw exception if the 2 of them are not equal.

When compiling with G++ 13.3 without optimization, i.e.:
``
g++ -O0 -Wall -std=c++23 -g -DNDEBUG  main.cpp
``
The code works as expected.

When compiling with G++ 13.3 with -O3 optimization, i.e.:
``
g++ -O3 -Wall -std=c++23 -g -DNDEBUG  main.cpp
``
The program throws exception, the system thought 2 pointers are not equal,
which is incorrect.

When compiling with G++ 11.5 with -O3 optimization, the system reports several
warnings during compiling, but the program does not throw exceptions at all. 

I guess this is a compiling bug introduced by the relative new version of G++.

BTW:
1. If I add some logic to dump the pointers with cout, the exceptions will not
throw anymore.
2. When I tried to use valgrind to analyze the program with -O3 optimization, I
can see the following error message:

==212907== Conditional jump or move depends on uninitialised value(s)
==212907==    at 0x484F238: strlen (in
/usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==212907==    by 0x109502: length (char_traits.h:399)
==212907==    by 0x109502: append (basic_string.h:1258)
==212907==    by 0x109502: operator+= (basic_string.h:1170)
==212907==    by 0x109502: put(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >&, void const*) (main.cpp:44)
==212907==    by 0x1092BD: main (main.cpp:103)

Reply via email to