Skip to content

Commit e795000

Browse files
Nicolas Pitreakpm00
authored andcommitted
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/[email protected] Signed-off-by: Nicolas Pitre <[email protected]> Cc: Biju Das <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Weißschuh <[email protected]> Cc: Uwe Kleine-König <[email protected]> Cc: David Laight <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent d71b90e commit e795000

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

lib/math/div64.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,13 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)
212212

213213
#endif
214214

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+
}
221222

222223
int shift = __builtin_ctzll(c);
223224

0 commit comments

Comments
 (0)