Fortran: NULL pointer dereference in gfc_simplify_image_index [PR104330]
authorSteve Kargl <kargl@gcc.gnu.org>
Mon, 17 Oct 2022 20:42:40 +0000 (22:42 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Mon, 17 Oct 2022 20:44:59 +0000 (22:44 +0200)
gcc/fortran/ChangeLog:

PR fortran/104330
* simplify.cc (gfc_simplify_image_index): Do not dereference NULL
pointer.

gcc/testsuite/ChangeLog:

PR fortran/104330
* gfortran.dg/pr104330.f90: New test.

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

index 6ac92cf..9c2fea8 100644 (file)
@@ -8266,7 +8266,7 @@ gfc_simplify_image_index (gfc_expr *coarray, gfc_expr *sub)
     if (ref->type == REF_COMPONENT)
       as = ref->u.ar.as;
 
-  if (as->type == AS_DEFERRED)
+  if (!as || as->type == AS_DEFERRED)
     return NULL;
 
   /* "valid sequence of cosubscripts" are required; thus, return 0 unless
diff --git a/gcc/testsuite/gfortran.dg/pr104330.f90 b/gcc/testsuite/gfortran.dg/pr104330.f90
new file mode 100644 (file)
index 0000000..9ff48e2
--- /dev/null
@@ -0,0 +1,20 @@
+! { dg-do compile }
+! { dg-options "-fcoarray=lib" }
+!
+! PR fortran/104330 - ICE in gfc_simplify_image_index
+! Contributed by G.Steinmetz
+
+program p
+  implicit none
+  type t
+  end type t
+  class(*), allocatable :: x[:]
+  class(t), allocatable :: y[:]
+  type(t),  allocatable :: z[:]
+  allocate (real :: x[*])
+  print *, image_index(x, [1])
+  allocate (t :: y[*])
+  print *, image_index(y, [1])
+  allocate (t :: z[*])
+  print *, image_index(z, [1])
+end