Open wangbo15 opened 1 week ago
This looks like the usual "side-effect free infinite loops are UB", but cc @AaronBallman as I'm not sure whether that applies to for (;;)
as well or not.
This is undefined behavior because the variable used to index the array is uninitialized, but let's pretend that's initialized so we're back to just looking at the loop.
https://eel.is/c++draft/basic.exec#intro.progress-1.6 says the program has to make forward progress if the infinite loop is trivial. https://eel.is/c++draft/stmt.iter.general#3 says this infinite loop is not trivial (because the loop has a non-empty body).
so I believe the loop is also UB.
This is undefined behavior because the variable used to index the array is uninitialized, but let's pretend that's initialized so we're back to just looking at the loop.
https://eel.is/c++draft/basic.exec#intro.progress-1.6 says the program has to make forward progress if the infinite loop is trivial. https://eel.is/c++draft/stmt.iter.general#3 says this infinite loop is not trivial (because the loop has a non-empty body).
so I believe the loop is also UB.
Based on the standard you referenced, I believe this is not UB.
If we modify the program as shown below, by transforming it into a non-trivial loop where the function b
produces side effects, this change highlights differing behavior across optimization levels.
For example, clang -O0
, clang -Os
, and clang -O1
exhibit varying behaviors in this scenario.
#include <memory>
unsigned long long a;
unsigned long long g;
void b(unsigned long long *a, int b) { g++; }
class {
public:
short c[10];
} e;
int main() {
for (size_t d = 0;;)
b(&a, e.c[d]);
}
b
does not produce "side effects" in the sense of the standard. You'd have to make g
volatile for that (or similar).
b
does not produce "side effects" in the sense of the standard. You'd have to makeg
volatile for that (or similar).
If change to volatile unsigned long long g
, the return values are same.
That's insufficient to produce the side effects though. Here's a more complete example without UB: https://godbolt.org/z/fvvK4KhYc
The following code triggers a
SIGSEGV
when compiled withclang-trunk
andclang 19.1.0
using the-Os
optimization level. With-O1
, the program exits with a return value of64
. However, when compiled withclang
-O0
orgcc
-O2
, the program behaves as expected and enters an infinite loop.Additionally, the behavior cases are also different on the
arm
backends.Please see: https://godbolt.org/z/79fzqePEv