aboutsummaryrefslogtreecommitdiff
path: root/gcc/fold-const.c
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2021-02-23 09:49:48 +0100
committerJakub Jelinek <jakub@redhat.com>2021-02-23 09:49:48 +0100
commit37b64a3547b91677189c6cbf4c08d7c80770a93a (patch)
treef0cba1525653beae219f2f3379164de524cbaf6c /gcc/fold-const.c
parentefa64fcce12074dd542670feb02eaee53e810a30 (diff)
downloadgcc-37b64a3547b91677189c6cbf4c08d7c80770a93a.zip
gcc-37b64a3547b91677189c6cbf4c08d7c80770a93a.tar.gz
gcc-37b64a3547b91677189c6cbf4c08d7c80770a93a.tar.bz2
fold-const: Fix ICE in fold_read_from_constant_string on invalid code [PR99204]
fold_read_from_constant_string and expand_expr_real_1 have code to optimize constant reads from string (tree vs. rtl). If the STRING_CST array type has zero low bound, index is fold converted to sizetype and so the compare_tree_int works fine, but if it has some other low bound, it calls size_diffop_loc and that function from 2 sizetype operands creates a ssizetype difference. expand_expr_real_1 then uses tree_fits_uhwi_p + compare_tree_int and so works fine, but fold-const.c only checked if index is INTEGER_CST and calls compare_tree_int, which means for negative index it will succeed and result in UB in the compiler. 2021-02-23 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/99204 * fold-const.c (fold_read_from_constant_string): Check that tree_fits_uhwi_p (index) rather than just that index is INTEGER_CST. * gfortran.dg/pr99204.f90: New test.
Diffstat (limited to 'gcc/fold-const.c')
-rw-r--r--gcc/fold-const.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index c35f987..afc3b7f 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -15433,7 +15433,7 @@ fold_read_from_constant_string (tree exp)
if (string
&& TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
&& TREE_CODE (string) == STRING_CST
- && TREE_CODE (index) == INTEGER_CST
+ && tree_fits_uhwi_p (index)
&& compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
&& is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
&char_mode)