public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Committed] PR fortran/92174
@ 2019-10-22 18:39 Steve Kargl
  2019-10-24  8:39 ` [PATCH] Fix another UBSAN in Fortran coarray Martin Liška
  0 siblings, 1 reply; 3+ messages in thread
From: Steve Kargl @ 2019-10-22 18:39 UTC (permalink / raw)
  To: fortran, gcc-patches

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

Committed as not so-obvious as I don't use UBSAN when fixing bugs.
The patch pushes the F2018:C822 check down a layer to prevent a segfault
when UBSAN is used.

2019-10-22  Steven G. Kargl  <kargl@gcc.gnu.org>

 PR fortran/92174
 * decl.c (attr_decl1): Move check for F2018:C822 from here ...
 * array.c (gfc_set_array_spec): ... to here. 

-- 
Steve

[-- Attachment #2: pr92174.diff --]
[-- Type: text/x-diff, Size: 1784 bytes --]

Index: gcc/fortran/array.c
===================================================================
--- gcc/fortran/array.c	(revision 277262)
+++ gcc/fortran/array.c	(working copy)
@@ -862,6 +863,10 @@ gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *a
 
       sym->as->cotype = as->cotype;
       sym->as->corank = as->corank;
+      /* Check F2018:C822.  */
+      if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS)
+	goto too_many;
+
       for (i = 0; i < as->corank; i++)
 	{
 	  sym->as->lower[sym->as->rank + i] = as->lower[i];
@@ -880,6 +885,10 @@ gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *a
       sym->as->cray_pointee = as->cray_pointee;
       sym->as->cp_was_assumed = as->cp_was_assumed;
 
+      /* Check F2018:C822.  */
+      if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS)
+	goto too_many;
+
       for (i = 0; i < sym->as->corank; i++)
 	{
 	  sym->as->lower[as->rank + i] = sym->as->lower[i];
@@ -894,6 +903,12 @@ gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *a
 
   free (as);
   return true;
+
+too_many:
+
+  gfc_error ("rank + corank of %qs exceeds %d at %C", sym->name,
+	     GFC_MAX_DIMENSIONS);
+  return false;
 }
 
 
Index: gcc/fortran/decl.c
===================================================================
--- gcc/fortran/decl.c	(revision 277262)
+++ gcc/fortran/decl.c	(working copy)
@@ -8524,15 +8524,6 @@ attr_decl1 (void)
       goto cleanup;
     }
 
-  /* Check F2018:C822.  */
-  if (sym->attr.dimension && sym->attr.codimension
-      && sym->as && sym->as->rank + sym->as->corank > 15)
-    {
-      gfc_error ("rank + corank of %qs exceeds 15 at %C", sym->name);
-      m = MATCH_ERROR;
-      return m;
-    }
-
   if (sym->attr.cray_pointee && sym->as != NULL)
     {
       /* Fix the array spec.  */

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] Fix another UBSAN in Fortran coarray.
  2019-10-22 18:39 [Committed] PR fortran/92174 Steve Kargl
@ 2019-10-24  8:39 ` Martin Liška
  2019-10-24  9:03   ` Tobias Burnus
  0 siblings, 1 reply; 3+ messages in thread
From: Martin Liška @ 2019-10-24  8:39 UTC (permalink / raw)
  To: sgk, fortran, gcc-patches

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

Hello.

There are 2 more places that need to be handled similarly
to not do out of bounds access.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

[-- Attachment #2: 0001-Fix-another-UBSAN-in-Fortran-coarray.patch --]
[-- Type: text/x-patch, Size: 1430 bytes --]

From b6698f326fa4625aca6b2fa65824f5aed8b331df Mon Sep 17 00:00:00 2001
From: Martin Liska <mliska@suse.cz>
Date: Thu, 24 Oct 2019 09:53:43 +0200
Subject: [PATCH] Fix another UBSAN in Fortran coarray.

gcc/fortran/ChangeLog:

2019-10-24  Martin Liska  <mliska@suse.cz>

	PR fortran/92174
	* array.c (gfc_resolve_array_spec): Break the loop
	for out of bounds index.
	* resolve.c (is_non_constant_shape_array): Likewise.
---
 gcc/fortran/array.c   | 3 +++
 gcc/fortran/resolve.c | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/gcc/fortran/array.c b/gcc/fortran/array.c
index f0980dd9cef..36223d2233d 100644
--- a/gcc/fortran/array.c
+++ b/gcc/fortran/array.c
@@ -410,6 +410,9 @@ gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
 
   for (i = 0; i < as->rank + as->corank; i++)
     {
+      if (i == GFC_MAX_DIMENSIONS)
+	return false;
+
       e = as->lower[i];
       if (!resolve_array_bound (e, check_constant))
 	return false;
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 93f2d0aa761..5deeb4fc87b 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -12266,6 +12266,9 @@ is_non_constant_shape_array (gfc_symbol *sym)
 	 simplification now.  */
       for (i = 0; i < sym->as->rank + sym->as->corank; i++)
 	{
+	  if (i == GFC_MAX_DIMENSIONS)
+	    break;
+
 	  e = sym->as->lower[i];
 	  if (e && (!resolve_index_expr(e)
 		    || !gfc_is_constant_expr (e)))
-- 
2.23.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Fix another UBSAN in Fortran coarray.
  2019-10-24  8:39 ` [PATCH] Fix another UBSAN in Fortran coarray Martin Liška
@ 2019-10-24  9:03   ` Tobias Burnus
  0 siblings, 0 replies; 3+ messages in thread
From: Tobias Burnus @ 2019-10-24  9:03 UTC (permalink / raw)
  To: Martin Liška, sgk, fortran, gcc-patches

OK. (I assume that for the culprit input, an error is show at some point.)

Thanks for ubsan testing!

Tobias

On 10/24/19 10:38 AM, Martin Liška wrote:
> Hello.
>
> There are 2 more places that need to be handled similarly
> to not do out of bounds access.
>
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
>
> Ready to be installed?
> Thanks,
> Martin

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-10-24  8:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-22 18:39 [Committed] PR fortran/92174 Steve Kargl
2019-10-24  8:39 ` [PATCH] Fix another UBSAN in Fortran coarray Martin Liška
2019-10-24  9:03   ` Tobias Burnus

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).