Bug 37203

Summary: Check ORDER= of RESHAPE
Product: gcc Reporter: Tobias Burnus <burnus>
Component: fortranAssignee: Daniel Franke <dfranke>
Status: RESOLVED FIXED    
Severity: normal CC: dfranke, gcc-bugs, tkoenig
Priority: P3 Keywords: diagnostic, patch
Version: 4.4.0   
Target Milestone: ---   
URL: http://gcc.gnu.org/ml/fortran/2009-01/msg00049.html
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2009-01-04 19:10:33
Bug Depends on:    
Bug Blocks: 20585    

Description Tobias Burnus 2008-08-22 17:43:20 UTC
Found at http://groups.google.com/group/gg95/browse_thread/thread/d27f173506fbb9cc

The following program compiles without any warning, but NAG f95 prints:

Error: line 6: Value 2 in ORDER arg to RESHAPE duplicated

integer, dimension(6) :: source1 = (/ 1, 2, 3, 4, 5, 6 /)
integer, dimension(2) :: shape1 = (/ 2, 5/)
integer, dimension(2) :: pad1 = (/ 0, 0/)

write (unit=*,fmt=*) reshape(source1, shape1, pad1, (/2,1/)) ! Valid
write (unit=*,fmt=*) reshape(source1, shape1, pad1, (/2,2/)) ! Invalid
end
Comment 1 Thomas Koenig 2008-08-23 14:11:58 UTC
Confirmed.

We should also have a run-time check for this:

tkoenig@gcc16:/tmp$ gfortran -fbounds-check foo.f90
tkoenig@gcc16:/tmp$ ./a.out
           1           6           2           0           3           0           4           0           5           0
*** glibc detected *** free(): invalid next size (fast): 0x0000000000508950 ***
Aborted
tkoenig@gcc16:/tmp$                      
Comment 2 Thomas Koenig 2008-09-07 09:12:53 UTC
I'm working on the run-time test.
Comment 3 Thomas Koenig 2008-09-07 13:34:41 UTC
Subject: Bug 37203

Author: tkoenig
Date: Sun Sep  7 13:33:18 2008
New Revision: 140086

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=140086
Log:
2008-09-07  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/37203
	* intrinsics/reshape_generic.c:  Add checking on
	out-of-bounds and duplicate values of order argument.
	* m4/reshape.m4:  Likewise.
	* generated/reshape_c10.c: Regenerated.
	* generated/reshape_c16.c: Regenerated.
	* generated/reshape_c4.c: Regenerated.
	* generated/reshape_c8.c: Regenerated.
	* generated/reshape_i16.c: Regenerated.
	* generated/reshape_i4.c: Regenerated.
	* generated/reshape_i8.c: Regenerated.
	* generated/reshape_r10.c: Regenerated.
	* generated/reshape_r16.c: Regenerated.
	* generated/reshape_r4.c: Regenerated.
	* generated/reshape_r8.c: Regenerated.

2008-09-07  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/37203
	* gfortran.d/reshape_order_1.f90:  New test case.
	* gfortran.d/reshape_order_2.f90:  New test case.
	* gfortran.d/reshape_order_3.f90:  New test case.
	* gfortran.d/reshape_order_4.f90:  New test case.


Added:
    trunk/gcc/testsuite/gfortran.dg/reshape_order_1.f90
    trunk/gcc/testsuite/gfortran.dg/reshape_order_2.f90
    trunk/gcc/testsuite/gfortran.dg/reshape_order_3.f90
    trunk/gcc/testsuite/gfortran.dg/reshape_order_4.f90
Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/generated/reshape_c10.c
    trunk/libgfortran/generated/reshape_c16.c
    trunk/libgfortran/generated/reshape_c4.c
    trunk/libgfortran/generated/reshape_c8.c
    trunk/libgfortran/generated/reshape_i16.c
    trunk/libgfortran/generated/reshape_i4.c
    trunk/libgfortran/generated/reshape_i8.c
    trunk/libgfortran/generated/reshape_r10.c
    trunk/libgfortran/generated/reshape_r16.c
    trunk/libgfortran/generated/reshape_r4.c
    trunk/libgfortran/generated/reshape_r8.c
    trunk/libgfortran/intrinsics/reshape_generic.c
    trunk/libgfortran/m4/reshape.m4

Comment 4 Daniel Franke 2009-01-04 14:56:57 UTC
simplify.c (gfc_simplify_reshape) has:

3547	  if (x[order[i]])
3548	    {
3549	      gfc_error ("Invalid permutation in ORDER parameter at %L",
3550			 &e->where);
3551	      gfc_free_expr (e);
3552	      goto bad_reshape;
3553	    }

