Skip to content

Commit 854686f

Browse files
committed
[compiler-rt] Fix signed shift overflows in absvdi2.c, absvsi2.c, negvdi2.c and negvsi2.c
When compiling compiler-rt with -fsanitize=undefined and running testcases you end up with the following warnings: UBSan: absvdi2.c:21:23: left shift of 1 by 63 places cannot be represented in type 'di_int' (aka 'long long') UBSan: absvsi2.c:21:23: left shift of 1 by 31 places cannot be represented in type 'si_int' (aka 'long') UBSan: negvdi2.c:20:32: left shift of 1 by 63 places cannot be represented in type 'di_int' (aka 'long long') UBSan: negvsi2.c:20:32: left shift of 1 by 31 places cannot be represented in type 'si_int' (aka 'long') This can be avoided by doing the shift in a matching unsigned variant of the type. This was found in an out of tree target. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D146932
1 parent fb77ca0 commit 854686f

File tree

4 files changed

+6
-4
lines changed

4 files changed

+6
-4
lines changed

compiler-rt/lib/builtins/absvdi2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
COMPILER_RT_ABI di_int __absvdi2(di_int a) {
2020
const int N = (int)(sizeof(di_int) * CHAR_BIT);
21-
if (a == ((di_int)1 << (N - 1)))
21+
if (a == ((di_int)((du_int)1 << (N - 1))))
2222
compilerrt_abort();
2323
const di_int t = a >> (N - 1);
2424
return (a ^ t) - t;

compiler-rt/lib/builtins/absvsi2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
COMPILER_RT_ABI si_int __absvsi2(si_int a) {
2020
const int N = (int)(sizeof(si_int) * CHAR_BIT);
21-
if (a == ((si_int)1 << (N - 1)))
21+
if (a == ((si_int)((su_int)1 << (N - 1))))
2222
compilerrt_abort();
2323
const si_int t = a >> (N - 1);
2424
return (a ^ t) - t;

compiler-rt/lib/builtins/negvdi2.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
// Effects: aborts if -a overflows
1818

1919
COMPILER_RT_ABI di_int __negvdi2(di_int a) {
20-
const di_int MIN = (di_int)1 << ((int)(sizeof(di_int) * CHAR_BIT) - 1);
20+
const di_int MIN =
21+
(di_int)((du_int)1 << ((int)(sizeof(di_int) * CHAR_BIT) - 1));
2122
if (a == MIN)
2223
compilerrt_abort();
2324
return -a;

compiler-rt/lib/builtins/negvsi2.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
// Effects: aborts if -a overflows
1818

1919
COMPILER_RT_ABI si_int __negvsi2(si_int a) {
20-
const si_int MIN = (si_int)1 << ((int)(sizeof(si_int) * CHAR_BIT) - 1);
20+
const si_int MIN =
21+
(si_int)((su_int)1 << ((int)(sizeof(si_int) * CHAR_BIT) - 1));
2122
if (a == MIN)
2223
compilerrt_abort();
2324
return -a;

0 commit comments

Comments
 (0)