Fortran: NULL pointer dereference checking arguments to ASSOCIATED intrinsic
authorHarald Anlauf <anlauf@gmx.de>
Wed, 30 Mar 2022 20:36:12 +0000 (22:36 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Wed, 30 Mar 2022 20:37:13 +0000 (22:37 +0200)
gcc/fortran/ChangeLog:

PR fortran/100892
* check.cc (gfc_check_associated): Avoid NULL pointer dereference.

gcc/testsuite/ChangeLog:

PR fortran/100892
* gfortran.dg/associated_target_8.f90: New test.

gcc/fortran/check.cc
gcc/testsuite/gfortran.dg/associated_target_8.f90 [new file with mode: 0644]

index fc97bb1..0c2cb50 100644 (file)
@@ -1504,7 +1504,7 @@ gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
      argument of intrinsic inquiry functions.  */
   if (pointer->rank != -1 && !rank_check (target, 0, pointer->rank))
     t = false;
-  if (target->rank > 0)
+  if (target->rank > 0 && target->ref)
     {
       for (i = 0; i < target->rank; i++)
        if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
diff --git a/gcc/testsuite/gfortran.dg/associated_target_8.f90 b/gcc/testsuite/gfortran.dg/associated_target_8.f90
new file mode 100644 (file)
index 0000000..75c2740
--- /dev/null
@@ -0,0 +1,16 @@
+! { dg-do compile }
+! PR fortran/100892 - procedure pointer to function returning array of size n 
+
+module m
+  implicit none
+  procedure(func1), pointer :: my_ptr => null()
+contains
+  subroutine test_sub
+    if (associated (my_ptr, func1)) print *,'associated'
+  end subroutine test_sub
+  function func1 (n)
+    integer, intent(in) :: n
+    real, dimension(n)  :: func1
+    func1 = 0.
+  end function
+end module m