Remix.run Logo
▲ wahern 2 hours ago

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.

▲jurf 4 minutes ago | parent | next [-]

ssize_t

▲uecker 2 hours ago | parent | prev [-]

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

▲wahern 11 minutes ago | parent [-]

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 thinking long and hard about your arithmetic, and at least for me, when I expend that effort I also tend to use overflow checking APIs, clamping, etc. Do that and the benefits of signed start to disappear, or at least the debate starts to lose relevance.