upvote
Not everybody recommends size_t. There's a sizeable community of people absolutely convinced most integers, including length and capacity integers, should be signed. And they stick to their guns even for C and C++. They have some very compelling logical arguments on their side, but IMO in languages like C and C++ they're just fatally unrealistic.
reply
This is anachronistic imo. There is no point of using signed or larger than needed integers when programming in a modern way.

You have fuzzing, test coverage checks, overflow checks, static analysis, llm review. There is no conceivable reason why you might have a problem with using an u16 for a buffer length.

Even reading the PR comments shows that these libraries are just really low quality.

They are discussing if they should check the error return on callsites now that they made a function fallible.

Even a library like openssl using raw arithmetic operators all over the place is just weird imo. Every arithmetic operation should be checked version of the operation unless there is a provable performance hit. Languages should just not have operators

reply
Why unrealistic? I was in the unsigned camp a long time ago and switched to signed types for indexing and sizes, and this made many problems go a way.

There was just a nginx issue that highlights how problematic the use of unsigned is in this context: CVE -2026-123007

reply
From a brief look, I'd say there were several wrong turns made that led to the CVE-2026-123007 overflow bug.

For why I think it's unrealistic to go all-in on signed integers in C, it's because sizeof is unsigned, and most APIs dealing with lengths use size_t, and there are many pitfalls when mixing signed and unsigned types.

But I agree that there's a good case for using signed for the task behind that CVE. As I said, there are compelling reasons.

OpenSSL is a case study in the pitfalls with mixing signed and unsigned in C code. A major reason is the careful dance it has to do mixing signed and unsigned, and it stumbles. There's no substitute for being careful with arithmetic, and at least for me, when I expend that effort I also tend to use overflow checking APIs, clamping, etc, because it's less prone to error. Do that and the benefits of signed start to disappear, or at least the debate starts to lose relevance. Why OpenSSL doesn't create and use a set of overflow checking routines is beyond me as it would simplify an incredible amount of very hairy and brittle code.

reply
The way to deal with APIs which use size_t and which works for me is to convert at the last moment.
reply
I'll have to more carefully read some of your code sometime. Any links to good exemplars? I'll admit I should be more amenable to making use of signed, and I wouldn't have said that several years ago.

At the same time, the more I employ overflow checking routines[1], the less interest I have in the whole debate. Those routines cut both ways as they also make it safer to mix signed and unsigned.

[1] GCC/clang builtins, which IIUC the new C2y API mirrors. The return value semantic is unfortunate, especially in C2y where they lack the "overflow" name prefix, but it's understandable why C2y had to copy it.

reply
Why would anyone prefer signed for indexing and sizes?

With unsigned you only need one check: 'i < bound', it works always, regardless how i is computed, and it doesn't matter if the calculation overflowed or wrapped around, still works.

With signed you need: 'i >= 0 && i < bound', and you need to be very careful how you calculate i because overflow is undefined behavior.

reply
The issue with accidental overflow leading to wrap-around is also relevant to the value of `bound`.

If `bound` is unsigned and experiences accidental wrap-around due to underflow, the simple `i < bound` check fails open: most values of i pass against the unintentionally-huge `bound`. The signed version fails closed (no i passes both i >= 0 and i < bound), which is usually safer.

That’s how the argument goes, anyway. You’re right that using signed means a missing `>= 0` check can be a problem, as well as all the risks of signed-overflow UB.

No perfect solution in C.

reply
The N >= 0 check you often get automatically using sanitizers, e.g. when using an array [N] somewhere, or by concerting to size_t at last moment and then getting a size which is so big that allocation is guaranteed to fail.

Not that are wraparound CVEs in Rust as well, so there is no perfect solution even outside of C.

reply
ssize_t
reply

    > That's why it's recommended to use size_t for passing sizes of buffers.
I have always been told it is a bad idea to use unsigned integers in your interfaces. Why? An accidental negative (signed) value will be cast into an enormous unsigned value. For example, if you look at the vast majority of the Win32 API, inputs are normally signed integers. They rarely use unsigned integers.
reply
> An accidental negative (signed) value will

If you use mostly unsigned values it's not a huge problem, as long as you don't perform subtractions or operations with possibility of overflow. If obtaining a negative value or getting an overflow may occur, you should perform proper checks before calling an API function. And in case of WinAPI or something similar usually no extra computations before a call are necessary, arguments are already precomputed (like with buffer sizes).

reply
never say never :)
reply