[compiler-rt] bb0548a - [compiler-rt] Avoid signed overflow in floatdidf.c and floatdisf.c

Karl-Johan Karlsson via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 17 00:15:27 PDT 2023


Author: Karl-Johan Karlsson
Date: 2023-03-17T08:14:57+01:00
New Revision: bb0548a6edf0027eca8d92e021c45afd51b29c9e

URL: https://github.com/llvm/llvm-project/commit/bb0548a6edf0027eca8d92e021c45afd51b29c9e
DIFF: https://github.com/llvm/llvm-project/commit/bb0548a6edf0027eca8d92e021c45afd51b29c9e.diff

LOG: [compiler-rt] Avoid signed overflow in floatdidf.c and floatdisf.c

When compiling compiler-rt with -fsanitize=undefined and running testcases you
end up with the following warning:

UBSan: floatdisf.c:27:15: signed integer overflow: 9223372036854775807 - -1 cannot be represented in type 'di_int' (aka 'long long')

This can be avoided by doing the subtraction in a matching unsigned variant of
the type, given that the overflow is the expected result of the subtraction.

The same kind of pattern exists in floatdidf.c

This was found in an out of tree target.

Reviewed By: phosek

Differential Revision: https://reviews.llvm.org/D146135

Added: 
    

Modified: 
    compiler-rt/lib/builtins/floatdidf.c
    compiler-rt/lib/builtins/floatdisf.c

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/builtins/floatdidf.c b/compiler-rt/lib/builtins/floatdidf.c
index d37c43b1f2f92..c994aad3f079e 100644
--- a/compiler-rt/lib/builtins/floatdidf.c
+++ b/compiler-rt/lib/builtins/floatdidf.c
@@ -50,7 +50,7 @@ COMPILER_RT_ABI double __floatdidf(di_int a) {
     return 0.0;
   const unsigned N = sizeof(di_int) * CHAR_BIT;
   const di_int s = a >> (N - 1);
-  a = (a ^ s) - s;
+  a = (du_int)(a ^ s) - s;
   int sd = N - __builtin_clzll(a); // number of significant digits
   int e = sd - 1;                  // exponent
   if (sd > DBL_MANT_DIG) {

diff  --git a/compiler-rt/lib/builtins/floatdisf.c b/compiler-rt/lib/builtins/floatdisf.c
index 5c6316431e394..0b62ed8689bc6 100644
--- a/compiler-rt/lib/builtins/floatdisf.c
+++ b/compiler-rt/lib/builtins/floatdisf.c
@@ -24,7 +24,7 @@ COMPILER_RT_ABI float __floatdisf(di_int a) {
     return 0.0F;
   const unsigned N = sizeof(di_int) * CHAR_BIT;
   const di_int s = a >> (N - 1);
-  a = (a ^ s) - s;
+  a = (du_int)(a ^ s) - s;
   int sd = N - __builtin_clzll(a); // number of significant digits
   si_int e = sd - 1;               // exponent
   if (sd > FLT_MANT_DIG) {


        


More information about the llvm-commits mailing list