https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127941

            Bug ID: 127941
           Summary: Invalid free when deallocating polymorphic object with
                    fixed-size array of derived types containing nested
                    allocatable components
           Product: gcc
           Version: 16.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gordeygoyman at gmail dot com
  Target Milestone: ---

**Product:** gcc  
**Component:** fortran  
**Version:** 16.1.1 (also affects 15.3.0)  
**Severity:** normal  
**Keywords:** wrong-code  
**Known to fail:** 15.3.0, 16.1.1

### Description

GNU Fortran generates incorrect deallocation code for a polymorphic allocatable
variable whose declared type contains a fixed-size array of derived types with
nested allocatable components.

The program below compiles successfully but aborts at runtime with `free():
invalid pointer` during `DEALLOCATE(x)`.

The error occurs even when the nested allocatable components have never been
allocated. Changing only the declaration of `x` from `CLASS(container)` to
`TYPE(container)` eliminates the failure.

### Minimal reproducer

```fortran
program repro
    type leaf
        real, allocatable :: value
    end type
    type item
        integer :: tag = 1
        type(leaf), allocatable :: children(:)
    end type
    type container
        type(item) :: entries(1)
    end type
    class(container), allocatable :: x
    allocate(x)
    deallocate(x)
end program
```

### Steps to reproduce

```bash
gfortran -std=f2018 -pedantic-errors -fcheck=all repro.f90 -o repro
./repro
```

### Actual behavior

Compilation succeeds, but execution aborts during `DEALLOCATE(x)`:

```text
free(): invalid pointer
Aborted (core dumped)
```

The process terminates with SIGABRT (exit status 134).

### Expected behavior

The program should terminate normally. The allocated polymorphic object has
dynamic type `container`, and all nested allocatable components are
unallocated.

No invalid deallocation should occur.

### Additional observations

- Reproduced with GNU Fortran 15.3.0 and 16.1.1 (20260728) on Linux x86_64.
- Replacing `CLASS(container), ALLOCATABLE` with `TYPE(container), ALLOCATABLE`
makes the test pass with both GNU Fortran versions.
- Replacing the fixed-size `entries(1)` array with a scalar component, or
removing the allocatable component from `leaf`, also avoids the failure.
- The failure persists when `children(1)` and its scalar `value` component are
explicitly allocated before deallocating `x`.


### Potentially related reports

- PR fortran/56218: Segfault with allocatable `INTENT(OUT)` derived type
argument having an allocatable component.
- PR fortran/92703: `VALUE` attribute handling for `CLASS` and derived types
with allocatable components.
- Historical patch: "Fix deallocation of nested derived typed components"
(December 2016): https://gcc.gnu.org/legacy-ml/fortran/2016-12/msg00009.html

These appear related but do not describe the same minimal reproducer.

Reply via email to