re PR fortran/71623 (Segfault when allocating deferred-length characters to size...
authorAndre Vehreschild <vehre@gcc.gnu.org>
Tue, 5 Jul 2016 12:06:22 +0000 (14:06 +0200)
committerAndre Vehreschild <vehre@gcc.gnu.org>
Tue, 5 Jul 2016 12:06:22 +0000 (14:06 +0200)
gcc/fortran/ChangeLog:

2016-07-05  Andre Vehreschild  <vehre@gcc.gnu.org>

PR fortran/71623
* trans-stmt.c (gfc_trans_allocate): Add code of pre block of typespec
in allocate to parent block.

gcc/testsuite/ChangeLog:

2016-07-05  Andre Vehreschild  <vehre@gcc.gnu.org>

PR fortran/71623
* gfortran.dg/deferred_character_17.f90: New test.

From-SVN: r238002

gcc/fortran/ChangeLog
gcc/fortran/trans-stmt.c
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/deferred_character_17.f90 [new file with mode: 0644]

index f833b97bb642f1d96f0a39ca2315474308bbcb34..0947d6dce733b54cebc8f8f84a48c8a4e6def536 100644 (file)
@@ -1,3 +1,9 @@
+2016-07-05  Andre Vehreschild  <vehre@gcc.gnu.org>
+
+       PR fortran/71623
+       * trans-stmt.c (gfc_trans_allocate): Add code of pre block of typespec
+       in allocate to parent block.
+
 2016-07-04  Jerry DeLisle  <jvdelisle@gcc.gnu.org>
 
        PR fortran/66575
index 84bf749780d616c43673d3ccafba3c09e66f8f68..5aa77789b1490c7d897984a1e440f2a54631e3d5 100644 (file)
@@ -5696,9 +5696,11 @@ gfc_trans_allocate (gfc_code * code)
          tmp = gfc_get_char_type (code->ext.alloc.ts.kind);
          tmp = TYPE_SIZE_UNIT (tmp);
          tmp = fold_convert (TREE_TYPE (se_sz.expr), tmp);
+         gfc_add_block_to_block (&block, &se_sz.pre);
          expr3_esize = fold_build2_loc (input_location, MULT_EXPR,
                                         TREE_TYPE (se_sz.expr),
                                         tmp, se_sz.expr);
+         expr3_esize = gfc_evaluate_now (expr3_esize, &block);
        }
     }
 
@@ -5897,6 +5899,7 @@ gfc_trans_allocate (gfc_code * code)
                 source= or mold= expression.  */
              gfc_init_se (&se_sz, NULL);
              gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
+             gfc_add_block_to_block (&block, &se_sz.pre);
              gfc_add_modify (&block, al_len,
                              fold_convert (TREE_TYPE (al_len),
                                            se_sz.expr));
@@ -5981,11 +5984,19 @@ gfc_trans_allocate (gfc_code * code)
                 specified by a type spec for deferred length character
                 arrays or unlimited polymorphic objects without a
                 source= or mold= expression.  */
-             gfc_init_se (&se_sz, NULL);
-             gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
-             gfc_add_modify (&block, al_len,
-                             fold_convert (TREE_TYPE (al_len),
-                                           se_sz.expr));
+             if (expr3_esize == NULL_TREE || code->ext.alloc.ts.kind != 1)
+               {
+                 gfc_init_se (&se_sz, NULL);
+                 gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
+                 gfc_add_block_to_block (&block, &se_sz.pre);
+                 gfc_add_modify (&block, al_len,
+                                 fold_convert (TREE_TYPE (al_len),
+                                               se_sz.expr));
+               }
+             else
+               gfc_add_modify (&block, al_len,
+                               fold_convert (TREE_TYPE (al_len),
+                                             expr3_esize));
            }
          else
            /* No length information needed, because type to allocate
index 8af6df819aec5a7f89035db1d6405f79cb506ec3..2db9b450df9fc90c89aa766eb32b652a82aa9a76 100644 (file)
@@ -1,3 +1,8 @@
+2016-07-05  Andre Vehreschild  <vehre@gcc.gnu.org>
+
+       PR fortran/71623
+       * gfortran.dg/deferred_character_17.f90: New test.
+
 2016-07-05  Christophe Lyon  <christophe.lyon@linaro.org>
 
        * gcc.target/arm/neon/polytypes.c: Move to ...
diff --git a/gcc/testsuite/gfortran.dg/deferred_character_17.f90 b/gcc/testsuite/gfortran.dg/deferred_character_17.f90
new file mode 100644 (file)
index 0000000..5a9d725
--- /dev/null
@@ -0,0 +1,13 @@
+!{ dg-do run }
+
+! Check fix for PR fortran/71623
+
+program allocatemvce
+  implicit none
+  character(len=:), allocatable :: string
+  integer, dimension(4), target :: array = [1,2,3,4]
+  integer, dimension(:), pointer :: array_ptr
+  array_ptr => array
+  ! The allocate used to segfault
+  allocate(character(len=size(array_ptr))::string)
+end program allocatemvce