Bug 12841 - passing null to a subroutine
passing null to a subroutine
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
tree-ssa
: P2 normal
: 4.0.0
Assigned To: Not yet assigned to anyone
: patch, rejects-valid
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2003-10-30 12:30 UTC by Joost VandeVondele
Modified: 2004-06-16 13:51 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-04-26 03:57:01


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joost VandeVondele 2003-10-30 12:30:56 UTC
passing NULL() to subroutines is allowed 

   MODULE T
   PUBLIC :: A
   CONTAINS
   SUBROUTINE A(B)
   REAL, POINTER :: B
   IF (ASSOCIATED(B)) CALL ABORT()
   END SUBROUTINE A
   END MODULE T
   USE T
   CALL A(NULL())
   END
Comment 1 Andrew Pinski 2004-01-05 19:14:15 UTC
Here is the error message:
 In file pr12841.f90:11

   CALL A(NULL())
         1
Error: Type/rank mismatch in argument 'b' at (1)
Comment 2 Tobias Schlüter 2004-05-31 22:30:32 UTC
The compile time error can be cured by the appended patch, which effectively
disables type/rank checking for the NULL pointer.

This fixes the bug, but the testcase exposes another bug, it still doesn't work
as apparently some confusion between pass-by-reference and pass-by-value occurs
(the same as in PR 15754): The modified testcase
   MODULE T
   PUBLIC :: A
   CONTAINS
   SUBROUTINE A(B)
   REAL, POINTER :: B
   B => NULL() ! <--------------------------- line added
   IF (ASSOCIATED(B)) CALL ABORT()
   END SUBROUTINE A
   END MODULE T
   USE T
   CALL A(NULL())
   END
works (i.e. it doesn't abort at runtime), whereas the original doesn't work.

Index: interface.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fortran/interface.c,v
retrieving revision 1.4
diff -u -p -r1.4 interface.c
--- interface.c 27 May 2004 12:35:12 -0000      1.4
+++ interface.c 31 May 2004 22:10:14 -0000
@@ -1096,7 +1096,8 @@ compare_parameter (gfc_symbol * formal,
       return compare_interfaces (formal, actual->symtree->n.sym, 0);
     }
  
-  if (!gfc_compare_types (&formal->ts, &actual->ts))
+  if (actual->expr_type != EXPR_NULL
+      && !gfc_compare_types (&formal->ts, &actual->ts))
     return 0;
  
   if (symbol_rank (formal) == actual->rank)
@@ -1235,7 +1236,8 @@ compare_actual_formal (gfc_actual_arglis
          return 0;
        }
  
-      if (compare_pointer (f->sym, a->expr) == 0)
+      if (a->expr->expr_type != EXPR_NULL
+         && compare_pointer (f->sym, a->expr) == 0)
        {
          if (where)
            gfc_error ("Actual argument for '%s' must be a pointer at %L",
Comment 3 Tobias Schlüter 2004-05-31 22:45:38 UTC
I found the place to fix the second bug. Patch here:
http://gcc.gnu.org/ml/fortran/2004-05/msg00408.html
Comment 4 CVS Commits 2004-06-12 14:06:26 UTC
Subject: Bug 12841

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	tobi@gcc.gnu.org	2004-06-12 14:06:19

Modified files:
	gcc/fortran    : ChangeLog trans-decl.c trans-expr.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gfortran.fortran-torture/execute: nullarg.f90 

Log message:
	fortran/
	PR fortran/12841
	* interface.c (compare_parameter, compare_actual_formal): Don't
	check types and array shapes for NULL()
	* trans-expr.c (conv_function_call): No double indirection for
	NULL()
	( I had accidentally committed the interface.c part before)
	
	testuite/
	PR fortran/12841
	* gfortran.fortran-torture/execute/null_arg.f90: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/ChangeLog.diff?cvsroot=gcc&r1=1.65&r2=1.66
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/trans-decl.c.diff?cvsroot=gcc&r1=1.11&r2=1.12
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/trans-expr.c.diff?cvsroot=gcc&r1=1.9&r2=1.10
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.3845&r2=1.3846
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.fortran-torture/execute/nullarg.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 5 Tobias Schlüter 2004-06-12 14:43:26 UTC
Fixed.