Comment by eru
3 years ago
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.
3 years ago
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
> If it's wrong break on compiler time, not on run time
The number being multiplied isn't known until runtime, so there's not a good way of doing this.
Well, funny that the compiler sees no problem in removing a check (on compile time) of a value that is totally possible to obtain, on runtime
3 replies →