r/ProgrammingLanguages • 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

115 comments sorted by

View all comments

Show parent comment

2

u/Tasty_Replacement_29 Bau May 07 '26

Ah I missed this message sorry.

> You are being ridiculous.

No, I'm serious.

> And also there is a simpler way

Both options are not simple. The simple and obvious way is not working because of unsigned.

> James Gosling or not I doubt that this is true.

I do not doubt that this is true. I agree with James Gosling. It doesn't mean you have to agree, right. But you should not call me or James Gosling ridiculous if we don't agree. I don't call you ridiculous either.

> How are integer overflows in multiplications a problem about unsigned integers exactly

They are a problem because there is no way to detect them with unsigned: the result of the multiplication is still positive. With signed, if there is an overflow, then the result of the multiplication is negative, and you can detect that easily.

> Except that malloc malloc worked as expected. Again this has absolutely nothing to do with unsigned integers.

Sure, the problem was the usage of unsigned multiplication. That's my point: the problem wouldn't be there if size_t is signed. See also https://vorpus.org/blog/why-does-calloc-exist/

> Stop it with your hysteria. Unsigned integers are used, so what?

I'm not hysteric. The problem is that unsigned integers are used, yes.

> This so fucking stupid how is it even an solution?

This is not fucking stupid, it is a solution.

> You are being dishonnest about the problem of mixing signed and unsigned.

No, I am not.

> Clearly you think this is unsigned's fault, you never consider that signed integers are equally at fault.

Yes, the problem is unsigned integers in this case.

> I can't even see why you would use signed integers anyway.

Well, with signed integers you would be able to:

  • Do the obvious loop for (size_t i = length-1; i < length; i--) { array[i] }
  • malloc would be able to detect the overflow at malloc(count * sizeof(int));

2

u/flatfinger May 07 '26

With signed, if there is an overflow, then the result of the multiplication is negative, and you can detect that easily.

A multiplication of positive numbers that overflows may yield a negative result, but it need not do so. When using 32-bit math, multiplying 65537 by 65537 will yield 131073.

When performing addition of unsigned numbers, wraparound may be detected by comparing the result with either source operand (in case of wraparound it will be less than both). When subtracting unsigned numbers, wraparound may be detected by comparing the result with the left operand (in case of wraparound it will be greater).

1

u/EggplantExtra4946 May 07 '26 edited May 07 '26

you should not call me or James Gosling ridiculous if we don't agree. I don't call you ridiculous either.

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.

They are a problem because there is no way to detect them with unsigned: the result of the multiplication is still positive. With signed, if there is an overflow, then the result of the multiplication is negative, and you can detect that easily.

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.

malloc would be able to detect the overflow at malloc

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.

1

u/Tasty_Replacement_29 Bau May 08 '26

> 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

You are right. (Even thought, it's quite easy: if the result of the addition has a different sign, it's overflow / underflow - this sign check is not available for unsigned). A bit simplified, there are two kinds of people: those that don't care and / or don't understand, and those that do care and understand. For those that don't care / understand, I am arguing the signed integer is less problematic, because overflow / underflow is extremely rare. With unsigned, it is quite common, because underflow is very close to a common case: 0.

> iterating in reverse is not that common

For Apache Jackrabbit (where I work on) I grepped for "for.*>= 0" and found 42 cases, and 2800 cases of "for.*= 0". Yes it is not common, but there are cases.

> bounds checking

This is true: there is one more case, you need to check for >= 0. But array bounds checking is typically done by the programming language. (this is about designs for new languages).

> a negative size is meaningless

No: it means there was an error. Basically, you have one bit to detect errors.

> with signed integers you're allowing possible out-of-bounds access

I don't follow why you would have it only with signed (if your language allows for 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.

I don't agree at all: with signed, it is easier to understand and prevent.

> a new language could have a primitive to allow you to check wether the arithmetic operation overflowed

Well there are ways to check that, it doesn't need to be integrated in the language. But using it everywhere, always, would slow down the language too much.

> the caller can make that check

The caller can make that check even now, but most callers are too lazy. The point is to have protection for the lazy developer.

> Correct programs

Well this is not about correct programs, it's about protection agains bugs, by _slightly_ incorrect programs.

1

u/EggplantExtra4946 May 08 '26

No: it means there was an error. Basically, you have one bit to detect errors.

So a negative size is not a valid size which is what I said.

I don't follow why you would have it only with signed (if your language allows for out-of-bounds access).

It's more a problem for 32bit sizes. If you happens to overflow an unsigned size/index it will wrap to 0 and stay in bounds, with a signed 32bit if you only have 31 bits available for the size (2GB) and if you overflow the sign in principle you should have an invalid size (negative) except that size/references are casted to unsigned so it could access 2GB of memory after the end of the buffer.

Well there are ways to check that, it doesn't need to be integrated in the language.

Using a conditional jump explcitly designed to check overflows is better and faster than other alternatives. I don't see why new languages should not take advantage of this cpu capability.

But using it everywhere, always, would slow down the language too much.

I didn't say that it all operations should have an implicit overflow check, I'm saying there should the primitive available to allow the user to manually check wether an overflow happened or not. Like this https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html but builtin.

I don't agree at all: with signed, it is easier to understand and prevent.

Bullshit. You yourself agreed that it's harder to understand and like I said, this is one the main classes of vulnerabilties and they happen all the time.

The point is to have protection for the lazy developer.

Sorry but that's not good enough, I would prefer to avoid programs written by lazy developpers and I won't allow them to touch to my code. Programming requires precision, writing correct programs even more so.

1

u/Tasty_Replacement_29 Bau May 08 '26

> So a negative size is not a valid size which is what I said.

Right. But with unsigned you have no way to detect the overflow / underflow, unless if you limit the maximum size (for example, require that the highest bit is zero or so).

> It's more a problem for 32bit sizes.

Yes, 32 bit array indexes... I'm not sure if I want in my language support such large arrays, or use 64-bit array indexing.

> Using a conditional jump explcitly designed to check overflows is better and faster than other alternatives.

Well... you already know that the array size is not negative. (In a memory-safe language). My language converts to C, and for the array-out-of-bounds check I can use

bool indexCheck(int x, int arrayLen) {
    return ((size_t) x) < ((size_t) arrayLen);
}