Comment by raverbashing

3 years ago

"unacceptably risky" except I can't think of anything more basic than asking the computer if a > b and it fails at that?

UB invades your whole program, not specific lines.

However in this case, the culprit wasn't comparison `a > b`, but assignment `a = b`.

In general, addition like 'a + b' also isn't safe in C.

  • It's not the assignment. It's the multiplication x * 0x1ff.

    The compiler has done range analysis and knows that at this point, x is non-negative. The programmer has dilgently ensured that values are such that the multiplication can't overflow, therefore the result of it is also non-negative. That means the later check for i being non-negative is trivially true.

  • If it's wrong break on compiler time, not on run time

    The problem is the compiler going implementation defined on the multiplication/assignment then going all language lawyer on the following line and blaming the user

    > In general, addition like 'a + b' also isn't safe in C.

    Cool, another reason to retire it

how does it fail at that? it does exactly what the standard advertises. you need to write standard-compliant code.