Fortran: reject bad SIZE argument while simplifying ISHFTC [PR106911]

Message ID trinity-23942435-f209-452a-b9e2-427a01a491ef-1670706846774@3c-app-gmx-bap38
State New
Headers
Series Fortran: reject bad SIZE argument while simplifying ISHFTC [PR106911] |

Commit Message

Harald Anlauf Dec. 10, 2022, 9:14 p.m. UTC
  Dear all,

we should not try to simplify ISHFTC if the SIZE argument
is known to be outside the allowed range.  It's better to
generate an error by terminating the simplification.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald
  

Comments

Li, Pan2 via Gcc-patches Dec. 10, 2022, 9:29 p.m. UTC | #1
On Sat, Dec 10, 2022 at 10:14:06PM +0100, Harald Anlauf via Fortran wrote:
> Dear all,
> 
> we should not try to simplify ISHFTC if the SIZE argument
> is known to be outside the allowed range.  It's better to
> generate an error by terminating the simplification.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 

Yes.  Thanks for the patch.
  

Patch

From ae4438537fbc2ed04f3b0fb32d4e02b0ed6977a1 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Sat, 10 Dec 2022 22:05:15 +0100
Subject: [PATCH] Fortran: reject bad SIZE argument while simplifying ISHFTC
 [PR106911]

gcc/fortran/ChangeLog:

	PR fortran/106911
	* simplify.cc (gfc_simplify_ishftc): If the SIZE argument is known
	to be outside the allowed range, terminate simplification.

gcc/testsuite/ChangeLog:

	PR fortran/106911
	* gfortran.dg/pr106911.f90: New test.
---
 gcc/fortran/simplify.cc                |  3 +++
 gcc/testsuite/gfortran.dg/pr106911.f90 | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/pr106911.f90

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index aff9a1b8ced..3d3aaba84df 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -3955,6 +3955,9 @@  gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
 	return NULL;

       gfc_extract_int (sz, &ssize);
+
+      if (ssize > isize || ssize <= 0)
+	return &gfc_bad_expr;
     }
   else
     ssize = isize;
diff --git a/gcc/testsuite/gfortran.dg/pr106911.f90 b/gcc/testsuite/gfortran.dg/pr106911.f90
new file mode 100644
index 00000000000..f3fe7f6ad68
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr106911.f90
@@ -0,0 +1,18 @@ 
+! { dg-do compile }
+! PR fortran/106911 - ICE in gfc_convert_mpz_to_signed
+! Contributed by G.Steinmetz
+
+program p
+  implicit none
+  integer, parameter :: a = 10
+  integer, parameter :: b = 20
+  integer, parameter :: c = ishftc(1_1, a, b) ! { dg-error "must be less than or equal" }
+  integer, parameter :: d = ishftc(1_1, a, 0) ! { dg-error "must be positive" }
+  interface
+     subroutine s
+       import :: a, b
+       integer, parameter :: e = ishftc(1_1, a, b) ! { dg-error "must be less than or equal" }
+       integer, parameter :: f = ishftc(1_1, a, 0) ! { dg-error "must be positive" }
+     end
+  end interface
+end
--
2.35.3