News for hackers. Stuff that matters.
Re:Parse, Don't Validate (2019) by imtringued on Tuesday February 10, 2026 @12:47PM (#46963791)
Using uint to exclude negative values is one of the most common mistakes, because underflow wrapping is the default instead of saturation. You subtract a big number from a small number and your number suddenly becomes extremely large. This is far worse than e.g. someone having traveled a negative distance.
Re:Parse, Don't Validate (2019) by Archelaos on Tuesday February 10, 2026 @01:51PM (#46964917)
In C# I use the 'checked' keyword in this or similar cases, when it might be relevant: c = checked(a - b);

Note that this does not violate the "Parse, Don't Validate" rule. This rule does not prevent you from doing stupid things with a "parsed" type.

In other cases, I use its cousin unchecked on int values, when an overflow is okay, such as in calculating an int hash code.