[PR,fortran/87045] - pointer to array of character

Message ID 5C857F47.5040706@gmx.de
State New
Headers show
Series
  • [PR,fortran/87045] - pointer to array of character
Related show

Commit Message

Harald Anlauf March 10, 2019, 9:19 p.m.
The code in the testcase died due to a run-time bounds-check
generated slightly too early, leading to a crash for deferred
character length.  Moving the character length check after the
assignment solves the issue.

Regtests cleanly on x86_64-pc-linux-gnu.

OK for trunk?

Thanks,
Harald

2019-03-10  Harald Anlauf  <anlauf@gmx.de>

	PR fortran/87045
	* trans-expr.c (gfc_trans_pointer_assignment): Move check for same
	string length so that we do not get false errors for deferred
	length.

2019-03-10  Harald Anlauf  <anlauf@gmx.de>

	PR fortran/87045
	* gfortran.dg/pr87045.f90: New test.

Index: gcc/testsuite/gfortran.dg/pr87045.f90
===================================================================
--- gcc/testsuite/gfortran.dg/pr87045.f90	(nonexistent)
+++ gcc/testsuite/gfortran.dg/pr87045.f90	(working copy)
@@ -0,0 +1,19 @@
+! { dg-do run }
+! { dg-additional-options "-fcheck=bounds" }
+!
+! PR fortran/87045 - pointer to array of character
+! Contributed by Valery Weber
+! This used to give an invalid run-time error
+
+program test
+  character(:), dimension(:), allocatable, target :: t
+  character(:), pointer, dimension(:) :: p
+  allocate( character(3) :: t(2) )
+  t(1) = "abc"
+  t(2) = "123"
+  p => t
+  if (size (p) /= 2) stop 1
+  if (len  (p) /= 3) stop 2
+  if (p(1) /= "abc") stop 3
+  if (p(2) /= "123") stop 4
+end program test

Comments

Steve Kargl March 13, 2019, 12:41 a.m. | #1
On Sun, Mar 10, 2019 at 10:19:03PM +0100, Harald Anlauf wrote:
> The code in the testcase died due to a run-time bounds-check

> generated slightly too early, leading to a crash for deferred

> character length.  Moving the character length check after the

> assignment solves the issue.

> 

> Regtests cleanly on x86_64-pc-linux-gnu.

> 

> OK for trunk?

> 


OK.

-- 
Steve
Harald Anlauf March 13, 2019, 9:39 p.m. | #2
Committed as r269664.

Thanks for the review!

Harald

On 03/13/19 01:41, Steve Kargl wrote:
> On Sun, Mar 10, 2019 at 10:19:03PM +0100, Harald Anlauf wrote:

>> The code in the testcase died due to a run-time bounds-check

>> generated slightly too early, leading to a crash for deferred

>> character length.  Moving the character length check after the

>> assignment solves the issue.

>>

>> Regtests cleanly on x86_64-pc-linux-gnu.

>>

>> OK for trunk?

>>

> 

> OK.

>

Patch

Index: gcc/fortran/trans-expr.c
===================================================================
--- gcc/fortran/trans-expr.c	(revision 269560)
+++ gcc/fortran/trans-expr.c	(working copy)
@@ -9199,16 +9199,6 @@ 
 	    }
 	}
 
-      /* Check string lengths if applicable.  The check is only really added
-	 to the output code if -fbounds-check is enabled.  */
-      if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
-	{
-	  gcc_assert (expr2->ts.type == BT_CHARACTER);
-	  gcc_assert (strlen_lhs && strlen_rhs);
-	  gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
-				       strlen_lhs, strlen_rhs, &block);
-	}
-
       /* If rank remapping was done, check with -fcheck=bounds that
 	 the target is at least as large as the pointer.  */
       if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
@@ -9243,6 +9233,16 @@ 
 	    gfc_add_modify (&block, tmp, build_zero_cst (TREE_TYPE (tmp)));
 	}
 
+      /* Check string lengths if applicable.  The check is only really added
+	 to the output code if -fbounds-check is enabled.  */
+      if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
+	{
+	  gcc_assert (expr2->ts.type == BT_CHARACTER);
+	  gcc_assert (strlen_lhs && strlen_rhs);
+	  gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
+				       strlen_lhs, strlen_rhs, &block);
+	}
+
       gfc_add_block_to_block (&block, &lse.post);
       if (rank_remap)
 	gfc_add_block_to_block (&block, &rse.post);