diff mbox

[fortran] Fix PR 49479, reshape with optional arg

Message ID 4E0901A7.90905@netcologne.de
State New
Headers show

Commit Message

Thomas Koenig June 27, 2011, 10:18 p.m. UTC
Hello world,

the attached patch fixes PR 49479, a regression for 4.7 and 4.6.  Test
case was supplied by Joost, the approach to the patch was suggested by
Tobias in comment#4 of the PR.  The patch certainly looks safe enough.

Regression-tested.  OK for trunk and, after a couple of days, for 4.6?

	Thomas

  2011-06-27  Thomas Koenig  <tkoenig@gcc.gnu.org>

         PR fortran/49479
         * m4/reshape.m4: If source allocation is smaller than one, set it
         to one.
         * generated/reshape_r16.c: Regenerated.
         * generated/reshape_c4.c: Regenerated.
         * generated/reshape_c16.c: Regenerated.
         * generated/reshape_c8.c: Regenerated.
         * generated/reshape_r4.c: Regenerated.
         * generated/reshape_i4.c: Regenerated.
         * generated/reshape_r10.c: Regenerated.
         * generated/reshape_r8.c: Regenerated.
         * generated/reshape_c10.c: Regenerated.
         * generated/reshape_i8.c: Regenerated.
         * generated/reshape_i16.c: Regenerated.

2011-06-27  Thomas Koenig  <tkoenig@gcc.gnu.org>

         PR fortran/49479
         * gfortran.dg/reshape_zerosize_3.f90:  New test.
! { dg-do run }
! PR 49479 - this used not to print anything.
! Test case by Joost VandeVondele.
MODULE M1
  IMPLICIT NONE
CONTAINS
SUBROUTINE S1(data)
    INTEGER, DIMENSION(:), INTENT(IN), &
      OPTIONAL                               :: DATA
    character(20) :: line
    IF (.not. PRESENT(data)) call abort
    write (unit=line,fmt='(I5)') size(data)
    if (line /= '    0               ') call abort
END SUBROUTINE

SUBROUTINE S2(N)
INTEGER :: N
INTEGER, ALLOCATABLE, DIMENSION(:, :)    :: blki
ALLOCATE(blki(3,N))
blki=0
CALL S1(RESHAPE(blki,(/3*N/)))
END SUBROUTINE S2
END MODULE M1

USE M1
CALL S2(0)
END
! { dg-final { cleanup-modules "m1" } }

Comments

Jerry DeLisle June 28, 2011, 2 a.m. UTC | #1
On 06/27/2011 03:18 PM, Thomas Koenig wrote:
> Hello world,
>
> the attached patch fixes PR 49479, a regression for 4.7 and 4.6. Test
> case was supplied by Joost, the approach to the patch was suggested by
> Tobias in comment#4 of the PR. The patch certainly looks safe enough.
>
> Regression-tested. OK for trunk and, after a couple of days, for 4.6?
>
> Thomas
>

OK,

Jerry
diff mbox

Patch

Index: m4/reshape.m4
===================================================================
--- m4/reshape.m4	(Revision 175543)
+++ m4/reshape.m4	(Arbeitskopie)
@@ -101,6 +101,8 @@ 
 
   if (ret->data == NULL)
     {
+      index_type alloc_size;
+
       rs = 1;
       for (n = 0; n < rdim; n++)
 	{
@@ -111,7 +113,13 @@ 
 	  rs *= rex;
 	}
       ret->offset = 0;
-      ret->data = internal_malloc_size ( rs * sizeof ('rtype_name`));
+
+      if (unlikely (rs < 1))
+        alloc_size = 1;
+      else
+        alloc_size = rs * sizeof ('rtype_name`);
+
+      ret->data = internal_malloc_size (alloc_size);
       ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim;
     }