Is it possible, within the confines of only the C++ language, to write a test that isolates and detects undefined behavior when executed? For example, given the following code:
void fn(int* p) {
int x = *p;
}
void foo() {
int x = 42;
fn(&x);
}
void bar() {
fn(nullptr);
}
Is it possible to construct a test that invokes foo and bar, and "passes" for foo but "fails" for bar? My assumption is that it is not possible because the very act of invoking bar means the compiler is free to generate code that always passes, always fails, only fails on Tuesdays, or just prints "thanks for all the fish" in an endless loop. Still I'd like to get confirmation from the experts that this is the case.
Note: I'm aware of things like clang's ubsan, static analysis tools, and platform-specific traps to assist with detection of UB, but my question is limited to the C++ language only.
Aucun commentaire:
Enregistrer un commentaire