Remix.run Logo
▲ Panzerschrek 3 hours ago

That's why it's recommended to use size_t for passing sizes of buffers. It's unlikely to get overflow with it. In this particular case input buffer needs to have size larger than half of address space in order to trigger overflow, which isn't possible (64-bit systems usually have only 48 bits of actual address space, 32-bit systems don't allow allocating so large blocks).

▲wahern 2 hours ago | parent | next [-]

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.

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

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 14 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 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.

▲jurf 7 minutes ago | parent | prev [-]

ssize_t

▲m00dy 3 hours ago | parent | prev [-]

never say never :)