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

            Bug ID: 119951
           Summary: Exception and std::stoi behaves abnormally in a rare
                    occasion
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: invisparent at outlook dot com
  Target Milestone: ---

@code

#include <iostream>
#include <string>

int f() {
    // We do nothing here,
    // deliberately omit the return stmt.
    // and note honestly we never invoke this function.
}

int main() {
    try {
        std::stoi("abc");
    }
    catch (...) {
        std::cerr << "123" << std::endl;
    }
    return 0;
}

@endcode

Code above should always output "123" in cerr because "abc" can't be converted
to int. In MSVC, it does so in various optimization level, C++ standard.
But in gcc, things become interesting:
Fix -lstdc++=17. When I specify optimization level -O0, -O2 and -O3, it does
output "123". But with -O1, program crashed and exit with 541541187(dec).
Fix -lstdc++=23, it only outputs "123" with -O0, but exit with 541541187(dec)
with -O1, -O2 and -O3.
Feeling weird? But oh no there's something weirder:
Note that function, f(), that we never invoke? If we change its return type to
void, or let f returns something, or delete it completely, and then compile it,
program will output "123" in all flags(optimization level, C++ standard) I've
tested!
What's more, if I keep f() ill-formed, but substitute "123" with string literal
"I'm NOT an integer" (xD), all programs output "123".

I'm so... so confused... I know f() is ill-formed, but a function that is never
invoked can affect program's behavior! With those tests above, I have to doubt
that may be a bug...
I have to say this is the most strange bug I've ever met. Fixing it is far
beyond my skill. So, I'd like to say a big thanks to all volunteers &
contributors who maintains gcc. And apologize for the inconvenience if I
misreport this bug.

Extra info:
gcc(g++) version: 14.2.0 (in w64devkit-2.1.0)
System: Windows 11, OS build 26100.3775
CPU: AMD Ryzen 9 9950X3D

Reply via email to