]> gcc.gnu.org Git - gcc.git/commitdiff
This patch fixes PR96624.
authorPaul Thomas <pault@gcc.gnu.org>
Fri, 28 Aug 2020 08:02:58 +0000 (09:02 +0100)
committerPaul Thomas <pault@gcc.gnu.org>
Fri, 28 Aug 2020 08:02:58 +0000 (09:02 +0100)
2020-08-28  Paul Thomas  <pault@gcc.gnu.org>

gcc/fortran
PR fortran/96624
* simplify.c (gfc_simplify_reshape): Detect zero shape and
clear index if found.

gcc/testsuite/
PR fortran/96624
* gfortran.dg/reshape_8.f90 : New test.

gcc/fortran/simplify.c
gcc/testsuite/gfortran.dg/reshape_8.f90 [new file with mode: 0644]

index 074b50c2e6889678bc56a10457b26640376731ce..8e0d2f97a60128c5313b7d5dbe9a079a9ced7a9c 100644 (file)
@@ -6398,7 +6398,7 @@ gfc_simplify_is_contiguous (gfc_expr *array)
 
   if (gfc_is_not_contiguous (array))
     return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 0);
-    
+
   return NULL;
 }
 
@@ -6725,6 +6725,7 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
   unsigned long j;
   size_t nsource;
   gfc_expr *e, *result;
+  bool zerosize = false;
 
   /* Check that argument expression types are OK.  */
   if (!is_constant_array_expr (source)
@@ -6847,7 +6848,14 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
   result->rank = rank;
   result->shape = gfc_get_shape (rank);
   for (i = 0; i < rank; i++)
-    mpz_init_set_ui (result->shape[i], shape[i]);
+    {
+      mpz_init_set_ui (result->shape[i], shape[i]);
+      if (shape[i] == 0)
+       zerosize = true;
+    }
+
+  if (zerosize)
+    goto sizezero;
 
   while (nsource > 0 || npad > 0)
     {
@@ -6897,6 +6905,8 @@ inc:
       break;
     }
 
+sizezero:
+
   mpz_clear (index);
 
   return result;
diff --git a/gcc/testsuite/gfortran.dg/reshape_8.f90 b/gcc/testsuite/gfortran.dg/reshape_8.f90
new file mode 100644 (file)
index 0000000..01799ac
--- /dev/null
@@ -0,0 +1,14 @@
+! { dg-do compile }
+! { dg-options "-fdump-tree-original" }
+!
+! Test the fix for PR96624 in which an attempt was made to assign
+! to the zero length temporary created by reshape, resulting in a segfault.
+!
+! Contributed by Dong Shenpo  <shenpo.dong@compiler-dev.com>
+!
+program test
+  integer :: a(2,0)
+  a = reshape([1,2,3,4], [2,0])
+  print *, a
+end
+! { dg-final { scan-tree-dump-times "data" 4 "original" } }
This page took 0.0887 seconds and 5 git commands to generate.