diff options
author | Jakub Jelinek <jakub@redhat.com> | 2024-01-25 13:15:23 +0100 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2024-01-25 13:15:23 +0100 |
commit | fb1b7e2fec951ba0bf4f68fac6a16929f4f63910 (patch) | |
tree | afaed299df64594243193e3ad76f5c0ccc665544 /gcc | |
parent | c3de14ba1ba0e77254118af64ed881f115ee42a0 (diff) | |
download | gcc-fb1b7e2fec951ba0bf4f68fac6a16929f4f63910.zip gcc-fb1b7e2fec951ba0bf4f68fac6a16929f4f63910.tar.gz gcc-fb1b7e2fec951ba0bf4f68fac6a16929f4f63910.tar.bz2 |
convert: Fix test for out of bounds shift count [PR113574]
The following patch is miscompiled, because convert_to_integer_1 for
LSHIFT_EXPR tests if the INTEGER_CST shift count is too high, but
incorrectly compares it against TYPE_SIZE rather than TYPE_PRECISION.
The type in question is unsigned _BitInt(1), which has TYPE_PRECISION 1,
TYPE_SIZE 8, and the shift count is 2 in that case.
2024-01-25 Jakub Jelinek <jakub@redhat.com>
PR middle-end/113574
* convert.cc (convert_to_integer_1) <case LSHIFT_EXPR>: Compare shift
count against TYPE_PRECISION rather than TYPE_SIZE.
* gcc.dg/torture/bitint-52.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/convert.cc | 3 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/torture/bitint-52.c | 23 |
2 files changed, 25 insertions, 1 deletions
diff --git a/gcc/convert.cc b/gcc/convert.cc index 179f5e6..5ad24ad 100644 --- a/gcc/convert.cc +++ b/gcc/convert.cc @@ -762,7 +762,8 @@ convert_to_integer_1 (tree type, tree expr, bool dofold) { /* If shift count is less than the width of the truncated type, really shift. */ - if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type))) + if (wi::to_widest (TREE_OPERAND (expr, 1)) + < TYPE_PRECISION (type)) /* In this case, shifting is like multiplication. */ goto trunc1; else diff --git a/gcc/testsuite/gcc.dg/torture/bitint-52.c b/gcc/testsuite/gcc.dg/torture/bitint-52.c new file mode 100644 index 0000000..d2896da --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/bitint-52.c @@ -0,0 +1,23 @@ +/* PR middle-end/113574 */ +/* { dg-do run { target bitint } } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ + +unsigned _BitInt(1) a; +unsigned _BitInt(8) b; + +void +foo (unsigned _BitInt(16) x) +{ + a += (x << 2) | b; +} + +int +main () +{ + foo (0xfef1uwb); + if (a) + __builtin_abort (); + return 0; +} |