/* 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);
/* 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));
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);
--- /dev/null
+! { 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