public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Harald Anlauf <anlauf@gmx.de>
To: Fritz Reese <fritzoreese@gmail.com>, fortran <fortran@gcc.gnu.org>
Cc: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] PR fortran/105310 - ICE when UNION is after the 8th field in a DEC STRUCTURE with -finit-derived -finit-local-zero
Date: Wed, 20 Apr 2022 22:27:29 +0200	[thread overview]
Message-ID: <ffe6feca-dade-af6b-0a08-694963a81071@gmx.de> (raw)
In-Reply-To: <CAE4aFAmhnT9jJ_Wc+trL-BAsBbg8=STBCzsSgpeDZ31W0FrVOg@mail.gmail.com>

Hi Fritz,

Am 20.04.22 um 20:03 schrieb Fritz Reese via Fortran:
> See the bug report at gcc dot gnu dot org/bugzilla/show_bug.cgi?id=105310 .
>
> This code was originally authored by me and the fix is trivial, so I
> intend to commit the attached patch in the next few days if there is
> no dissent.

OK if you add a/the testcase.

>
> The bug is caused by gfc_conv_union_initializer in
> gcc/fortran/trans-expr.cc, which accepts a pointer to a vector of
> constructor trees (vec<constructor_elt, va_gc>*) as an argument, then
> appends one or two field constructors to the vector. The problem is
> the use of CONSTRUCTOR_APPEND_ELT(v, ...) within
> gfc_conv_union_initializer, which modifies the vector pointer v when a
> reallocation of the vector occurs, but the pointer is passed by value.
> Therefore, when a vector reallocation occurs, the caller's
> (gfc_conv_structure) vector pointer is not updated and subsequently
> points to freed memory. Chaos ensues.
>
> The bug only occurs when gfc_conv_union_initializer itself triggers
> the reallocation, which is whenever the vector is "full"
> (v->m_vecpfx.m_alloc == v->m_vecpfx.m_num). Since the vector defaults
> to allocating 8 elements and doubles in size for every reallocation,
> the bug only occurs when there are 8, 16, 32, etc... fields with
> initializers prior to the union, causing the vector of constructors to
> be resized when entering gfc_conv_union_initializer. The
> -finit-derived and -finit-local-zero options together ensure each
> field has an initializer, triggering the bug.
>
> The patch fixes the bug by passing the vector pointer to
> gfc_conv_union_initializer by reference, matching the signature of
> vec_safe_push from within the CONSTRUCTOR_APPEND_ELT macro.
>
> --
> Fritz Reese

As this affects all branches, you may backport the patch as far as
you feel reasonable.  (No, I do not use DEC extensions personally.)

Thanks for the patch!

Harald

WARNING: multiple messages have this Message-ID
From: Harald Anlauf <anlauf@gmx.de>
To: fortran@gcc.gnu.org
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] PR fortran/105310 - ICE when UNION is after the 8th field in a DEC STRUCTURE with -finit-derived -finit-local-zero
Date: Wed, 20 Apr 2022 22:27:29 +0200	[thread overview]
Message-ID: <ffe6feca-dade-af6b-0a08-694963a81071@gmx.de> (raw)
Message-ID: <20220420202729.j2XPSTWqt46lf4fdb5IU7kVpHENii5VsfXaJheNx-uc@z> (raw)
In-Reply-To: <CAE4aFAmhnT9jJ_Wc+trL-BAsBbg8=STBCzsSgpeDZ31W0FrVOg@mail.gmail.com>

Hi Fritz,

Am 20.04.22 um 20:03 schrieb Fritz Reese via Fortran:
> See the bug report at gcc dot gnu dot org/bugzilla/show_bug.cgi?id=105310 .
> 
> This code was originally authored by me and the fix is trivial, so I
> intend to commit the attached patch in the next few days if there is
> no dissent.

OK if you add a/the testcase.

> 
> The bug is caused by gfc_conv_union_initializer in
> gcc/fortran/trans-expr.cc, which accepts a pointer to a vector of
> constructor trees (vec<constructor_elt, va_gc>*) as an argument, then
> appends one or two field constructors to the vector. The problem is
> the use of CONSTRUCTOR_APPEND_ELT(v, ...) within
> gfc_conv_union_initializer, which modifies the vector pointer v when a
> reallocation of the vector occurs, but the pointer is passed by value.
> Therefore, when a vector reallocation occurs, the caller's
> (gfc_conv_structure) vector pointer is not updated and subsequently
> points to freed memory. Chaos ensues.
> 
> The bug only occurs when gfc_conv_union_initializer itself triggers
> the reallocation, which is whenever the vector is "full"
> (v->m_vecpfx.m_alloc == v->m_vecpfx.m_num). Since the vector defaults
> to allocating 8 elements and doubles in size for every reallocation,
> the bug only occurs when there are 8, 16, 32, etc... fields with
> initializers prior to the union, causing the vector of constructors to
> be resized when entering gfc_conv_union_initializer. The
> -finit-derived and -finit-local-zero options together ensure each
> field has an initializer, triggering the bug.
> 
> The patch fixes the bug by passing the vector pointer to
> gfc_conv_union_initializer by reference, matching the signature of
> vec_safe_push from within the CONSTRUCTOR_APPEND_ELT macro.
> 
> --
> Fritz Reese

As this affects all branches, you may backport the patch as far as
you feel reasonable.  (No, I do not use DEC extensions personally.)

Thanks for the patch!

Harald


  reply	other threads:[~2022-04-20 20:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-20 18:03 Fritz Reese
2022-04-20 20:27 ` Harald Anlauf [this message]
2022-04-20 20:27   ` Harald Anlauf
2022-04-21 16:29   ` Fritz Reese

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=ffe6feca-dade-af6b-0a08-694963a81071@gmx.de \
    --to=anlauf@gmx.de \
    --cc=fortran@gcc.gnu.org \
    --cc=fritzoreese@gmail.com \
    --cc=gcc-patches@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).