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

            Bug ID: 115542
           Summary: Invalid finalization in derived type containing
                    allocatable entities
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: federico.perini at gmail dot com
  Target Milestone: ---

Created attachment 58464
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58464&action=edit
Finalization test program

- Affected versions: gfortran >= 13
- MWE at: https://godbolt.org/z/rvP5qcvEz (also attached)
- Possibly related: 110626

When a derived type has at least 3 layers:
type(r)
  - type(q)
      - type(p)
all types finalizable, and at least one "complex" component i.e. allocatable or
real(real128), type finalization on the intermediate variables is wrongly
called:

- twice instead of once
- the second time, I believe with a wrong memory address (or at least, with
object data containing garbage).

It's not in the MWE, but I have user cases where a pointer may become
associated due to the invalid values in the second call, which triggers further
issues and crashes (i.e. because the finalizer tries to deallocate it).

In the attached program, there is an intent(out) argument so finalization
should only be called once, as the subroutine enters:

 enter in-n-out 
   r final                      ! ok: finalize parent type
     - q final  F               ! ok: finalize intermediate type
       - p final           -1   ! ok: finalize 3rd-level scalars
       - p final           -1   ! ok: finalize 3rd-level scalars
       - p final            0   ! WRONG: 2nd-time and wrong value
         ERROR!                 ! WRONG: should not be called
       - p final            0   ! WRONG: 2nd-time and wrong value
         ERROR!                 ! WRONG: should not be called
 hello world
 exit in-n-out

Thank you,
Federico

Reply via email to