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

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);
}