diff mbox series

[fortran] PR96312 - [10/11 Regression] Reallocation on assignment uses undefined variables

Message ID CAGkQGiLoQwVgM6sh-HtqOUR+NbZWYE8dF8v0kKi369h6wdbp_w@mail.gmail.com
State New
Headers show
Series [fortran] PR96312 - [10/11 Regression] Reallocation on assignment uses undefined variables | expand

Commit Message

Paul Richard Thomas Aug. 9, 2020, 9:46 a.m. UTC
Hi All,

trans-expr.c(fcncall_realloc_result) unconditionally compared the shapes of
the function result and the lhs. This is clearly wrong when the lhs is not
allocated since the bounds could be used uninitialized as found by the
reporter. The patch does the obvious thing by checking the allocation
status before doing the comparison.

Regtests OK on FC31/x86_64 - OK for master?

Paul

This patch fixes PR96312. Cures a used uninitialized warning.

2020-08-9  Paul Thomas  <pault@gcc.gnu.org>

gcc/fortran
PR fortran/96312
* trans-expr.c (fcncall_realloc_result): Only compare shapes if
lhs was allocated..

gcc/testsuite/
PR fortran/96312
* gfortran.dg/pr96312.f90: New test.

Comments

Thomas Koenig Aug. 9, 2020, 9:57 a.m. UTC | #1
Hi Paul,

> Regtests OK on FC31/x86_64 - OK for master?

OK. This bug is also exposed by a matmul optimization
that I introduced quite a long time ago, so a backport
would also be good (I can do that if you prefer).

Best regards

	Thomas
diff mbox series

Patch

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index b7c568e90e6..36ff9b5cbc6 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -9936,6 +9936,8 @@  fcncall_realloc_result (gfc_se *se, int rank)
   tree tmp;
   tree offset;
   tree zero_cond;
+  tree not_same_shape;
+  stmtblock_t shape_block;
   int n;
 
   /* Use the allocation done by the library.  Substitute the lhs
@@ -9965,7 +9967,11 @@  fcncall_realloc_result (gfc_se *se, int rank)
   tmp = gfc_conv_descriptor_data_get (res_desc);
   gfc_conv_descriptor_data_set (&se->post, desc, tmp);
 
-  /* Check that the shapes are the same between lhs and expression.  */
+  /* Check that the shapes are the same between lhs and expression.
+     The evaluation of the shape is done in 'shape_block' to avoid
+     unitialized warnings from the lhs bounds. */
+  not_same_shape = boolean_false_node;
+  gfc_start_block (&shape_block);
   for (n = 0 ; n < rank; n++)
     {
       tree tmp1;
@@ -9982,15 +9988,24 @@  fcncall_realloc_result (gfc_se *se, int rank)
       tmp = fold_build2_loc (input_location, NE_EXPR,
 			     logical_type_node, tmp,
 			     gfc_index_zero_node);
-      tmp = gfc_evaluate_now (tmp, &se->post);
-      zero_cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
-				   logical_type_node, tmp,
-				   zero_cond);
+      tmp = gfc_evaluate_now (tmp, &shape_block);
+      if (n == 0)
+	not_same_shape = tmp;
+      else
+	not_same_shape = fold_build2_loc (input_location, TRUTH_OR_EXPR,
+					  logical_type_node, tmp,
+					  not_same_shape);
     }
 
   /* 'zero_cond' being true is equal to lhs not being allocated or the
      shapes being different.  */
-  zero_cond = gfc_evaluate_now (zero_cond, &se->post);
+  tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
+			 zero_cond, not_same_shape);
+  gfc_add_modify (&shape_block, zero_cond, tmp);
+  tmp = gfc_finish_block (&shape_block);
+  tmp = build3_v (COND_EXPR, zero_cond,
+		  build_empty_stmt (input_location), tmp);
+  gfc_add_expr_to_block (&se->post, tmp);
 
   /* Now reset the bounds returned from the function call to bounds based
      on the lhs lbounds, except where the lhs is not allocated or the shapes