diff mbox series

[6/8] math: Fix UB on cospif

Message ID 20250425205618.360232-7-adhemerval.zanella@linaro.org
State Accepted
Commit 8eeb7de8a200a43c914cfa230a25b84d3269cae2
Headers show
Series Fix UB in math implementations | expand

Commit Message

Adhemerval Zanella April 25, 2025, 8:54 p.m. UTC
The left shift overflows for 'int', use uint32_t instead.  It syncs
with CORE-MATH commit bbfabd993a71b049c210b0febfd06d18369fadc1.

Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu.
---
 sysdeps/ieee754/flt-32/s_cospif.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Carlos O'Donell April 29, 2025, 12:47 p.m. UTC | #1
On 4/25/25 4:54 PM, Adhemerval Zanella wrote:
> The left shift overflows for 'int', use uint32_t instead.  It syncs
> with CORE-MATH commit bbfabd993a71b049c210b0febfd06d18369fadc1.
> 
> Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu.

LGTM.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>

> ---
>   sysdeps/ieee754/flt-32/s_cospif.c | 6 +++---
>   1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/sysdeps/ieee754/flt-32/s_cospif.c b/sysdeps/ieee754/flt-32/s_cospif.c
> index 1e838037e1..eb4a10f57f 100644
> --- a/sysdeps/ieee754/flt-32/s_cospif.c
> +++ b/sysdeps/ieee754/flt-32/s_cospif.c
> @@ -3,7 +3,7 @@
>   Copyright (c) 2022-2025 Alexei Sibidanov.
>   
>   The original version of this file was copied from the CORE-MATH
> -project (src/binary32/cospi/cospif.c, revision f786e13).
> +project (src/binary32/cospi/cospif.c, revision bbfabd99).

OK.

>   
>   Permission is hereby granted, free of charge, to any person obtaining a copy
>   of this software and associated documentation files (the "Software"), to deal
> @@ -58,10 +58,10 @@ __cospif (float x)
>       {
>         if (__glibc_unlikely (p > 63))
>   	return 1.0f;
> -      int32_t iq = m << (p - 32);
> +      int32_t iq = (uint32_t)m << (p - 32);

OK.

>         return S[(iq + 32) & 127];
>       }
> -  int32_t k = m << p;
> +  int32_t k = (uint32_t)m << p;

OK.

>     if (__glibc_unlikely (k == 0))
>       {
>         int32_t iq = m >> (32 - p);

OK.

commit bbfabd993a71b049c210b0febfd06d18369fadc1
Author: Paul Zimmermann <Paul.Zimmermann@inria.fr>
Date:   Mon Mar 17 12:29:20 2025 +0100

     various fixes to avoid undefined behavior for binary32 functions
     
     (contributed by Ganesh Ajjanagadde <gajjanag@alum.mit.edu>
      see https://sympa.inria.fr/sympa/arc/core-math/2025-03/msg00007.html)
diff mbox series

Patch

diff --git a/sysdeps/ieee754/flt-32/s_cospif.c b/sysdeps/ieee754/flt-32/s_cospif.c
index 1e838037e1..eb4a10f57f 100644
--- a/sysdeps/ieee754/flt-32/s_cospif.c
+++ b/sysdeps/ieee754/flt-32/s_cospif.c
@@ -3,7 +3,7 @@ 
 Copyright (c) 2022-2025 Alexei Sibidanov.
 
 The original version of this file was copied from the CORE-MATH
-project (src/binary32/cospi/cospif.c, revision f786e13).
+project (src/binary32/cospi/cospif.c, revision bbfabd99).
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
@@ -58,10 +58,10 @@  __cospif (float x)
     {
       if (__glibc_unlikely (p > 63))
 	return 1.0f;
-      int32_t iq = m << (p - 32);
+      int32_t iq = (uint32_t)m << (p - 32);
       return S[(iq + 32) & 127];
     }
-  int32_t k = m << p;
+  int32_t k = (uint32_t)m << p;
   if (__glibc_unlikely (k == 0))
     {
       int32_t iq = m >> (32 - p);