Re:Undefined behavior in C is a reading error
by tom_mellior on Thursday May 20, 2021 @05:36PM
(#27227630)
This was in the Linux kernel, which is compiled with special kernel flags which make dereferencing null pointers legal. In the context of that code, dereferencing a pointer and later checking it for null was absoutely meaningful. Optimizing the later null check was a compiler bug that was acknowledged and fixed. The compiler here didn't respect the semantics it had promised to kernel code. This was entirely uncontroversial; it was not a case of "unwanted optimization based on undefined behavior", it was a case of "compiler bug breaking well-defined code". Again, all this in a kernel context.
In user code GCC will still happily remove the null check because in user code this is an actual bug in the user's code.
In user code GCC will still happily remove the null check because in user code this is an actual bug in the user's code.