Dear all,

while testing different stuff using code from the initial commit,
I figured that I copy-pasted erroneous code that could lead to
an infinite loop which did not update its control variable,
and an unhandled REF_INQUIRY in a switch statement.

Fixed and committed as simple and obvious, also in the function
from where I plagiated... ;-)  after regtesting.

See r15-8040-ga5d56278d145d4 and attached.

Thanks,
Harald

Am 06.03.25 um 23:00 schrieb Steve Kargl:
On Thu, Mar 06, 2025 at 10:49:08PM +0100, Harald Anlauf wrote:

Thanks for the speedy review!


It was a bit easier than normal.  After I submitted
the PR, I started to poke around in fortran/resolve.cc
to see if I could deal with the issue.  I saw that you
grab the PR last night, and left you to work your
magic.

From a5d56278d145d439092adf6f65c865c85623f881 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anl...@gmx.de>
Date: Thu, 13 Mar 2025 18:46:54 +0100
Subject: [PATCH] Fortran: improve checking of substring bounds [PR119118]

Commit r15-7873 copy-pasted erroneous code containing a non-terminating
loop that did not progress its control variable, and a switch statement
with an unhandled case leading to a gcc_unreachable () with suitable input.

	PR fortran/119118

gcc/fortran/ChangeLog:

	* dependency.cc (contains_forall_index_p): Let loop over elements
	of a constructor update its control variable.  Handle REF_INQUIRY
	in switch statement.
	(gfc_contains_implied_index_p): Likewise.

gcc/testsuite/ChangeLog:

	* gfortran.dg/bounds_check_26.f90: Update test.
---
 gcc/fortran/dependency.cc                     | 6 ++++--
 gcc/testsuite/gfortran.dg/bounds_check_26.f90 | 4 +++-
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/gcc/fortran/dependency.cc b/gcc/fortran/dependency.cc
index 28b872f6638..57c0c49391b 100644
--- a/gcc/fortran/dependency.cc
+++ b/gcc/fortran/dependency.cc
@@ -1853,7 +1853,7 @@ contains_forall_index_p (gfc_expr *expr)
     case EXPR_STRUCTURE:
     case EXPR_ARRAY:
       for (c = gfc_constructor_first (expr->value.constructor);
-	   c; gfc_constructor_next (c))
+	   c; c = gfc_constructor_next (c))
 	if (contains_forall_index_p (c->expr))
 	  return true;
       break;
@@ -1874,6 +1874,7 @@ contains_forall_index_p (gfc_expr *expr)
 	break;
 
       case REF_COMPONENT:
+      case REF_INQUIRY:
 	break;
 
       case REF_SUBSTRING:
@@ -1933,7 +1934,7 @@ gfc_contains_implied_index_p (gfc_expr *expr)
     case EXPR_STRUCTURE:
     case EXPR_ARRAY:
       for (c = gfc_constructor_first (expr->value.constructor);
-	   c; gfc_constructor_next (c))
+	   c; c = gfc_constructor_next (c))
 	if (gfc_contains_implied_index_p (c->expr))
 	  return true;
       break;
@@ -1954,6 +1955,7 @@ gfc_contains_implied_index_p (gfc_expr *expr)
 	break;
 
       case REF_COMPONENT:
+      case REF_INQUIRY:
 	break;
 
       case REF_SUBSTRING:
diff --git a/gcc/testsuite/gfortran.dg/bounds_check_26.f90 b/gcc/testsuite/gfortran.dg/bounds_check_26.f90
index 69ac9fbe2f2..ddfcbd07f3c 100644
--- a/gcc/testsuite/gfortran.dg/bounds_check_26.f90
+++ b/gcc/testsuite/gfortran.dg/bounds_check_26.f90
@@ -19,6 +19,8 @@ program main
   print *,      str(-n:11)  ! 2 checked bounds
   print *, len (str(-n:11)) ! 2 checked bounds
 
+  print *,      str(-n*n%kind:sum(n-[0,n%kind])) ! 2 checked bounds
+
 end program main
 
-! { dg-final { scan-tree-dump-times "Substring out of bounds:" 10 "original" } }
+! { dg-final { scan-tree-dump-times "Substring out of bounds:" 12 "original" } }
-- 
2.43.0

Reply via email to