public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fortran: error recovery simplifying UNPACK for insufficient FIELD [PR107922]
@ 2022-12-01 20:25 Harald Anlauf
  2022-12-04 13:11 ` Mikael Morin
  0 siblings, 1 reply; 3+ messages in thread
From: Harald Anlauf @ 2022-12-01 20:25 UTC (permalink / raw)
  To: fortran, gcc-patches

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

Dear all,

we did not properly handle the case of insufficient array-valued
FIELD when trying to simplify UNPACK and could run into a NULL
pointer dereference.  The fix is obvious.

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

Thanks,
Harald


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr107922.diff --]
[-- Type: text/x-patch, Size: 2337 bytes --]

From 0ff50e73c6fce52263b9530daffe6743c1fc9b2c Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Thu, 1 Dec 2022 21:16:46 +0100
Subject: [PATCH] Fortran: error recovery simplifying UNPACK for insufficient
 FIELD [PR107922]

gcc/fortran/ChangeLog:

	PR fortran/107922
	* simplify.cc (gfc_simplify_unpack): Terminate simplification when
	array-valued argument FIELD does not provide enough elements.

gcc/testsuite/ChangeLog:

	PR fortran/107922
	* gfortran.dg/unpack_field_1.f90: New test.
---
 gcc/fortran/simplify.cc                      |  9 ++++++++-
 gcc/testsuite/gfortran.dg/unpack_field_1.f90 | 15 +++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/unpack_field_1.f90

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index b6184181f26..0a0a7755f4c 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -8485,7 +8485,14 @@ gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
 	    }
 	}
       else if (field->expr_type == EXPR_ARRAY)
-	e = gfc_copy_expr (field_ctor->expr);
+	if (field_ctor)
+	  e = gfc_copy_expr (field_ctor->expr);
+	else
+	  {
+	    /* Not enough elements in array FIELD.  */
+	    gfc_free_expr (result);
+	    return &gfc_bad_expr;
+	  }
       else
 	e = gfc_copy_expr (field);

diff --git a/gcc/testsuite/gfortran.dg/unpack_field_1.f90 b/gcc/testsuite/gfortran.dg/unpack_field_1.f90
new file mode 100644
index 00000000000..ca3cfbd2bd4
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/unpack_field_1.f90
@@ -0,0 +1,15 @@
+! { dg-do compile }
+! PR fortran/107922 - ICE in gfc_simplify_unpack
+! Test error recovery when shapes of FIELD and MASK do not match
+! Contributed by G.Steinmetz
+
+program p
+  integer, parameter :: a(2) = 1
+  integer, parameter :: d(3) = 1
+  logical, parameter :: mask(3) = [.false.,.true.,.false.]
+  integer, parameter :: b(2) = unpack(a,mask,a)          ! { dg-error "must have identical shape" }
+  integer :: c(3) = unpack(a,[.false.,.true.,.false.],a) ! { dg-error "must have identical shape" }
+  print *, unpack(a,mask,a)                              ! { dg-error "must have identical shape" }
+  print *, unpack(a,mask,d) ! OK
+  print *, unpack(a,mask,3) ! OK
+end
--
2.35.3


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

* Re: [PATCH] Fortran: error recovery simplifying UNPACK for insufficient FIELD [PR107922]
  2022-12-01 20:25 [PATCH] Fortran: error recovery simplifying UNPACK for insufficient FIELD [PR107922] Harald Anlauf
@ 2022-12-04 13:11 ` Mikael Morin
  2022-12-04 20:04   ` Harald Anlauf
  0 siblings, 1 reply; 3+ messages in thread
From: Mikael Morin @ 2022-12-04 13:11 UTC (permalink / raw)
  To: Harald Anlauf, fortran, gcc-patches

Le 01/12/2022 à 21:25, Harald Anlauf via Fortran a écrit :
> Dear all,
> 
> we did not properly handle the case of insufficient array-valued
> FIELD when trying to simplify UNPACK and could run into a NULL
> pointer dereference.  The fix is obvious.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 
The change looks good, but please add braces around the new if-else 
structure.
OK with that change.
Thanks.

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

* Re: [PATCH] Fortran: error recovery simplifying UNPACK for insufficient FIELD [PR107922]
  2022-12-04 13:11 ` Mikael Morin
@ 2022-12-04 20:04   ` Harald Anlauf
  0 siblings, 0 replies; 3+ messages in thread
From: Harald Anlauf @ 2022-12-04 20:04 UTC (permalink / raw)
  To: Mikael Morin, fortran, gcc-patches

Hi Mikael,

Am 04.12.22 um 14:11 schrieb Mikael Morin:
> Le 01/12/2022 à 21:25, Harald Anlauf via Fortran a écrit :
>> Dear all,
>>
>> we did not properly handle the case of insufficient array-valued
>> FIELD when trying to simplify UNPACK and could run into a NULL
>> pointer dereference.  The fix is obvious.
>>
>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>
> The change looks good, but please add braces around the new if-else
> structure.
> OK with that change.

thanks for the hint.  It indeed looks more readable with this small change.

Pushed: r13-4486-g24b9337d1f1b5197b6498dceb9074319be003449

> Thanks.
>

Thanks,
Harald


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

end of thread, other threads:[~2022-12-04 20:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-01 20:25 [PATCH] Fortran: error recovery simplifying UNPACK for insufficient FIELD [PR107922] Harald Anlauf
2022-12-04 13:11 ` Mikael Morin
2022-12-04 20:04   ` Harald Anlauf

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