public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Thomas König" <tk@tkoenig.net>
To: "fortran@gcc.gnu.org" <fortran@gcc.gnu.org>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [patch, fortran, committed] Fix PR 96556, segfault on NULL pointer dereference
Date: Mon, 10 Aug 2020 19:18:22 +0200	[thread overview]
Message-ID: <61e0b00d-7536-ab17-e8d6-3ea7a5d6d262@tkoenig.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 662 bytes --]

Hi,

in the code about DO loop warning I recently introduced, there was
a hidden NULL pointer dereference found by Jürgen Reuter and fixed
as obvious and simple in r11-2636.

Fix NULL pointer dereference in doloop_contained_function_call.

Thanks to Jürgen for the bug report and to Dominique for spotting
what commit this was caused by.

Unfortunately, not all bugs are quite as simple and obvious as this
one :-)

gcc/fortran/ChangeLog:

	PR fortran/96556
	* frontend-passes.c (doloop_contained_function_call):
	Do not dereference a NULL pointer for value.function.esym.

gcc/testsuite/ChangeLog:

	PR fortran/96556
	* gfortran.dg/do_check_15.f90: New test.

[-- Attachment #2: p1.diff --]
[-- Type: text/x-patch, Size: 2295 bytes --]

diff --git a/gcc/fortran/frontend-passes.c b/gcc/fortran/frontend-passes.c
index 6bcb1f06b1c..83f6fd804b1 100644
--- a/gcc/fortran/frontend-passes.c
+++ b/gcc/fortran/frontend-passes.c
@@ -2329,7 +2329,8 @@ doloop_contained_function_call (gfc_expr **e,
   gfc_symbol *sym, *do_var;
   contained_info *info;
 
-  if (expr->expr_type != EXPR_FUNCTION || expr->value.function.isym)
+  if (expr->expr_type != EXPR_FUNCTION || expr->value.function.isym
+      || expr->value.function.esym == NULL)
     return 0;
 
   sym = expr->value.function.esym;
diff --git a/gcc/testsuite/gfortran.dg/do_check_15.f90 b/gcc/testsuite/gfortran.dg/do_check_15.f90
new file mode 100644
index 00000000000..f67452b4660
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/do_check_15.f90
@@ -0,0 +1,58 @@
+! { dg-do compile }
+! PR fortran/96556 - this used to cause an ICE.
+! Test case by Juergen Reuter.
+module polarizations
+
+  implicit none
+  private
+
+  type :: smatrix_t
+     private
+     integer :: dim = 0
+     integer :: n_entry = 0
+     integer, dimension(:,:), allocatable :: index
+   contains
+     procedure :: write => smatrix_write
+  end type smatrix_t
+
+  type, extends (smatrix_t) :: pmatrix_t
+     private
+   contains
+     procedure :: write => pmatrix_write
+     procedure :: normalize => pmatrix_normalize
+  end type pmatrix_t
+
+contains
+
+  subroutine msg_error (string)
+    character(len=*), intent(in), optional :: string
+  end subroutine msg_error
+  
+  subroutine smatrix_write (object)
+    class(smatrix_t), intent(in) :: object
+  end subroutine smatrix_write
+
+  subroutine pmatrix_write (object)
+    class(pmatrix_t), intent(in) :: object
+    call object%smatrix_t%write ()
+  end subroutine pmatrix_write
+
+  subroutine pmatrix_normalize (pmatrix)
+    class(pmatrix_t), intent(inout) :: pmatrix
+    integer :: i, hmax
+    logical :: fermion, ok
+    do i = 1, pmatrix%n_entry
+       associate (index => pmatrix%index(:,i))
+         if (index(1) == index(2)) then
+            call error ("diagonal must be real")
+         end if
+       end associate
+    end do
+  contains
+    subroutine error (msg)
+      character(*), intent(in) :: msg
+      call pmatrix%write ()
+    end subroutine error
+  end subroutine pmatrix_normalize
+
+end module polarizations

                 reply	other threads:[~2020-08-10 17:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=61e0b00d-7536-ab17-e8d6-3ea7a5d6d262@tkoenig.net \
    --to=tk@tkoenig.net \
    --cc=fortran@gcc.gnu.org \
    --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).