Re: [PATCH 5/5] types: Add standard __ob_trap and __ob_wrap scalar types

From: Kees Cook

Date: Tue Mar 31 2026 - 15:00:38 EST


On Tue, Mar 31, 2026 at 11:25:20AM -0700, Linus Torvalds wrote:
> If you want to use the overflow builtins (or our wrappers aorund
> them), you can't just do the math. You have to do crazy crap like
>
> int end;
> if (check_add_overflow(start, len, &end))
> ... handle overflow ...
> do_something(start, end);
>
> which obviously no sane person will do, when they can just write
>
> do_something(start, start+len);
> [...]
> So I think overflow handling should do the same. Instead of the bad
> "check_sub_overflow()" model we have now, we should have
>
> res = add_overflow(a,b,label);
>
> and now you can use a trapping operation *without* having to break the
> normal flow of code, ie you can do
>
> do_something(start, add_overflow(start, len, overflow))
> ...
> overflow:
> // Maybe needs to release locks, who knows
> return -EINVAL;
>
> notice?

The syntax problem (as made clear by many other people, and even you
here in the first half of this email) is that no one will use function
based math primitives. Everyone absolutely hates it. I would have gone
this route (as it is the design of the user-access code), but everyone
so violently rejected functional math that it seemed not even worth the
attempt.

> Wrapping does not need this kind of thing. Wrapping is literally a "I
> know I don't need to care", while trapping is a "I know I need to
> handle it".
>
> It's just that handling the trapping should not need to be done right
> where the operation is done.

I agree completely. The trouble is how to build that into the existing
arithmetic statement syntax of C. This has been an unsolved problem for
50 years. :(

--
Kees Cook