From 04a58d29c44977a21090692d3ddeeaf3e92fabff Mon Sep 17 00:00:00 2001 From: Paul Thomas Date: Tue, 14 Nov 2017 17:38:38 +0000 Subject: [PATCH] backport: re PR fortran/82934 (Segfault on assumed character length in allocate) 2017-11-13 Paul Thomas Backport from trunk PR fortran/82934 * trans-stmt.c (gfc_trans_allocate): Remove the gcc_assert on null string length for assumed length typespec and set expr3_esize to NULL_TREE; 2017-11-13 Paul Thomas Backport from trunk PR fortran/82934 * gfortran.dg/allocate_assumed_charlen_1.f90: New test. From-SVN: r254733 --- gcc/fortran/ChangeLog | 8 ++++++ gcc/fortran/trans-stmt.c | 5 ++-- gcc/testsuite/ChangeLog | 6 ++++ .../allocate_assumed_charlen_1.f90 | 28 +++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/allocate_assumed_charlen_1.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 603ed33fc3a4..f278002b90a2 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,11 @@ +2017-11-13 Paul Thomas + + Backport from trunk + PR fortran/82934 + * trans-stmt.c (gfc_trans_allocate): Remove the gcc_assert on + null string length for assumed length typespec and set + expr3_esize to NULL_TREE; + 2017-11-13 Paul Thomas Backport from trunk diff --git a/gcc/fortran/trans-stmt.c b/gcc/fortran/trans-stmt.c index d18e21bdc26e..45510bc51f73 100644 --- a/gcc/fortran/trans-stmt.c +++ b/gcc/fortran/trans-stmt.c @@ -5693,10 +5693,9 @@ gfc_trans_allocate (gfc_code * code) if (code->ext.alloc.ts.type != BT_CHARACTER) expr3_esize = TYPE_SIZE_UNIT ( gfc_typenode_for_spec (&code->ext.alloc.ts)); - else + else if (code->ext.alloc.ts.u.cl->length != NULL) { gfc_expr *sz; - gcc_assert (code->ext.alloc.ts.u.cl->length != NULL); sz = gfc_copy_expr (code->ext.alloc.ts.u.cl->length); gfc_init_se (&se_sz, NULL); gfc_conv_expr (&se_sz, sz); @@ -5710,6 +5709,8 @@ gfc_trans_allocate (gfc_code * code) tmp, se_sz.expr); expr3_esize = gfc_evaluate_now (expr3_esize, &block); } + else + expr3_esize = NULL_TREE; } /* Loop over all objects to allocate. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index fd9561b184c8..df7913a0ae41 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2017-11-13 Paul Thomas + + Backport from trunk + PR fortran/82934 + * gfortran.dg/allocate_assumed_charlen_1.f90: New test. + 2017-11-13 Paul Thomas Backport from trunk diff --git a/gcc/testsuite/gfortran.dg/allocate_assumed_charlen_1.f90 b/gcc/testsuite/gfortran.dg/allocate_assumed_charlen_1.f90 new file mode 100644 index 000000000000..382df36375d5 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/allocate_assumed_charlen_1.f90 @@ -0,0 +1,28 @@ +! { dg-do run } +! +! PR82934: Segfault on compilation in trans-stmt.c:5919(8.0.0). +! The original report only had one item in the allocate list. This +! has been doubled up to verify that the correct string length is +! is used in the allocation. +! +! Contributed by FortranFan on clf. +! + character(len=42), allocatable :: foo + character(len=22), allocatable :: foofoo + + call alloc( foo , foofoo) + + if (len(foo) .ne. 42) call abort + if (len(foofoo) .ne. 22) call abort + +contains + + subroutine alloc( bar, barbar ) + + character(len=*), allocatable :: bar, barbar + + allocate( character(len=*) :: bar , barbar) ! <= Here! + + end subroutine + +end -- 2.47.2