which detects invalid permutations in the case of constant(!) arguments. 
Closing as fixed.
Comment 5 Mikael Morin 2009-01-04 19:00:20 UTC
(In reply to comment #4)
> which detects invalid permutations in the case of constant(!) arguments. 
> Closing as fixed.
> 
No, it's not. Reopening.
The initial testcase is still not catch. 
Comment 6 Daniel Franke 2009-01-04 19:10:33 UTC
(In reply to comment #5)
> (In reply to comment #4)
> > which detects invalid permutations in the case of constant(!) arguments. 
> > Closing as fixed.
> > 
> No, it's not. Reopening.
> The initial testcase is still not catch. 

Note the emphasis on _constant_. To get the error, change the testcase to: 

integer, dimension(6), PARAMETER :: source1 = (/ 1, 2, 3, 4, 5, 6 /)
integer, dimension(2), PARAMETER :: shape1 = (/ 2, 5/)
integer, dimension(2), PARAMETER :: pad1 = (/ 0, 0/)

write (unit=*,fmt=*) reshape(source1, shape1, pad1, (/2,1/)) ! Valid
write (unit=*,fmt=*) reshape(source1, shape1, pad1, (/2,2/)) ! Invalid
end

And while trying to argue why this never will be caught as is, I realize that you are right. That SOURCE, SHAPE and PAD are variables doesn't matter as in this case ORDER is constant and can be checked.

Stupidity needs to be punished. Assigning myself to fix it.
Comment 7 Daniel Franke 2009-01-04 21:25:06 UTC
(In reply to comment #6)
> Stupidity needs to be punished. Assigning myself to fix it.

Patch: http://gcc.gnu.org/ml/fortran/2009-01/msg00049.html
Comment 8 Daniel Franke 2009-03-22 12:12:24 UTC
Subject: Bug 37203

Author: dfranke
Date: Sun Mar 22 12:12:10 2009
New Revision: 144996

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=144996
Log:
gcc/fortran/:
2009-03-22  Daniel Franke  <franke.daniel@gmail.com>

        PR fortran/37203
        * check.c (gfc_check_reshape): Additional checks for the
        SHAPE and ORDER arguments.
        * simplify.c (gfc_simplify_reshape): Converted argument checks
        to asserts.


gcc/testsuite/:
2009-03-22  Daniel Franke  <franke.daniel@gmail.com>

        PR fortran/37203
        * gfortran.dg/reshape_order_5.f90: New.
        * gfortran.dg/reshape_shape_1.f90: New.



Added:
    branches/fortran-dev/gcc/fortran/ChangeLog.dev
    branches/fortran-dev/gcc/testsuite/ChangeLog.fortran-dev
    branches/fortran-dev/gcc/testsuite/gfortran.dg/reshape_order_5.f90
    branches/fortran-dev/gcc/testsuite/gfortran.dg/reshape_shape_1.f90
Modified:
    branches/fortran-dev/gcc/fortran/check.c
    branches/fortran-dev/gcc/fortran/simplify.c

Comment 9 Tobias Burnus 2009-06-04 21:52:58 UTC
Subject: Bug 37203

Author: burnus
Date: Thu Jun  4 21:52:32 2009
New Revision: 148190

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=148190
Log:
gcc/fortran/
2009-06-04  Daniel Franke  <franke.daniel@gmail.com>

        PR fortran/37203
        * check.c (gfc_check_reshape): Additional checks for the
        SHAPE and ORDER arguments.
        * simplify.c (gfc_simplify_reshape): Converted argument checks
        to asserts.

gcc/testsuite/
2009-06-04  Daniel Franke  <franke.daniel@gmail.com>

        PR fortran/37203
        * gfortran.dg/reshape_order_5.f90: New.
        * gfortran.dg/reshape_shape_1.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/reshape_order_5.f90
    trunk/gcc/testsuite/gfortran.dg/reshape_shape_1.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/check.c
    trunk/gcc/fortran/simplify.c
    trunk/gcc/testsuite/ChangeLog

Comment 10 Tobias Burnus 2009-06-04 21:59:06 UTC
Merged patch from the fortran-dev branch to the trunk (4.5).

Close bug as FIXED. Thanks for the patches, Thomas and Daniel!
Comment 11 Tobias Burnus 2009-06-06 21:10:22 UTC
Subject: Bug 37203

Author: burnus
Date: Sat Jun  6 21:09:57 2009
New Revision: 148238

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=148238
Log:
2009-06-06  Daniel Franke  <franke.daniel@gmail.com>

        PR fortran/37203
        * simplify.c (gfc_simplify_reshape): Fixed reshaping of empty
        * arrays
        without padding.

2009-06-06  Daniel Franke  <franke.daniel@gmail.com>

        PR fortran/37203
        * reshape_zerosize_2.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/reshape_zerosize_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/simplify.c
    trunk/gcc/testsuite/ChangeLog