[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <4qprn4oq-qo64-r3n9-9rs7-345o552qo3s5@onlyvoer.pbz>
Date: Tue, 20 May 2025 18:28:08 -0400 (EDT)
From: Nicolas Pitre <npitre@...libre.com>
To: David Laight <david.laight.linux@...il.com>
cc: Andrew Morton <akpm@...ux-foundation.org>, linux-kernel@...r.kernel.org,
u.kleine-koenig@...libre.com, Oleg Nesterov <oleg@...hat.com>,
Peter Zijlstra <peterz@...radead.org>,
Biju Das <biju.das.jz@...renesas.com>
Subject: Re: [PATCH v2 next 2/4] lib: mul_u64_u64_div_u64() Use BUG_ON() for
divide by zero
On Tue, 20 May 2025, David Laight wrote:
> On Mon, 19 May 2025 22:21:15 -0400 (EDT)
> Nicolas Pitre <npitre@...libre.com> wrote:
>
> > On Sun, 18 May 2025, David Laight wrote:
> >
> > > Do an explicit BUG_ON(!divisor) instead of hoping the 'undefined
> > > behaviour' the compiler generated for a compile-time 1/0 is in any
> > > way useful.
> > >
> > > It may be better to define the function to return ~(u64)0 for
> > > divide by zero.
> > >
> > > Signed-off-by: David Laight <david.laight.linux@...il.com>
> > > ---
> > >
> > > A new change for v2 of the patchset.
> > > Whereas gcc inserts (IIRC) 'ud2' clang is likely to let the code
> > > continue and generate 'random' results for any 'undefined bahaviour'.
> >
> > clang does exactly the same as gcc.
>
> Did you see the recent 'rant' from Linus about the way clang handles UB.
> I'm pretty sure 'divide by zero' is UB, valid options include.
> - Jump to random location in the current function (what Linus was ranting).
> - Jump to any random location with any register values.
> - Enable user access to all kernel memory.
> - Permanently damage your cpu.
> - Make your computer catch fire.
> - Send an ICBM to some unspecified location.
LOL
> If you want a 'divide by zero' error you better generate one. eg:
> int n = 0;
> OPTIMSER_HIDE_VAR(n);
> i = 1 / n;
As you say then. As long as the resulting behavior is coherent for all
division flavors on a given architecture.
Nicolas
Powered by blists - more mailing lists