PR fortran/103473 - [11/12 Regression] ICE in simplify_minmaxloc_nodim, at fortran/simplify.c:5287

Message ID trinity-4d9e2363-ce7c-4e72-95fd-f74783877814-1638223284647@3c-app-gmx-bs36
State New
Headers
Series PR fortran/103473 - [11/12 Regression] ICE in simplify_minmaxloc_nodim, at fortran/simplify.c:5287 |

Commit Message

Harald Anlauf Nov. 29, 2021, 10:01 p.m. UTC
  Dear all,

another trivial and obvious one, discovered by Gerhard.

We can have a NULL pointer dereference simplifying MINLOC/MAXLOC
on an array that was not properly declared.

OK for mainline / affected 11-branch after regtesting completes?

Thanks,
Harald
  

Comments

Mikael Morin Nov. 30, 2021, 11:37 a.m. UTC | #1
Le 29/11/2021 à 23:01, Harald Anlauf via Fortran a écrit :
> Dear all,
> 
> another trivial and obvious one, discovered by Gerhard.
> 
> We can have a NULL pointer dereference simplifying MINLOC/MAXLOC
> on an array that was not properly declared.
> 
> OK for mainline / affected 11-branch after regtesting completes?
> 
Yes, fine as well.

I have the impression that there are quite a number of bugs of this 
kind, and that maybe we could take a more systematic approach to not try 
to simplify something with errors.

Thanks.
  

Patch

From 6bdecd3805eb0d55722992ccb517d08b9bafe605 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Mon, 29 Nov 2021 22:56:30 +0100
Subject: [PATCH] Fortran: error recovery when simplifying MINLOC/MAXLOC

gcc/fortran/ChangeLog:

	PR fortran/103473
	* simplify.c (simplify_minmaxloc_nodim): Avoid NULL pointer
	dereference when shape is not set.

gcc/testsuite/ChangeLog:

	PR fortran/103473
	* gfortran.dg/minmaxloc_15.f90: New test.
---
 gcc/fortran/simplify.c                     |  3 +++
 gcc/testsuite/gfortran.dg/minmaxloc_15.f90 | 11 +++++++++++
 2 files changed, 14 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/minmaxloc_15.f90

diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index c9e13b59da9..fb7b7814603 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -5280,6 +5280,9 @@  simplify_minmaxloc_nodim (gfc_expr *result, gfc_expr *extremum,
       && !mask->value.logical)
     goto finish;

+  if (array->shape == NULL)
+    goto finish;
+
   for (i = 0; i < array->rank; i++)
     {
       count[i] = 0;
diff --git a/gcc/testsuite/gfortran.dg/minmaxloc_15.f90 b/gcc/testsuite/gfortran.dg/minmaxloc_15.f90
new file mode 100644
index 00000000000..e4eba3501d5
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/minmaxloc_15.f90
@@ -0,0 +1,11 @@ 
+! { dg-do compile }
+! PR fortran/103473 - ICE in simplify_minmaxloc_nodim
+! Test case by Gerhard Steinmetz.
+
+subroutine s
+  implicit none
+  integer, parameter :: a(+'1') = [1] ! { dg-error "unary numeric operator" }
+  print *, minloc (a)
+end
+
+! { dg-prune-output "Parameter array" }
--
2.26.2