https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111245
Bug ID: 111245
Summary: miscompilation: missing assignment when exception
thrown
Product: gcc
Version: 13.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: briangreenery at gmail dot com
Target Milestone: ---
gcc version 13.2.0 (Compiler-Explorer-Build-gcc--binutils-2.40)
Target: x86_64-linux-gnu
It looks like initialization is incorrectly skipped when:
* The initialization is overwritten by an unconditional assignment from a
function
* However, that function throws an exception which avoids the assignment
The following code should return 5, but it returns random garbage on -O1 or
-O2.
https://godbolt.org/z/T3qTnheYW
# 0 "/app/example.cpp"
# 1 "/app//"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 0 "<command-line>" 2
# 1 "/app/example.cpp"
struct Int {
int value;
};
__attribute__((noipa)) Int always_throws() { throw 123; }
void foo(Int &x) {
try {
x = always_throws();
} catch (...) {
}
}
int main() {
Int x;
x.value = 5;
foo(x);
return x.value;
}