From 8f0aaee5db5dda4c40b358db7fc622695210dd54 Mon Sep 17 00:00:00 2001 From: Thomas Koenig Date: Sun, 11 May 2008 20:28:52 +0000 Subject: [PATCH] re PR fortran/35719 (pointer to zero sized array not associated) 2008-05-11 Thomas Koenig PR fortran/35719 * trans.c (gfc_call_malloc): If size equals zero, allocate one byte; don't return a null pointer. 2008-05-11 Thomas Koenig PR fortran/35719 * gfortran.dg/associated_5.f90: New test. From-SVN: r135187 --- gcc/fortran/ChangeLog | 6 ++++++ gcc/fortran/trans.c | 16 +++++++--------- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gfortran.dg/associated_5.f90 | 19 +++++++++++++++++++ 4 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/associated_5.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index e635842f9c5..ae78f0ea8af 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,9 @@ +2008-05-11 Thomas Koenig + + PR fortran/35719 + * trans.c (gfc_call_malloc): If size equals zero, allocate one + byte; don't return a null pointer. + 2008-05-10 Francois-Xavier Coudert PR fortran/36197 diff --git a/gcc/fortran/trans.c b/gcc/fortran/trans.c index a9951e48c57..f303128a28d 100644 --- a/gcc/fortran/trans.c +++ b/gcc/fortran/trans.c @@ -440,12 +440,12 @@ gfc_trans_runtime_check (tree cond, stmtblock_t * pblock, locus * where, /* Call malloc to allocate size bytes of memory, with special conditions: + if size < 0, generate a runtime error, - + if size == 0, return a NULL pointer, + + if size == 0, return a malloced area of size 1, + if malloc returns NULL, issue a runtime error. */ tree gfc_call_malloc (stmtblock_t * block, tree type, tree size) { - tree tmp, msg, negative, zero, malloc_result, null_result, res; + tree tmp, msg, negative, malloc_result, null_result, res; stmtblock_t block2; size = gfc_evaluate_now (size, block); @@ -468,6 +468,10 @@ gfc_call_malloc (stmtblock_t * block, tree type, tree size) /* Call malloc and check the result. */ gfc_start_block (&block2); + + size = fold_build2 (MAX_EXPR, size_type_node, size, + build_int_cst (size_type_node, 1)); + gfc_add_modify_expr (&block2, res, build_call_expr (built_in_decls[BUILT_IN_MALLOC], 1, size)); @@ -481,13 +485,7 @@ gfc_call_malloc (stmtblock_t * block, tree type, tree size) gfc_add_expr_to_block (&block2, tmp); malloc_result = gfc_finish_block (&block2); - /* size == 0 */ - zero = fold_build2 (EQ_EXPR, boolean_type_node, size, - build_int_cst (size_type_node, 0)); - tmp = fold_build2 (MODIFY_EXPR, pvoid_type_node, res, - build_int_cst (pvoid_type_node, 0)); - tmp = fold_build3 (COND_EXPR, void_type_node, zero, tmp, malloc_result); - gfc_add_expr_to_block (block, tmp); + gfc_add_expr_to_block (block, malloc_result); if (type != NULL) res = fold_convert (type, res); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 97b447fda2e..50143ff8ed7 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2008-05-11 Thomas Koenig + + PR fortran/35719 + * gfortran.dg/associated_5.f90: New test. + 2008-05-11 Richard Sandiford * gcc.target/mips/scc-1.c: Require mips16_attribute, and add diff --git a/gcc/testsuite/gfortran.dg/associated_5.f90 b/gcc/testsuite/gfortran.dg/associated_5.f90 new file mode 100644 index 00000000000..a2007752f3c --- /dev/null +++ b/gcc/testsuite/gfortran.dg/associated_5.f90 @@ -0,0 +1,19 @@ +! { dg-do run } +! PR 35719 - associated used to fail with zero-sized automatic arrays +! Test case contributed by Dick Hendrickson + + program try_mf1053 + + call mf1053 ( 1, 2, 3, 4) + end + + SUBROUTINE MF1053 (nf1, nf2, nf3, nf4) + INTEGER, pointer :: ptr(:,:) + INTEGER, target :: ILA1(NF2,NF4:NF3) + + ptr => ILA1 + + if (ASSOCIATED (ptr, ILA1(NF1:NF2,NF4:NF3) ) ) call abort + if ( .not. ASSOCIATED(ptr) ) call abort + + END SUBROUTINE -- 2.30.2