public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r8-10890] fold-const: Fix ICE in fold_read_from_constant_string on invalid code [PR99204]
@ 2021-04-22 16:51 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2021-04-22 16:51 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:f843203ea2c65d6319d784079baa8375c406a52c

commit r8-10890-gf843203ea2c65d6319d784079baa8375c406a52c
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Feb 23 09:49:48 2021 +0100

    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.
    
    (cherry picked from commit f53a9b563b5017af179f1fd900189c0ba83aa2ec)

Diff:
---
 gcc/fold-const.c                      |  2 +-
 gcc/testsuite/gfortran.dg/pr99204.f90 | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 520f677fd1d..a319c61d471 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -13817,7 +13817,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)
diff --git a/gcc/testsuite/gfortran.dg/pr99204.f90 b/gcc/testsuite/gfortran.dg/pr99204.f90
new file mode 100644
index 00000000000..9b6f27fa3d2
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr99204.f90
@@ -0,0 +1,10 @@
+! PR tree-optimization/99204
+! { dg-do compile }
+! { dg-options "-O2 -w" }
+
+program pr99204
+  character :: c
+  integer :: i = -12345678
+  c = 'abc'(i:i)
+  print *, c
+end


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-22 16:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-22 16:51 [gcc r8-10890] fold-const: Fix ICE in fold_read_from_constant_string on invalid code [PR99204] Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).