Hello All, This is the third of the three patches to fix bugs found by Jerry/Claude.
The majority of the testcase is fixed by the second patch. However, the intent_out subroutine, 's_out', was causing a segfault because the vptr def_init field was NULL. Preventing the NULL from being passed to the vptr copy function cleanly fixes the bug. Regression tests OK on FC45/x86_64 - OK for mainline? Cheers Paul
From 53a66753d426de0b2739572d53b1aa6ed2dc65f0 Mon Sep 17 00:00:00 2001 From: Paul Thomas <[email protected]> Date: Fri, 20 Nov 2026 09:59:53 +0000 Subject: [PATCH] Fortran: Invalid memory reference with class PDT intent out [PR128886] 2026-11-29 Paul Thomas <[email protected]> gcc/fortran PR fortran/128886 * trans-expr.cc (gfc_trans_class_array_init_assign): The check for a NULL rhs is needed for class PDTs as well as unlimited polymorphis expressions. gcc/testsuite/ PR fortran/128886 * gfortran.dg/pdt_bug31.f90: New test. --- gcc/fortran/trans-expr.cc | 2 +- gcc/testsuite/gfortran.dg/pdt_bug31.f90 | 66 +++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug31.f90 diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index 390089b45ed..695afaec225 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -1791,7 +1791,7 @@ gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj) res = gfc_trans_call (ppc_code, false, NULL, NULL, false); gfc_free_statements (ppc_code); - if (UNLIMITED_POLY(obj)) + if (UNLIMITED_POLY(obj) || IS_CLASS_PDT (obj)) { /* Check if rhs is non-NULL. */ gfc_se src; diff --git a/gcc/testsuite/gfortran.dg/pdt_bug31.f90 b/gcc/testsuite/gfortran.dg/pdt_bug31.f90 new file mode 100644 index 00000000000..71fa758d30d --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pdt_bug31.f90 @@ -0,0 +1,66 @@ +! { dg-do run } +! +! CLASS PDT dummy arrays: Primary problem was a repeated typo in trans-decl.cc. +! +! Subroutine 's_out' needed special attention because the vptr default initializer +! was NULL. +! +! Contributed by Jerry Delisle <[email protected]> +! +module m + implicit none + type t(n) + integer, len :: n = 2 + integer :: x(n) + integer :: tag = 42 + end type +contains + subroutine s_shape (a) + class(t(n=3)), intent(in) :: a(:) + if (sum (a%x(2)) /= 7) stop 1 + end subroutine + subroutine s_expl (a) + class(t(n=3)), intent(in) :: a(2) + if (sum (a(1)%x) /= 6) stop 2 + end subroutine + subroutine s_size (a) + class(t(n=3)), intent(in) :: a(*) + if (sum (a(2)%x) /= 15) stop 3 + end subroutine + subroutine s_rank (a, i) + class(t(n=3)), intent(in) :: a(..) + integer, intent(in) :: i + if (rank (a) /= i) stop 4 + end subroutine + subroutine s_alloc (a) + class(t(n=:)), allocatable, intent(inout) :: a(:) + if (.not. allocated (a)) allocate (t(n=3) :: a(2)) + a(1)%x = [11, 12, 13] + a(2)%x = [14, 15, 16] + end subroutine + subroutine s_out (a) + class(t(n=3)), intent(out) :: a(:) + if ((size (a, 1) /= 2) .or. & ! Dimension of a retained + (sum (a(1)%x) /= 36) .or. & ! Size and contents of pdt_array retained + a(2)%tag /= 42) stop 5 ! Not pdt component set to initalizer. + end subroutine +end module + + use m + class(t(n=3)), allocatable :: x(:), y(:,:) + allocate (x(2)) + x(1)%x = [1, 2, 3] + x(2)%x = [4, 5, 6] + x%tag = 1 + call s_shape (x) + call s_expl (x) + call s_size (x) + call s_rank (x, 1) + call s_rank (y, 2) + deallocate (x) + call s_alloc (x) + if (sum (x(1)%x) + sum (x(2)%x) /= 81) stop 6 + x%tag = 1 + call s_out (x) + if (allocated (x)) deallocate (x) +end -- 2.56.1
