| Summary: | ALLOCATE of parameterized derived-type array initializes only first element | ||
|---|---|---|---|
| Product: | gcc | Reporter: | Jerry DeLisle <jvdelisle> |
| Component: | fortran | Assignee: | Jerry DeLisle <jvdelisle> |
| Status: | RESOLVED FIXED | ||
| Severity: | normal | CC: | pault |
| Priority: | P3 | Keywords: | wrong-code |
| Version: | 16.0 | ||
| Target Milestone: | --- | ||
| Host: | Target: | ||
| Build: | Known to work: | ||
| Known to fail: | Last reconfirmed: | 2026-06-07 00:00:00 | |
| Bug Depends on: | |||
| Bug Blocks: | 82173, 125515 | ||
The master branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>: https://gcc.gnu.org/g:81a0bec12189136350dbb43969743fab8b87a212 commit r17-1409-g81a0bec12189136350dbb43969743fab8b87a212 Author: Jerry DeLisle <jvdelisle@gcc.gnu.org> Date: Fri Jun 5 13:19:58 2026 -0700 fortran: ALLOCATE of parameterized derived-type array initializes only first element gfc_trans_allocate passed expr->rank to gfc_allocate_pdt_comp and gfc_deallocate_pdt_comp for array allocations of parameterized derived types. For an allocate-shape-spec expression (e.g. "allocate(a(n))"), expr->rank is 0 even though the symbol is an array; the rank is carried on the symbol rather than on the allocate expression. With rank 0 the loop that initialises each element's allocatable components ran zero times, so only element zero was initialised. Subsequent accesses to elements 1..N-1 used garbage pointers and crashed. Fix: when se.expr is a GFC array descriptor, take the rank from the descriptor via GFC_TYPE_ARRAY_RANK instead of from expr->rank. Apply the same correction to the CLASS path and to the matching deallocate calls in gfc_trans_deallocate. Assisted by: Claude Sonnet 4.6 PR fortran/125534 gcc/fortran/ChangeLog: * trans-stmt.cc (gfc_trans_allocate): Use GFC_TYPE_ARRAY_RANK from the GFC descriptor type when se.expr is a descriptor, rather than expr->rank, for the rank passed to gfc_allocate_pdt_comp. Apply the same fix to the CLASS path. (gfc_trans_deallocate): Likewise for gfc_deallocate_pdt_comp. gcc/testsuite/ChangeLog: * gfortran.dg/pdt_array_alloc_1.f90: New test. The releases/gcc-16 branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>: https://gcc.gnu.org/g:5598624357ff22cdf28769550049732f3b7bb000 commit r16-9102-g5598624357ff22cdf28769550049732f3b7bb000 Author: Jerry DeLisle <jvdelisle@gcc.gnu.org> Date: Fri Jun 5 13:19:58 2026 -0700 fortran: ALLOCATE of parameterized derived-type array initializes only first element gfc_trans_allocate passed expr->rank to gfc_allocate_pdt_comp and gfc_deallocate_pdt_comp for array allocations of parameterized derived types. For an allocate-shape-spec expression (e.g. "allocate(a(n))"), expr->rank is 0 even though the symbol is an array; the rank is carried on the symbol rather than on the allocate expression. With rank 0 the loop that initialises each element's allocatable components ran zero times, so only element zero was initialised. Subsequent accesses to elements 1..N-1 used garbage pointers and crashed. Fix: when se.expr is a GFC array descriptor, take the rank from the descriptor via GFC_TYPE_ARRAY_RANK instead of from expr->rank. Apply the same correction to the CLASS path and to the matching deallocate calls in gfc_trans_deallocate. Assisted-by: Claude Sonnet 4.6 PR fortran/125534 gcc/fortran/ChangeLog: * trans-stmt.cc (gfc_trans_allocate): Use GFC_TYPE_ARRAY_RANK from the GFC descriptor type when se.expr is a descriptor, rather than expr->rank, for the rank passed to gfc_allocate_pdt_comp. Apply the same fix to the CLASS path. (gfc_trans_deallocate): Likewise for gfc_deallocate_pdt_comp. gcc/testsuite/ChangeLog: * gfortran.dg/pdt_array_alloc_1.f90: New test. (cherry picked from commit 5f12403a8ff7d2e8a62703aa33496b514cf6af3e) Closing |
When allocating an array of a parameterized derived type (PDT) that has allocatable components, gfortran initializes only the first element. Accessing the allocatable components of any other element uses garbage pointers and crashes. module m implicit none integer, parameter :: default_real = kind(0.) type :: tensor_t(k) integer, kind :: k = default_real real(k), allocatable :: values_(:) end type end module program test use m implicit none integer, parameter :: n = 5 type(tensor_t), allocatable :: a(:) integer :: i allocate (a(n)) do i = 1, n a(i)%values_ = [real(i), real(i*2)] end do if (any (a(3)%values_ /= [3., 6.])) stop 1 end program $ gfortran pdt_array_alloc_1.f90 && ./a.out The program crashes because a(2) through a(n) have uninitialized allocatable component pointers. The program should run and exit cleanly. An ugly one for sure.