]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Fix UB in ldbl-128 __ieee754_rem_pio2l
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 7 May 2025 16:04:23 +0000 (16:04 +0000)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 8 May 2025 12:34:17 +0000 (09:34 -0300)
UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-128ibm/e_rem_pio2l.c:254:27 left shift of 325455441821696 by 23 cannot be represented in type 'long int'

sysdeps/ieee754/ldbl-128ibm/e_rem_pio2l.c

index d65d94bb62b9dcc120af77fa51bdd8fa994e25c6..db7f59d5b4aa7960112833c11cbce860a4d1631c 100644 (file)
@@ -249,11 +249,11 @@ int32_t __ieee754_rem_pio2l(long double x, long double *y)
   /* This is faster than doing this in floating point, because we
      have to convert it to integers anyway and like this we can keep
      both integer and floating point units busy.  */
-  tx [0] = (double)(((ixd >> 25) & 0x7fffff) | 0x800000);
-  tx [1] = (double)((ixd >> 1) & 0xffffff);
-  tx [2] = (double)(((ixd << 23) | (lxd >> 41)) & 0xffffff);
-  tx [3] = (double)((lxd >> 17) & 0xffffff);
-  tx [4] = (double)((lxd << 7) & 0xffffff);
+  tx [0] = (double)((((uint64_t)ixd >> 25) & 0x7fffff) | 0x800000);
+  tx [1] = (double)(((uint64_t)ixd >> 1) & 0xffffff);
+  tx [2] = (double)((((uint64_t)ixd << 23) | (lxd >> 41)) & 0xffffff);
+  tx [3] = (double)(((uint64_t)lxd >> 17) & 0xffffff);
+  tx [4] = (double)(((uint64_t)lxd << 7) & 0xffffff);
 
   n = __kernel_rem_pio2 (tx, tx + 5, exp, ((lxd << 7) & 0xffffff) ? 5 : 4,
                         3, two_over_pi);