public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch, fortran] PR96624 - A segment fault occurred when using the reshape function result to assign a variable
@ 2020-08-27  6:17 Paul Richard Thomas
  2020-08-27  7:46 ` Tobias Burnus
  0 siblings, 1 reply; 2+ messages in thread
From: Paul Richard Thomas @ 2020-08-27  6:17 UTC (permalink / raw)
  To: fortran, gcc-patches

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

Hi All,

Here is another of Steve Kargl's patches.

Before the patch is applied, the following code is generated:
    atmp.0.span = 4;
    atmp.0.data = 0B;
    atmp.0.offset = 0;
    (*(integer(kind=4)[0] * restrict) atmp.0.data)[0] = 1;
    (*(integer(kind=4)[0] * restrict) atmp.0.data)[1] = 2;

which causes a segfault at run time. The test case counts the number of
occurrences of 'data' to check that the bad assignments have gone.

Regtests OK on FC31/x86_64 - OK for aster?

This patch fixes PR96624.

2020-08-27  Paul Thomas  <pault@gcc.gnu.org>

gcc/fortran
PR fortran/96624
* simplify.c (gfc_simplify_reshape): Detect zero shape and
clear index if found.

gcc/testsuite/
PR fortran/96624
* gfortran.dg/reshape_8.f90 : New test.

[-- Attachment #2: submit.diff --]
[-- Type: text/x-patch, Size: 1172 bytes --]

diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index 074b50c2e68..8e0d2f97a60 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -6398,7 +6398,7 @@ gfc_simplify_is_contiguous (gfc_expr *array)
 
   if (gfc_is_not_contiguous (array))
     return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 0);
-    
+
   return NULL;
 }
 
@@ -6725,6 +6725,7 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
   unsigned long j;
   size_t nsource;
   gfc_expr *e, *result;
+  bool zerosize = false;
 
   /* Check that argument expression types are OK.  */
   if (!is_constant_array_expr (source)
@@ -6847,7 +6848,14 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
   result->rank = rank;
   result->shape = gfc_get_shape (rank);
   for (i = 0; i < rank; i++)
-    mpz_init_set_ui (result->shape[i], shape[i]);
+    {
+      mpz_init_set_ui (result->shape[i], shape[i]);
+      if (shape[i] == 0)
+	zerosize = true;
+    }
+
+  if (zerosize)
+    goto sizezero;
 
   while (nsource > 0 || npad > 0)
     {
@@ -6897,6 +6905,8 @@ inc:
       break;
     }
 
+sizezero:
+
   mpz_clear (index);
 
   return result;

[-- Attachment #3: pr96624.f90 --]
[-- Type: text/x-fortran, Size: 411 bytes --]

! { dg-do compile }
! { dg-options "-fdump-tree-original" }
!
! Test the fix for PR96624 in which an attempt was made to assign
! to the zero length temporary created by reshape, resulting in a segfault.
!
! Contributed by Dong Shenpo  <shenpo.dong@compiler-dev.com>
!
program test
  integer :: a(2,0)
  a = reshape([1,2,3,4], [2,0])
  print *, a
end
! { dg-final { scan-tree-dump-times "data" 3 "original" } }

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

* Re: [Patch, fortran] PR96624 - A segment fault occurred when using the reshape function result to assign a variable
  2020-08-27  6:17 [Patch, fortran] PR96624 - A segment fault occurred when using the reshape function result to assign a variable Paul Richard Thomas
@ 2020-08-27  7:46 ` Tobias Burnus
  0 siblings, 0 replies; 2+ messages in thread
From: Tobias Burnus @ 2020-08-27  7:46 UTC (permalink / raw)
  To: Paul Richard Thomas, fortran, gcc-patches

Looks good to me.

Thanks for the patch!

Tobias

On 8/27/20 8:17 AM, Paul Richard Thomas via Fortran wrote:
> Hi All,
>
> Here is another of Steve Kargl's patches.
>
> Before the patch is applied, the following code is generated:
>      atmp.0.span = 4;
>      atmp.0.data = 0B;
>      atmp.0.offset = 0;
>      (*(integer(kind=4)[0] * restrict) atmp.0.data)[0] = 1;
>      (*(integer(kind=4)[0] * restrict) atmp.0.data)[1] = 2;
>
> which causes a segfault at run time. The test case counts the number of
> occurrences of 'data' to check that the bad assignments have gone.
>
> Regtests OK on FC31/x86_64 - OK for aster?
>
> This patch fixes PR96624.
>
> 2020-08-27  Paul Thomas  <pault@gcc.gnu.org>
>
> gcc/fortran
> PR fortran/96624
> * simplify.c (gfc_simplify_reshape): Detect zero shape and
> clear index if found.
>
> gcc/testsuite/
> PR fortran/96624
> * gfortran.dg/reshape_8.f90 : New test.
-----------------
Mentor Graphics (Deutschland) GmbH, Arnulfstraße 201, 80634 München / Germany
Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Alexander Walter

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

end of thread, other threads:[~2020-08-27  7:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-27  6:17 [Patch, fortran] PR96624 - A segment fault occurred when using the reshape function result to assign a variable Paul Richard Thomas
2020-08-27  7:46 ` 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).