https://gcc.gnu.org/g:1611acc1f72cad30c7ecccb5c85246836c1d0299

commit r11-11299-g1611acc1f72cad30c7ecccb5c85246836c1d0299
Author: Harald Anlauf <anl...@gmx.de>
Date:   Thu Mar 28 22:34:40 2024 +0100

    Fortran: fix NULL pointer dereference on overlapping initialization 
[PR50410]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/50410
            * trans-expr.c (gfc_conv_structure): Check for NULL pointer.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/50410
            * gfortran.dg/data_initialized_4.f90: New test.
    
    (cherry picked from commit 6fb253a25dff13253d63553f02e0fe72c5e3ab4e)

Diff:
---
 gcc/fortran/trans-expr.c                         |  2 +-
 gcc/testsuite/gfortran.dg/data_initialized_4.f90 | 16 ++++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 5adee114157..37f16f37e12 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -8879,7 +8879,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
index 00000000000..156b6607edf
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/data_initialized_4.f90
@@ -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

Reply via email to