public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "anlauf at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/112772] New: Some issues with OPTIONAL, ALLOCATABLE dummy arguments
Date: Wed, 29 Nov 2023 21:35:42 +0000	[thread overview]
Message-ID: <bug-112772-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 112772
           Summary: Some issues with OPTIONAL, ALLOCATABLE dummy arguments
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: anlauf at gcc dot gnu.org
  Target Milestone: ---

While working on pr100651, the following issues surfaced (not discussed there):

program main
  call test_rank1 ()
contains
  subroutine test_rank1 (msg1)
    class(*), optional, allocatable :: msg1(:)
    if (present (msg1)) stop 77
    call assert_rank1 ()        ! <- no problem here
    call assert_rank1 (msg1)    ! <- problematic code path
  end

  subroutine assert_rank1 (msg2)
    class(*), optional, allocatable :: msg2(:)
  end
end

This crashes at runtime.  The dump for the problematic code path is:

  {
    struct __class__STAR_1_0a class.0;

    if (msg1 != 0B)
      {
        class.0._len = msg1->_len;
        class.0._data = msg1->_data;
        class.0._vptr = msg1->_vptr;
      }
    assert_rank1 (msg1 != 0B ? &class.0 : 0B);
    msg1->_data = class.0._data;
    msg1->_vptr = class.0._vptr;
    msg1->_len = class.0._len;
  }

We need to protect the copy-out for an absent optional argument.

Also, the testcase provided in

https://gcc.gnu.org/pipermail/fortran/2023-November/059969.html

which is similar to the above but uses deferred-length character
shows a questionable tree dump (excerpt):

void test_rank1 (struct array01_character(kind=1) * msg1, integer(kind=8) *
_msg1)
{
  integer(kind=8) D.4350;
  bitsizetype D.4351;
  sizetype D.4352;

  D.4350 = *_msg1;
  D.4351 = (bitsizetype) (sizetype) *_msg1 * 8;
  D.4352 = (sizetype) *_msg1;
  if (msg1 != 0B)
    {
      _gfortran_stop_numeric (77, 0);
    }
  L.2:;
  assert_rank1 (0B, &0);
  {
    struct array01_character(kind=1) * D.4348;
    integer(kind=8) * D.4349;

    D.4348 = msg1 != 0B ? msg1 : 0B;
    D.4349 = msg1 != 0B ? _msg1 : 0B;
    assert_rank1 (D.4348, D.4349);
  }
}

This emits warning messages with -fsanitize=undefined, likely due to the
unconditional dereferencing of _msg1 at function entry.

             reply	other threads:[~2023-11-29 21:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-29 21:35 anlauf at gcc dot gnu.org [this message]
2023-11-30 20:48 ` [Bug fortran/112772] " anlauf at gcc dot gnu.org
2023-11-30 21:08 ` anlauf at gcc dot gnu.org
2023-12-01 21:03 ` anlauf at gcc dot gnu.org
2023-12-01 21:21 ` cvs-commit at gcc dot gnu.org
2023-12-02 14:23 ` anlauf at gcc dot gnu.org

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-112772-4@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).