]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: fix NULL pointer dereference on overlapping initialization [PR50410]
authorHarald Anlauf <anlauf@gmx.de>
Thu, 28 Mar 2024 21:34:40 +0000 (22:34 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 29 Mar 2024 09:31:38 +0000 (10:31 +0100)
gcc/fortran/ChangeLog:

PR fortran/50410
* trans-expr.cc (gfc_conv_structure): Check for NULL pointer.

gcc/testsuite/ChangeLog:

PR fortran/50410
* gfortran.dg/data_initialized_4.f90: New test.

gcc/fortran/trans-expr.cc
gcc/testsuite/gfortran.dg/data_initialized_4.f90 [new file with mode: 0644]

index 079ac93aa8a47954f2e12bb90a74232efb520641..d21e3956d6e7aae7e057e8d27f64bd5b687bf261 100644 (file)
@@ -9650,7 +9650,7 @@ gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
   cm = expr->ts.u.derived->components;
 
   for (c = gfc_constructor_first (expr->value.constructor);
-       c; c = gfc_constructor_next (c), cm = cm->next)
+       c && cm; c = gfc_constructor_next (c), cm = cm->next)
     {
       /* Skip absent members in default initializers and allocatable
         components.  Although the latter have a default initializer
diff --git a/gcc/testsuite/gfortran.dg/data_initialized_4.f90 b/gcc/testsuite/gfortran.dg/data_initialized_4.f90
new file mode 100644 (file)
index 0000000..156b660
--- /dev/null
@@ -0,0 +1,16 @@
+! { dg-do compile }
+! { dg-additional-options "-std=legacy" }
+!
+! PR fortran/50410
+!
+! Silently allow overlapping initialization in legacy mode (used to ICE)
+
+program p
+  implicit none
+  type t
+     integer :: g = 1
+  end type t
+  type(t) :: u = t(2)
+  data u%g /3/
+  print *, u    ! this might print "2"
+end