> That interpretation of the specification language is clearly bogus as that means more or less all C and C++ programs can be compiled to a single return instruction - when compiler devs introduced this new new interpretation of UB, it turned out they had to then go through and add a bunch of exceptions for basics like memset, memcpy, and memmove could work without falling afoul of their invented nonsense.
I've never heard of anything like this, have you any sources for this happening?
I think aliasing rules messes up memcpy if you are dogmatic?
> I think aliasing rules messes up memcpy if you are dogmatic?
I'm _not_ dogmatic or a language lawyer and aliasing is a total pain in the ass, but that's a whole different topic to compilers getting fundamentals wrong because of language lawyering.
[0]: https://stackoverflow.com/questions/49298704/how-reason-abou...
memmove fails as it requires pointer comparisons between separate allocations, which is UB.
Comparing unrelated pointers is unspecified, not undefined. It's also an implementation detail of memmove. It can use a magical total order comparison of pointers that is otherwise unavailable to users of the language.
memset and memcpy read and write padding bytes, which is UB. If you try to say memset/cpy is a "primitive operation", then you're still stuck with the "correct" struct initialization (memset, bzero) being UB if you did something like:
void f(bool condition) {
if (condition) {
struct structwithpadding thing;
bzero(&thing, sizeof(thing));
} else {
printf("wat\n");
}
}
The ludicrously broad definition of what UB apparent allows, this can be "optimized" to void f(bool condition) {
printf("wat\n");
}
Because bzero() writes to padding, and that is UB, which means the condition==true branch "can't happen" \o/memmove falls to UB much requires pointer comparisons between pointers to different objects, which is UB.
Compiler designers just chose the notion of "obviously user wouldn't invoke the footgun, so any code that would be causes by footgun can be removed"
Since, in the process of optimization, optimizing compilers make conclusions about parts of a program based on other parts of the program which may be executed later, undefined behavior in one part of a program causes invalid conclusions about other parts which may be executed earlier. Therefore undefined behavior cannot be contained to just parts of a program executed after a particular part, if the compiler tries to optimize parts of the program based on parts that may be executed later.
That interpretation of the specification language is clearly bogus as that means more or less all C and C++ programs can be compiled to a single return instruction - when compiler devs introduced this new new interpretation of UB, it turned out they had to then go through and add a bunch of exceptions for basics like memset, memcpy, and memmove could work without falling afoul of their invented nonsense.