This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch, fortran] Fix PR 35719
- From: Thomas Koenig <tkoenig at netcologne dot de>
- To: fortran at gcc dot gnu dot org
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Wed, 07 May 2008 21:25:40 +0200
- Subject: [patch, fortran] Fix PR 35719
Hello world,
the bug, discovered by Dick Hendrickson, was caused by using a null
pointer as return value when trying to malloc 0 bytes. This caused the
ASSOCIATED function to fail when applied to a pointer to an automatic
array. The approach in this patch is to allocate at least one byte
for each malloc, so the comparison of the pointers can be meaningful.
Regression-tested on i686-pc-linux-gnu. OK for trunk?
Thomas
2008-05-07 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-07 Thomas Koenig <tkoenig@gcc.gnu.org>
PR fortran/35719
* gfortran.dg/associated_5.f90: New test.
Index: trans.c
===================================================================
--- trans.c (revision 134952)
+++ trans.c (working copy)
@@ -440,12 +440,12 @@ gfc_trans_runtime_check (tree cond, stmt
/* 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, tr
/* 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, tr
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);
! { 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