Skip to content

Commit bb0548a

Browse files
committed
[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
1 parent 684955a commit bb0548a

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

compiler-rt/lib/builtins/floatdidf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ COMPILER_RT_ABI double __floatdidf(di_int a) {
5050
return 0.0;
5151
const unsigned N = sizeof(di_int) * CHAR_BIT;
5252
const di_int s = a >> (N - 1);
53-
a = (a ^ s) - s;
53+
a = (du_int)(a ^ s) - s;
5454
int sd = N - __builtin_clzll(a); // number of significant digits
5555
int e = sd - 1; // exponent
5656
if (sd > DBL_MANT_DIG) {

compiler-rt/lib/builtins/floatdisf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ COMPILER_RT_ABI float __floatdisf(di_int a) {
2424
return 0.0F;
2525
const unsigned N = sizeof(di_int) * CHAR_BIT;
2626
const di_int s = a >> (N - 1);
27-
a = (a ^ s) - s;
27+
a = (du_int)(a ^ s) - s;
2828
int sd = N - __builtin_clzll(a); // number of significant digits
2929
si_int e = sd - 1; // exponent
3030
if (sd > FLT_MANT_DIG) {

0 commit comments

Comments
 (0)