They just think you have lots of dead code because of silly undefined behavior nonsense. char *allocate_a_string_please(int n)
{
if (n + 1 < n)
return 0; // overflow
return malloc(n + 1); // space for the NUL
}
This code seems okay at first glance, it's a simple integer overflow check that makes sense to anyone who reads it. The addition will overflow when n equals INT_MAX, it's going to wrap around and the function will return NULL. Reasonable.Unfortunately, we cannot have nice things because of optimizing compilers and the holy C standard. The compiler "knows" that signed integer overflow is undefined. In practice, it just assumes that integer overflow cannot ever happen and uses this "fact" to "optimize" this program. Since signed integers "cannot" overflow, it "proves" that the condition always evaluates to false. This leads it to conclude that both the condition and the consequent are dead code. Then it just deletes the safety check and introduces potential security vulnerabilities into the software. They had to add literal compiler builtins to let people detect overflow conditions and make the compiler actually generate the code they want it to generate. Fighting the compiler's assumptions and axioms gets annoying at some point and people eventually discover the mercy of compiler flags such as -fwrapv and -fno-strict-aliasing. Anyone doing systems programming with strict aliasing enabled is probably doing it wrong. Can't even cast pointers without the compiler screwing things up. |
I have been known to write patches converting signed integers to unsigned integers in places where signed arithmetic makes no sense.