Fortran: NULL pointer dereference while parsing a function [PR107423]

Message ID trinity-7efcc196-d97a-4fa6-bd76-48e9eb90d528-1670876532663@3c-app-gmx-bs69
State New
Headers
Series Fortran: NULL pointer dereference while parsing a function [PR107423] |

Commit Message

Harald Anlauf Dec. 12, 2022, 8:22 p.m. UTC
  Dear all,

here's another obvious patch by Steve which prevents a
NULL pointer dereference during parsing.

Regtested on x86_64-pc-linux-gnu.

Will commit to mainline within 24h unless there are comments.

Thanks,
Harald
  

Patch

From f23a5252ee086f9b78c44150d409e40a445c6928 Mon Sep 17 00:00:00 2001
From: Steve Kargl <kargl@gcc.gnu.org>
Date: Mon, 12 Dec 2022 21:11:07 +0100
Subject: [PATCH] Fortran: NULL pointer dereference while parsing a function
 [PR107423]

gcc/fortran/ChangeLog:

	PR fortran/107423
	* parse.cc (parse_spec): Avoid NULL pointer dereference when parsing
	a function and an error occured.

gcc/testsuite/ChangeLog:

	PR fortran/107423
	* gfortran.dg/pr107423.f90: New test.
---
 gcc/fortran/parse.cc                   |  2 +-
 gcc/testsuite/gfortran.dg/pr107423.f90 | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr107423.f90

diff --git a/gcc/fortran/parse.cc b/gcc/fortran/parse.cc
index cdae43fa1fd..bc2b2188eea 100644
--- a/gcc/fortran/parse.cc
+++ b/gcc/fortran/parse.cc
@@ -4015,7 +4015,7 @@  parse_spec (gfc_statement st)
       gfc_symbol* proc = gfc_current_ns->proc_name;
       gcc_assert (proc);

-      if (proc->result->ts.type == BT_UNKNOWN)
+      if (proc->result && proc->result->ts.type == BT_UNKNOWN)
 	function_result_typed = true;
     }

diff --git a/gcc/testsuite/gfortran.dg/pr107423.f90 b/gcc/testsuite/gfortran.dg/pr107423.f90
new file mode 100644
index 00000000000..9ae64c94ae0
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr107423.f90
@@ -0,0 +1,18 @@ 
+! { dg-do compile }
+! { dg-options "-std=f95" }
+! PR fortran/107423 - ICE in parse_spec
+! Contributed by G.Steinmetz
+
+program p
+  type t(k)
+     integer, kind :: k          ! { dg-error "Fortran 2003" }
+     integer :: a
+  end type
+contains
+  function f()
+    type(t(4)), allocatable :: x ! { dg-error "Invalid character" }
+    allocate (t(4) :: x)         ! { dg-error "cannot be used" }
+  end   ! { dg-error "END" }
+end     ! { dg-error "END" }
+
+! { dg-prune-output "Unexpected end of file" }
--
2.35.3