aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Koenig <tkoenig@gcc.gnu.org>2008-05-11 20:28:52 +0000
committerThomas Koenig <tkoenig@gcc.gnu.org>2008-05-11 20:28:52 +0000
commit8f0aaee5db5dda4c40b358db7fc622695210dd54 (patch)
tree513f449bdb1b4bb3020f0ed10b74d5e0e4289e74
parent8ffb4ce0b0e6c2a9cc70d5ecc60d07951ba40f9c (diff)
downloadgcc-8f0aaee5db5dda4c40b358db7fc622695210dd54.zip
gcc-8f0aaee5db5dda4c40b358db7fc622695210dd54.tar.gz
gcc-8f0aaee5db5dda4c40b358db7fc622695210dd54.tar.bz2
re PR fortran/35719 (pointer to zero sized array not associated)
2008-05-11 Thomas Koenig <tkoenig@gcc.gnu.org> 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 <tkoenig@gcc.gnu.org> PR fortran/35719 * gfortran.dg/associated_5.f90: New test. From-SVN: r135187
-rw-r--r--gcc/fortran/ChangeLog6
-rw-r--r--gcc/fortran/trans.c16
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/gfortran.dg/associated_5.f9019
4 files changed, 37 insertions, 9 deletions
diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog
index e635842..ae78f0e 100644
--- a/gcc/fortran/ChangeLog
+++ b/gcc/fortran/ChangeLog
@@ -1,3 +1,9 @@
+2008-05-11 Thomas Koenig <tkoenig@gcc.gnu.org>
+
+ 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 <fxcoudert@gcc.gnu.org>
PR fortran/36197
diff --git a/gcc/fortran/trans.c b/gcc/fortran/trans.c
index a9951e4..f303128 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 97b447f..50143ff 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2008-05-11 Thomas Koenig <tkoenig@gcc.gnu.org>
+
+ PR fortran/35719
+ * gfortran.dg/associated_5.f90: New test.
+
2008-05-11 Richard Sandiford <rdsandiford@googlemail.com>
* 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 0000000..a200775
--- /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