Re: [PATCH 5/5] types: Add standard __ob_trap and __ob_wrap scalar types
From: Linus Torvalds
Date: Tue Mar 31 2026 - 14:31:34 EST
On Tue, 31 Mar 2026 at 11:02, Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> The other is saying "overflow needs special handling"
Btw, this is why I also completely despise the current overflow builtins.
Yes, they handle overflow. But they don't treat it as some *special*
thing. They are garbage that forces code that uses them to be garbage.
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);
instead (particularly since usually that "start+len" is just a tiny
detail in the code anyway).
Notice how it breaks up the code logic, and also requires you to add
random intermediate variables etc. It's bad, it makes the code look
bad, and people don't use it as a result.
The reason people like exception handling is that you can make the
fixup be done elsewhere, and you *don't* have to deal with it at the
point where you just want to use the result and with silly
intermediate variables etc.
IOW, exception handling means that you can continue to use the normal
flow of code for the normal case, and you deal with errors separately.
That is good. Much better than the "check_sub_overflow()" kind of crazy thing.
So I very much understand why all modern languages do it - but at the
same time most exception handling is complete garbage, because almost
everybody ends up thinking that it should nest syntactically, which is
completely wrong.
Error handling does not nest: it exits. If you have two different
exceptional cases in the same expression or statement, they have no
inherent nesting, and the order isn't even someting you should care
about. But they can cause different error codes or different fixups,
and they need *separate* handling.
This is why the kernel user space exception handling ended up with a
"label" model the moment the compilers could deal with it (in fact, I
very much asked for that interface, and compilers finally gave it to
me after years).
It means that you can move the exception handling out of line, without
having to interrupt the actual normal code flow. And youc an do it
without the bogus nesting that makes no sense.
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?
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.
And in C, that means "goto". And I actualyl will claim that "goto" is
superior to most crazy language models with some "try()" block that
nests.
Language designers have been corrupted by "things must nest". BS.
Linus