History

fix UB in constant folding of double -> signed integer conversion

Guest0x0 committed 2 years ago

Commit b8b6a5f1 parent 12acbf33 files · +18 −1Browse files

Unified · Split

3 files
  1. tccgen.c +4 −1
  2. tests/tests2/134_double_to_signed.c +10 −0
  3. tests/tests2/134_double_to_signed.expect +4 −0
tccgen.c+4−1
@@ -3249,7 +3249,10 @@ error: · 3248 unmodified lines
3249 vtop->c.i = (vtop->c.ld != 0);3249 vtop->c.i = (vtop->c.ld != 0);
3250 } else {3250 } else {
3251 if(sf)3251 if(sf)
3252 vtop->c.i = vtop->c.ld;3252 /* the range of [int64_t] is enough to hold the integer part of any float value.
3253 Meanwhile, converting negative double to unsigned integer is UB.
3254 So first convert to [int64_t] here. */
3255 vtop->c.i = (int64_t)vtop->c.ld;
3253 else if (sbt_bt == VT_LLONG || (PTR_SIZE == 8 && sbt == VT_PTR))3256 else if (sbt_bt == VT_LLONG || (PTR_SIZE == 8 && sbt == VT_PTR))
3254 ;3257 ;
3255 else if (sbt & VT_UNSIGNED)3258 else if (sbt & VT_UNSIGNED)
tests/tests2/134_double_to_signed.c+10−0
@@ -0,0 +1,10 @@
1#include <stdio.h>
2int main() {
3 printf("%d\n", (int)-1.0);
4 double d = -1.0;
5 printf("%d\n", (int)d);
6
7 printf("%d\n", (int)-2147483648.0);
8 d = -2147483648.0;
9 printf("%d\n", (int)d);
10}
tests/tests2/134_double_to_signed.expect+4−0
@@ -0,0 +1,4 @@
1-1
2-1
3-2147483648
4-2147483648

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t