diff mbox series

[8/8] math: Fix UB on sinpif

Message ID 20250425205618.360232-9-adhemerval.zanella@linaro.org
State Accepted
Commit 84977600dace5a7cfcb0918e6757939fd4969839
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 bbfabd99.

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

Comments

Carlos O'Donell April 29, 2025, 12:53 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 bbfabd99.
> 
> 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_sinpif.c | 8 ++++----
>   1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/sysdeps/ieee754/flt-32/s_sinpif.c b/sysdeps/ieee754/flt-32/s_sinpif.c
> index 99a8bbbcf3..c0d15e777b 100644
> --- a/sysdeps/ieee754/flt-32/s_sinpif.c
> +++ b/sysdeps/ieee754/flt-32/s_sinpif.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/sinpi/sinpif.c, revision f786e13).
> +project (src/binary32/sinpi/sinpif.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
> @@ -51,7 +51,7 @@ __sinpif (float x)
>       {
>         if (__glibc_unlikely (s < -6))
>   	return copysignf (0.0f, x);
> -      int32_t iq = m << (-s - 1);
> +      int32_t iq = (uint32_t)m << (-s - 1);
>         iq &= 127;
>         if (iq == 0 || iq == 64)
>   	return copysignf (0.0f, x);
> @@ -63,10 +63,10 @@ __sinpif (float x)
>         return z * (0x1.921fb54442d18p+1 + z2 * (-0x1.4abbce625be53p+2));
>       }
>     int32_t si = 25 - s;
> -  if (__glibc_unlikely (si >= 0 && (m << si) == 0))
> +  if (__glibc_unlikely (si >= 0 && ((uint32_t)m << si) == 0))
>       return copysignf (0.0f, x);
>   
> -  int32_t k = m << (31 - s);
> +  int32_t k = (uint32_t)m << (31 - s);

OK. Cast to uint32_t.

>     double z = k, z2 = z * z;
>     double fs = SN[0] + z2 * (SN[1] + z2 * SN[2]);
>     double fc = CN[0] + z2 * (CN[1] + z2 * CN[2]);

ommit 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_sinpif.c b/sysdeps/ieee754/flt-32/s_sinpif.c
index 99a8bbbcf3..c0d15e777b 100644
--- a/sysdeps/ieee754/flt-32/s_sinpif.c
+++ b/sysdeps/ieee754/flt-32/s_sinpif.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/sinpi/sinpif.c, revision f786e13).
+project (src/binary32/sinpi/sinpif.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
@@ -51,7 +51,7 @@  __sinpif (float x)
     {
       if (__glibc_unlikely (s < -6))
 	return copysignf (0.0f, x);
-      int32_t iq = m << (-s - 1);
+      int32_t iq = (uint32_t)m << (-s - 1);
       iq &= 127;
       if (iq == 0 || iq == 64)
 	return copysignf (0.0f, x);
@@ -63,10 +63,10 @@  __sinpif (float x)
       return z * (0x1.921fb54442d18p+1 + z2 * (-0x1.4abbce625be53p+2));
     }
   int32_t si = 25 - s;
-  if (__glibc_unlikely (si >= 0 && (m << si) == 0))
+  if (__glibc_unlikely (si >= 0 && ((uint32_t)m << si) == 0))
     return copysignf (0.0f, x);
 
-  int32_t k = m << (31 - s);
+  int32_t k = (uint32_t)m << (31 - s);
   double z = k, z2 = z * z;
   double fs = SN[0] + z2 * (SN[1] + z2 * SN[2]);
   double fc = CN[0] + z2 * (CN[1] + z2 * CN[2]);