* [PATCH] fixp-arith: prevent division by zero in fixp_sin32_rad()
@ 2024-03-12 9:48 Roman Smirnov
2024-03-13 18:23 ` Sergey Shtylyov
0 siblings, 1 reply; 3+ messages in thread
From: Roman Smirnov @ 2024-03-12 9:48 UTC (permalink / raw)
To: linux-kernel; +Cc: Roman Smirnov, Sergey Shtylyov, lvc-project
The parameter twopi can have a zero value. It is necessary
to prevent division by zero.
Found by Linux Verification Center (linuxtesting.org) with Svace.
Signed-off-by: Roman Smirnov <r.smirnov@omp.ru>
---
include/linux/fixp-arith.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/include/linux/fixp-arith.h b/include/linux/fixp-arith.h
index e485fb0c1201..434eee8bd9ef 100644
--- a/include/linux/fixp-arith.h
+++ b/include/linux/fixp-arith.h
@@ -118,8 +118,9 @@ static inline s32 fixp_sin32_rad(u32 radians, u32 twopi)
/*
* Avoid too large values for twopi, as we don't want overflows.
+ * Also prevent division by zero.
*/
- BUG_ON(twopi > 1 << 18);
+ BUG_ON(!twopi || twopi > 1 << 18);
degrees = (radians * 360) / twopi;
tmp = radians - (degrees * twopi) / 360;
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] fixp-arith: prevent division by zero in fixp_sin32_rad()
2024-03-12 9:48 [PATCH] fixp-arith: prevent division by zero in fixp_sin32_rad() Roman Smirnov
@ 2024-03-13 18:23 ` Sergey Shtylyov
2026-01-31 19:26 ` Sergey Shtylyov
0 siblings, 1 reply; 3+ messages in thread
From: Sergey Shtylyov @ 2024-03-13 18:23 UTC (permalink / raw)
To: Roman Smirnov, linux-kernel; +Cc: lvc-project
On 3/12/24 12:48 PM, Roman Smirnov wrote:
> The parameter twopi can have a zero value. It is necessary
> to prevent division by zero.
>
> Found by Linux Verification Center (linuxtesting.org) with Svace.
>
> Signed-off-by: Roman Smirnov <r.smirnov@omp.ru>
> ---
> include/linux/fixp-arith.h | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/fixp-arith.h b/include/linux/fixp-arith.h
> index e485fb0c1201..434eee8bd9ef 100644
> --- a/include/linux/fixp-arith.h
> +++ b/include/linux/fixp-arith.h
> @@ -118,8 +118,9 @@ static inline s32 fixp_sin32_rad(u32 radians, u32 twopi)
>
> /*
> * Avoid too large values for twopi, as we don't want overflows.
> + * Also prevent division by zero.
> */
> - BUG_ON(twopi > 1 << 18);
> + BUG_ON(!twopi || twopi > 1 << 18);
Not really sure ATM that BUG_ON() is better than division by 0
(which should cause exception 0 on x86 but I'm not sure about e.g.
ARM32/64)...
>
> degrees = (radians * 360) / twopi;
> tmp = radians - (degrees * twopi) / 360;
>
Reviewed-by: Sergey Shtylyov <s.shtylyov@omp.ru>
[...]
MBR, Sergey
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] fixp-arith: prevent division by zero in fixp_sin32_rad()
2024-03-13 18:23 ` Sergey Shtylyov
@ 2026-01-31 19:26 ` Sergey Shtylyov
0 siblings, 0 replies; 3+ messages in thread
From: Sergey Shtylyov @ 2026-01-31 19:26 UTC (permalink / raw)
To: Roman Smirnov, linux-kernel; +Cc: lvc-project
On 3/13/24 9:23 PM, Sergey Shtylyov wrote:
[...]
>> The parameter twopi can have a zero value. It is necessary
>> to prevent division by zero.
>>
>> Found by Linux Verification Center (linuxtesting.org) with Svace.
>>
>> Signed-off-by: Roman Smirnov <r.smirnov@omp.ru>
>> ---
>> include/linux/fixp-arith.h | 3 ++-
>> 1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/include/linux/fixp-arith.h b/include/linux/fixp-arith.h
>> index e485fb0c1201..434eee8bd9ef 100644
>> --- a/include/linux/fixp-arith.h
>> +++ b/include/linux/fixp-arith.h
>> @@ -118,8 +118,9 @@ static inline s32 fixp_sin32_rad(u32 radians, u32 twopi)
>>
>> /*
>> * Avoid too large values for twopi, as we don't want overflows.
>> + * Also prevent division by zero.
>> */
>> - BUG_ON(twopi > 1 << 18);
>> + BUG_ON(!twopi || twopi > 1 << 18);
>
> Not really sure ATM that BUG_ON() is better than division by 0
> (which should cause exception 0 on x86 but I'm not sure about e.g.
> ARM32/64)...
Now I know what happens there: on arch/arm/ an pr_err() will be called
followed by dump_stack(); on arch/arm64/ division instruction will silently
yield 0 result...
>>
>> degrees = (radians * 360) / twopi;
>> tmp = radians - (degrees * twopi) / 360;
>>
>
> Reviewed-by: Sergey Shtylyov <s.shtylyov@omp.ru>
The patch does seem legitimate...
]...]
MBR, Sergey
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-01-31 19:42 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-12 9:48 [PATCH] fixp-arith: prevent division by zero in fixp_sin32_rad() Roman Smirnov
2024-03-13 18:23 ` Sergey Shtylyov
2026-01-31 19:26 ` Sergey Shtylyov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®