Open ian-abbott opened 1 year ago
This code has undefined behavior - though there's discussion on other bugs about at least ensuring that functions optimized in this way at least end with a ud2 or other trap instruction.
I know a lot of folks believe that this is not undefined behavior in C but both Zero-initialize objects of automatic storage duration and Uninitialized Reads Understanding the proposed revisions to the C language there is not consensus in this area and it should still be considered UB in C as well as C++.
Consider this invalid program using an uninitialized variable:
foo.c
Compiling to assembly language with clang-15 on Debian, with optimizer enabled:
Details of compiler used:
Resulting assembly language:
foo.s
Note that no assembly code been emitted for the C code from the line
if (garbage)
to the end of the function, including any code to return from the function.Related question by user Lundin (not me) on StackOverflow:
https://stackoverflow.com/q/75533693/5264491