r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
Blog post Unsigned Sizes: A Five Year Mistake
https://c3-lang.org/blog/unsigned-sizes-a-five-year-mistake/
91
Upvotes
r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
View all comments
Show parent comment
1
u/EggplantExtra4946 May 07 '26 edited May 07 '26
It's true that it is techically an edge case but unsigned underflow is much less hard to understand and to deal with than sign underflow/overflow. With signed, you need to conside wheter the 2 numbers were positive, negative, one one positive and one negative. If some programmers can't grasp what unsigned underflow/overflow there is no way in hell than they should fully understand all the signed overflow/underflow edge cases and how to deal with them. I for one, can't really claim to understand the signed integer edge cases, but I do understand unsigned integers edge cases, because it is simpler.
I don't think it's fair to call the loop example a deal breaker, iterating in reverse is not that common, it's not hard to solve, it's simpler to solve that signed integer edge cases.
The larger point is that bounds checking is simpler with unsigned integers and god knows how much important this is. With unsigned integers you only need one simple bound check: if (index < length) or if (index + elem_length <= length), but with signed intgers there is always the additional possibility that the input was a negative numbers, always the possibility of a sign overflow. With unsigned, even you have an overflow, you're going to have a bug but at least you will still not have an out-of-bounds memory access, but with signed integers you're allowing possible out-of-bounds access. Integer overflows are a whole class of vulnerabilities that are much harder to reason with and that are much harder to avoid with signed intgers everywhere. All that is what makes me say that the whole "signed integers are easy, unsigned integers are hard" talk is bullshit.
Fair point, but then a negative size is meaningless and there are better ways to check overflow than that. Since we're talking about possible better designs for new languages, not necessarily C/C++, a new language could have a primitive to allow you to check wether the arithmetic operation overflowed, like the C/C++ intrinsics allowing to use jump if carry/overflow in x86, but builtin. That's the best solution to solve this particular problem imo, better have new primitives than reduce the expressivity of what we have now.
With new overflow checking primitives the caller can make that check.
But here again I don't think neither malloc nor unsigned integers are the real issue. Correct programs just won't be allocating that much data because this is more RAM that exist in the entire world and because how much memory a given program will be allocating for temporary or output data structures is correlated to the size of the input, and the input won't be that big. If an overflow happens here it's likely due to a bug somewhere else or because you're using unsanitized input data, input data who should have been rejected earlier because the values are not acceptable.