Is anyone aware of a real world situation where this would actually be a problem?
Undefined behavior can make anything false. Suppose you have a test T and undefined behavior UB. In the code if (T) { … UB … }, the compiler is allowed to conclude UB will never occur, and therefore T must be false. This can lead it to determine that variables used in T must or must not have certain values, and so on.
In particular, in the code if (T) { … UB … } else { … OtherCode … }, the compiler may eliminate the test T (except for any observable behavior it contains) and eliminate the “then” clause and always execute only the “else” clause.
Whether this occurs in any particular situation may be difficult to predict, as compiler optimizers handle numerous chains of deductions/reductions/transformations.
In your case, consider the code:
unsigned int n = 1;
char* s = "X" + 1;
char *p = malloc(foo);
if (!p)
{
fprintf(stderr, "Error, out of memory.\n");
exit(EXIT_FAILURE);
}
*p = s[n];
…
Similarly to the code explained above, the compiler may conclude !p is always true, in which case this program would always report it is out of memory even though the malloc actually returns a non-null pointer. That would be good since you would catch the error immediately, before deploying the program. But the same compiler behavior could produce a more insidious result that is not caught for a long time.