The irony here is that the CPU will set the underflow making this trivial to detect in ASM with the `jb` instruction. The equivalent C++ would be something like:
if (new_len > child.length())
As stated in the article, checking if an unsigned is less than 0 is a silly logic error but obviously one that is understandable to make. Still it's the sort of thing you'd expect automated tools would be able to catch.
Re:Night of a cascading failure
by AstralStorm on Tuesday January 22, 2019 @12:02PM
(#18969816)
The problem is being written as <= 0. The compiler could complain "why aren't you just == 0 because it is unsigned" but that's probably enabled at -Wpedantic or such level.
Re:Night of a cascading failure
by ridiculous_fish on Monday January 21, 2019 @11:54PM
(#18966082)
That's a good thing: the cliff is visible (zero is familiar) and intuitive (negative numbers are unsupported).
The pitfalls of signed arithmetic are much more insidious. Left shift a negative value? UB. Negate a negative value: maybe it's still negative. INT_MIN / -1? Crash!
Even basic arithmetic operations, e.g. the average of two integers, is unreasonably hard with signed arithmetic.
>Even basic arithmetic operations, e.g. the average of two integers, is unreasonably hard with signed arithmetic.
What do you have in mind? I understand that the addition can overflow, but that can happen with unsigned as well.
I agree with you, but it doesn't sound like it would have helped things in this domain, as a negative index into a string (if they used signed ints and subtracted larger values) would have produced (according to the spec, undefined) values that were likely just as wrong.
Re:Night of a cascading failure
by lostmyoldone on Monday January 21, 2019 @07:20PM
(#18964905)
There was a check for a negative difference before the array access, so signed arithmetic would have hit that check. Well, at least for any reasonable definition of signed arithmetic.
Doesn’t it make more sense to model counting of string length using natural numbers instead of integers?
It’s sort of like a dependent type which ensures you cannot represent invalid values.
I think that only works if your language traps underflow. In a language like C++ where unsigned integers silently wrap around, you’re not really ensuring that you cannot represent invalid values, merely that you represent invalid values as valid values.
This doesn't apply if you are using unsized for bit twiddling, but then you shouldn't be using minus anyway.