]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Fix UB in flt-32 and dbl-64 significand
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Mon, 5 May 2025 15:22:54 +0000 (12:22 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 8 May 2025 12:25:49 +0000 (09:25 -0300)
On x86_64:

$ math/test-float-significand
testing float (without inline functions)
UBSAN: Undefined behaviour in ./s_significand_template.c:31:45 negation of 2147483648 cannot be represented in type 'int'

math/s_significand_template.c

index d00b0525e4607a6bd2dcae2b9beb505c593a16fd..6a0e3faa708800ee535e7f4e1efa82b8de52c8bb 100644 (file)
@@ -28,7 +28,8 @@
 FLOAT
 M_DECL_FUNC (__significand) (FLOAT x)
 {
-  return M_SUF (__ieee754_scalb) (x,(FLOAT) - M_SUF (__ilogb) (x));
+  int ex = - (unsigned int) M_SUF (__ilogb) (x);
+  return M_SUF (__ieee754_scalb) (x,(FLOAT) ex);
 }
 
 /* Don't provide _FloatN aliases for significand which was originally