]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Fix UB on llroundl
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 2 May 2025 17:46:40 +0000 (14:46 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 8 May 2025 12:25:49 +0000 (09:25 -0300)
Building with --enable-ubasn triggers:

UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-96/s_llroundl.c:70:25 left shift of 4294967296 by 31 cannot be represented in type 'long long int'

The right shift is undefined if value overflow, but code is assuming
an arithmetic shift.

sysdeps/ieee754/ldbl-96/s_llroundl.c

index d4babe4418256001b88ee9a48aca1f371f4206b6..ad2b3c56c712ca57e9f36a674bc2d1d79b827cf9 100644 (file)
@@ -67,7 +67,8 @@ __llroundl (long double x)
 
          if (j0 > 31)
            {
-             result = (result << (j0 - 31)) | (j >> (63 - j0));
+             result = ((unsigned long long int)result << (j0 - 31))
+               | (j >> (63 - j0));
 #ifdef FE_INVALID
              if (sign == 1 && result == LLONG_MIN)
                /* Rounding brought the value out of range.  */