See attached patch. A DATA statement that initializes a pointer to an
array element, e.g. data p /t(3)/, segfaults in
gfc_conv_scalarized_array_ref. resolve_data resolves the variables of
a DATA statement but not its values, so t(3) is still an AR_UNKNOWN
reference when it reaches code generation, where it takes the
scalarized path with no scalarizer. The patch calls gfc_resolve_expr
on each value in resolve_data before the values are matched to the
variables.

The fix is in resolve_data in resolve.cc; the rest is the new test. The
bug was found with the Fujitsu compiler test suite; the test is new.

Regression tested on x86_64.

OK for mainline?

Regards,

Jerry

---

fortran: [PR125762] Fix segfault for DATA pointer init to an array element

Resolve the value expressions of a DATA statement in resolve_data.

Assisted-by: Claude Opus 5.5

        PR fortran/125762

gcc/fortran/ChangeLog:

        * resolve.cc (resolve_data): Call gfc_resolve_expr on each value
        expression.

gcc/testsuite/ChangeLog:

        * gfortran.dg/data_pointer_4.f90: New test.
>From f04ab44c4062e3968715a28039a1b088f263d2f7 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Thu, 11 Jun 2026 18:17:46 -0700
Subject: [PATCH] fortran: [PR125762] Fix segfault for DATA pointer init to an
 array element

Resolve the value expressions of a DATA statement in resolve_data.

Assisted-by: Claude Opus 5.5

	PR fortran/125762

gcc/fortran/ChangeLog:

	* resolve.cc (resolve_data): Call gfc_resolve_expr on each value
	expression.

gcc/testsuite/ChangeLog:

	* gfortran.dg/data_pointer_4.f90: New test.
---
 gcc/fortran/resolve.cc                       |  5 ++++
 gcc/testsuite/gfortran.dg/data_pointer_4.f90 | 25 ++++++++++++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/data_pointer_4.f90

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 67eaea64c9b..29247f4ec2d 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -20025,10 +20025,15 @@ resolve_data_variables (gfc_data_variable *d)
 static void
 resolve_data (gfc_data *d)
 {
+  gfc_data_value *v;
 
   if (!resolve_data_variables (d->var))
     return;
 
+  for (v = d->value; v; v = v->next)
+    if (v->expr && !gfc_resolve_expr (v->expr))
+      return;
+
   values.vnode = d->value;
   if (d->value == NULL)
     mpz_set_ui (values.left, 0);
diff --git a/gcc/testsuite/gfortran.dg/data_pointer_4.f90 b/gcc/testsuite/gfortran.dg/data_pointer_4.f90
new file mode 100644
index 00000000000..6f6d947da15
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/data_pointer_4.f90
@@ -0,0 +1,25 @@
+! { dg-do run }
+!
+! PR 125762
+!
+! A DATA statement initializing a pointer to an array element (or array-element
+! substring) target segfaulted in gfc_conv_scalarized_array_ref: the element
+! reference reached gfc_conv_array_ref with an unresolved type (AR_UNKNOWN) and
+! no scalarizer, so the scalarized path was wrongly taken.
+
+program p
+  integer, target :: t(10) = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+  integer, pointer :: p1, p2
+
+  character(5), target :: s(2) = ["abcde", "fghij"]
+  character(2), pointer :: cp
+
+  data p1 /t(3)/
+  data p2 /t(7)/
+  data cp /s(2)(2:3)/
+
+  if (p1 /= 3) stop 1
+  if (.not. associated (p1, t(3))) stop 2
+  if (p2 /= 7) stop 3
+  if (cp /= "gh") stop 4
+end program p
-- 
2.56.0

Reply via email to