public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "pault at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/97920] [FINAL] -O2  segment fault due to extend derive type's member being partially allocated
Date: Tue, 08 Dec 2020 17:05:19 +0000	[thread overview]
Message-ID: <bug-97920-4-4RYib6rz3D@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-97920-4@http.gcc.gnu.org/bugzilla/>

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

Paul Thomas <pault at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |WAITING
                 CC|                            |pault at gcc dot gnu.org

--- Comment #2 from Paul Thomas <pault at gcc dot gnu.org> ---
(In reply to Martin Liška from comment #1)
> Confirmed with valgrind. At least as old as 4.9.0.

Hi,

>From a quick perusal of the standard, I find in F2003 16.4.2.1:

"Unless a pointer is initialized (explicitly or by default), it has an initial
association status of undefined. A pointer may be initialized to have an
association status of disassociated".

In your testcase, the status of b%b is undefined and so the compiler can do
anything it wants with it, including segfaulting. I think therefore that you
should initialize the derived types in your application as follows:

  type t1
    real, dimension(:), pointer :: a => NULL ()
  contains
    final :: t1f
  end type

  type, extends(t1) :: t2
    real, dimension(:), pointer :: b => NULL ()
  contains
    final :: t2f
  end type

This clears the valgrind error "Conditional jump or move depends on
uninitialised value(s)". Also the finalization is invoked so that the programme
completes with zero memory allocation,

To my surprise (probably due to standard ignorance), leaving the declared type
declarations as you have them, and declaring 'b' as

  type(t2) :: b = t2 (NULL(), NULL())

clears the valgrind fault but no finalization occurs. I notice that
finalization does not occur if an entity has the save attribute. gfortran
assigns 'b' the IMPLICIT-SAVE attribute, which is why the finalization does not
occur. I have been unable to find whether or not this is conforming.

However, initializing 'b' in an assignment:
  b = t2(NULL(), NULL())

clears the valgrind fault and results in the deallocation of memory. This
confirms my suspicion about the save attribute.

In conclusion, I do not believe that this is a bug. If you do not use pointers
as pointers, make them allocatable instead. These are automatically nullified
on entry into scope.

Thanks for the report by the way!

Paul

  parent reply	other threads:[~2020-12-08 17:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-20  2:08 [Bug fortran/97920] New: " xin.liu@compiler-dev.com
2020-11-20  7:35 ` [Bug fortran/97920] " marxin at gcc dot gnu.org
2020-12-08 17:05 ` pault at gcc dot gnu.org [this message]
2020-12-12 13:58 ` tkoenig at gcc dot gnu.org
2020-12-16  0:27 ` xin.liu@compiler-dev.com
2020-12-16  0:30 ` xin.liu@compiler-dev.com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-97920-4-4RYib6rz3D@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).