public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/115542] New: Invalid finalization in derived type containing allocatable entities
@ 2024-06-18 16:44 federico.perini at gmail dot com
  0 siblings, 0 replies; only message in thread
From: federico.perini at gmail dot com @ 2024-06-18 16:44 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-06-18 16:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-18 16:44 [Bug fortran/115542] New: Invalid finalization in derived type containing allocatable entities federico.perini at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).