]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Fix UB in ldbl-128ibm llroundl
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 8 May 2025 12:21:06 +0000 (12:21 +0000)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 8 May 2025 13:18:12 +0000 (10:18 -0300)
sysdeps/ieee754/ldbl-128ibm/s_lroundl.c

index c357bb09726f1f63067d4f96ee46f32bc1884a8a..57eaeb9562cc02d4345cf7dc3ad366edb2d23f44 100644 (file)
 #include <math_ldbl_opt.h>
 #include <float.h>
 #include <ieee754.h>
+#include <intprops.h>
+
+static inline bool
+check_sign (unsigned long int v1, unsigned long int v2)
+{
+  enum { sign_shift = sizeof (unsigned long int) * CHAR_BIT - 1 };
+  return (v1 & (1UL << sign_shift)) == (v2 & (1UL << sign_shift));
+}
 
 long
 __lroundl (long double x)
 {
   double xh, xl;
-  long res, hi, lo;
+  long res, hi, lo, tmp;
 
   ldbl_unpack (x, &xh, &xl);
 
@@ -84,7 +92,7 @@ __lroundl (long double x)
       res = (long int) ((unsigned long int) hi + (unsigned long int) lo);
 
       /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi).  */
-      if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0)))
+      if (__glibc_unlikely (check_sign (hi, lo) && !check_sign (res, hi)))
        goto overflow;
 
       xh -= lo;
@@ -93,24 +101,25 @@ __lroundl (long double x)
       hi = res;
       if (xh > 0.5)
        {
-         res += 1UL;
+         INT_ADD_WRAPV (res, 1, &res);
        }
       else if (xh == 0.5)
        {
          if (xl > 0.0 || (xl == 0.0 && res >= 0))
-           res += 1UL;
+           INT_ADD_WRAPV (res, 1, &res);
        }
       else if (-xh > 0.5)
        {
-         res -= 1UL;
+         INT_SUBTRACT_WRAPV (res, 1UL, &res);
        }
       else if (-xh == 0.5)
        {
          if (xl < 0.0 || (xl == 0.0 && res <= 0))
-           res -= 1UL;
+           INT_SUBTRACT_WRAPV (res, 1UL, &res);
        }
 
-      if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0)))
+      INT_SUBTRACT_WRAPV (res, hi, &tmp);
+      if (__glibc_unlikely (check_sign (hi, tmp) && !check_sign (res, hi)))
        goto overflow;
 
       return res;