fix UB in constant folding of double -> signed integer conversion
This commit is contained in:
parent
12acbf3e92
commit
b8b6a5fd7b
3 changed files with 18 additions and 1 deletions
5
tccgen.c
5
tccgen.c
|
@ -3249,7 +3249,10 @@ error:
|
||||||
vtop->c.i = (vtop->c.ld != 0);
|
vtop->c.i = (vtop->c.ld != 0);
|
||||||
} else {
|
} else {
|
||||||
if(sf)
|
if(sf)
|
||||||
vtop->c.i = vtop->c.ld;
|
/* the range of [int64_t] is enough to hold the integer part of any float value.
|
||||||
|
Meanwhile, converting negative double to unsigned integer is UB.
|
||||||
|
So first convert to [int64_t] here. */
|
||||||
|
vtop->c.i = (int64_t)vtop->c.ld;
|
||||||
else if (sbt_bt == VT_LLONG || (PTR_SIZE == 8 && sbt == VT_PTR))
|
else if (sbt_bt == VT_LLONG || (PTR_SIZE == 8 && sbt == VT_PTR))
|
||||||
;
|
;
|
||||||
else if (sbt & VT_UNSIGNED)
|
else if (sbt & VT_UNSIGNED)
|
||||||
|
|
10
tests/tests2/134_double_to_signed.c
Normal file
10
tests/tests2/134_double_to_signed.c
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
int main() {
|
||||||
|
printf("%d\n", (int)-1.0);
|
||||||
|
double d = -1.0;
|
||||||
|
printf("%d\n", (int)d);
|
||||||
|
|
||||||
|
printf("%d\n", (int)-2147483648.0);
|
||||||
|
d = -2147483648.0;
|
||||||
|
printf("%d\n", (int)d);
|
||||||
|
}
|
4
tests/tests2/134_double_to_signed.expect
Normal file
4
tests/tests2/134_double_to_signed.expect
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
-1
|
||||||
|
-1
|
||||||
|
-2147483648
|
||||||
|
-2147483648
|
Loading…
Reference in a new issue