Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mul_u64_u64_div_u64: fix the division-by-zero behavior

The current implementation forces a compile-time 1/0 division, which
generates an undefined instruction (ud2 on x86) rather than a proper
runtime division-by-zero exception.

Change to trigger an actual div-by-0 exception at runtime, consistent with
other division operations. Use a non-1 dividend to prevent the compiler
from optimizing the division into a comparison.

Link: https://lkml.kernel.org/r/q246p466-1453-qon9-29so-37105116009q@onlyvoer.pbz
Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Cc: Biju Das <biju.das.jz@bp.renesas.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Weißschuh <linux@weissschuh.net>
Cc: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Cc: David Laight <david.laight.linux@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Nicolas Pitre and committed by
Andrew Morton
e795000e d71b90e5

+7 -6
+7 -6
lib/math/div64.c
··· 212 212 213 213 #endif 214 214 215 - /* make sure c is not zero, trigger exception otherwise */ 216 - #pragma GCC diagnostic push 217 - #pragma GCC diagnostic ignored "-Wdiv-by-zero" 218 - if (unlikely(c == 0)) 219 - return 1/0; 220 - #pragma GCC diagnostic pop 215 + /* make sure c is not zero, trigger runtime exception otherwise */ 216 + if (unlikely(c == 0)) { 217 + unsigned long zero = 0; 218 + 219 + OPTIMIZER_HIDE_VAR(zero); 220 + return ~0UL/zero; 221 + } 221 222 222 223 int shift = __builtin_ctzll(c); 223 224