logoalt Hacker News

kstrauser • today at 6:22 AM • 2 replies • view on HN

How? That doesn’t seem like it should be possible. Why is that?


Replies

robinsonb5 • today at 7:01 AM

As I understand it, the compiler "knows" the range of valid values for a variable (for instance, if an int was promoted from an unsigned char, it knows it doesn't need to bother with testing for negative values). If the value came from uninitialised malloc'ed memory, the compiler knows that there are no valid values less than zero, no valid values directly equal to zero, and no valid values greater than zero - thus all three tests fail, and the overall negated test always succeeds!

➕ show 1 reply
afdbcreid • today at 7:00 AM

Because it invokes UB.