Bug 31538 - misleading bounds check error
Summary: misleading bounds check error
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: 4.5.1
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Fortran_bounds_checking
  Show dependency treegraph
 
Reported: 2007-04-11 16:08 UTC by Tobias Burnus
Modified: 2010-04-17 22:12 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-04-11 22:52:54


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2007-04-11 16:08:35 UTC
The line 23 of gfortran.dg/result_in_spec_1.f90 (PR 31215) contains
      r(:)(i:i) = achar(mod(i,32)+iachar('@'))

With glibc's MALLOC_CHECK_=2 the program simply segfaults.

Running the test case using valgrind gives the following error:

==12576== Invalid write of size 1
==12576==    at 0x4C23DD7: memmove (in /usr/lib64/valgrind/amd64-linux/vgpreload_memcheck.so)
==12576==    by 0x400ACF: __test1__test2 (result_in_spec_1.f90:23)
==12576==    by 0x4011D1: myfunc.1445 (result_in_spec_1.f90:42)
==12576==    by 0x400E9B: MAIN__ (result_in_spec_1.f90:35)
==12576==    by 0x4014EB: main (fmain.c:22)
==12576==  Address 0x404E8BE is 0 bytes after a block of size 6 alloc'd
==12576==    at 0x4C22D06: malloc (in /usr/lib64/valgrind/amd64-linux/vgpreload_memcheck.so)
==12576==    by 0x4E3ED38: _gfortrani_get_mem (memory.c:53)
==12576==    by 0x40119F: myfunc.1445 (result_in_spec_1.f90:42)
==12576==    by 0x400E9B: MAIN__ (result_in_spec_1.f90:35)
==12576==    by 0x4014EB: main (fmain.c:22)
Comment 1 Tobias Burnus 2007-04-11 20:26:10 UTC
Actually, it turned out that this is an out-of-bounds problem:

  character(len(ch)) :: chr(2)
  chr = test2 (1)

however, test(1) returns an array of the size (2*1+1)+1 = 4.

gfortran's -fbounds-check message is a bit misleading, though:

Fortran runtime error: Array bound mismatch for dimension 1 of array 'r' (in file '/home/tob/projects/gcc/gcc/testsuite/gfortran.dg/result_in_spec_1.f90', at line 22)

I think NAG f95's is better:

Rank 1 of array operand has extent 4 instead of 2
In MYFUNC, line 42 of result_in_spec_1.f90

Line 22 is:
  do i = 1, len(r)
where "r" is function result character(len=3),dimension(4). And line 42 is:
  chr = test2 (1)

The test case needs to be fixed, the question is whether the bounding error should be improved too?
Comment 2 Paul Thomas 2007-04-11 21:05:37 UTC
Subject: Re:  result_in_spec_1.f90: Invalid write

Tobias,
> however, test(1) returns an array of the size (2*1+1)+1 = 4.
>   
Thanks for spotting that one.  I'm just in the process of committing the 
fix as 'obvious', if a very sluggish network will permit.
> gfortran's -fbounds-check message is a bit misleading, though:
>
> Fortran runtime error: Array bound mismatch for dimension 1 of array 'r' (in
> file '/home/tob/projects/gcc/gcc/testsuite/gfortran.dg/result_in_spec_1.f90',
> at line 22)
>
> I think NAG f95's is better:
>
> Rank 1 of array operand has extent 4 instead of 2
> In MYFUNC, line 42 of result_in_spec_1.f90
>
> Line 22 is:
>   do i = 1, len(r)
> where "r" is function result character(len=3),dimension(4). And line 42 is:
>   chr = test2 (1)
>
> The test case needs to be fixed, the question is whether the bounding error
> should be improved too?
>   
Yes, the line number should certainly be put right.

Cheers

Paul

>
>   


Comment 3 Paul Thomas 2007-04-11 22:48:31 UTC
Subject: Bug 31538

Author: pault
Date: Wed Apr 11 22:48:15 2007
New Revision: 123725

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=123725
Log:
2007-04-11  Paul Thomas  <pault@gcc.gnu.org>

	PR testsuite/31538
	* gfortran.dg/result_in_spec_1.f90: Increase dimension from 2
	to 4 at line 38 to fix bounds problem.


Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/result_in_spec_1.f90

Comment 4 Paul Thomas 2007-04-11 22:52:54 UTC
I fixed the result_in_spec_1.f90 problem in trunk.

The diagnostic should remain as a problem, however.  I have hijacked this PR for it:)

Paul
Comment 5 Tobias Burnus 2007-08-10 09:43:36 UTC
Newly created test case. Expected:
* Extend (size) should be printed for "a = f()", as NAG f95 does

(I'm not sure that "different shape" is correct for the current a=b message; additionally, the A should not be capitalized and the D in different should.)


 integer :: a(-4:1), b(0:4)
 b = 5
! a(-4:1) = b(0:4) ! Error: different shape for Array
!                  ! assignment at (1) on dimension 1 (6/5)
!
! gfortran: Array bound mismatch for dimension 1 of array 'f'
! NAG f95:  Rank 1 of array operand has extent 5 instead of 2
 a(i:1) = f(b)
contains
  function f(x)
    integer :: x(:),f(size(x))
    f = x
  end function
end
Comment 6 kargls 2010-04-16 16:05:05 UTC
(In reply to comment #5)
> Newly created test case. Expected:
> * Extend (size) should be printed for "a = f()", as NAG f95 does
> 
> (I'm not sure that "different shape" is correct for the current a=b message;
> additionally, the A should not be capitalized and the D in different should.)
> 
> 
>  integer :: a(-4:1), b(0:4)
>  b = 5
> ! a(-4:1) = b(0:4) ! Error: different shape for Array
> !                  ! assignment at (1) on dimension 1 (6/5)
> !
> ! gfortran: Array bound mismatch for dimension 1 of array 'f'
> ! NAG f95:  Rank 1 of array operand has extent 5 instead of 2
>  a(i:1) = f(b)
> contains
>   function f(x)
>     integer :: x(:),f(size(x))
>     f = x
>   end function
> end
> 

The above is clearly invalid, so gfortran can go anything 
it wants.

laptop:kargl[236] gfc4x -o z -fcheck=all g.f90 -Wuninitialized 
g.f90: In function 'MAIN__':
g.f90:8:0: warning: 'i' may be used uninitialized in this function


Comment 7 kargls 2010-04-16 16:14:13 UTC
(In reply to comment #6)
> (In reply to comment #5)
> > Newly created test case. Expected:
> > * Extend (size) should be printed for "a = f()", as NAG f95 does
> > 
> > (I'm not sure that "different shape" is correct for the current a=b message;
> > additionally, the A should not be capitalized and the D in different should.)
> > 
> > 
> >  integer :: a(-4:1), b(0:4)
> >  b = 5
> > ! a(-4:1) = b(0:4) ! Error: different shape for Array
> > !                  ! assignment at (1) on dimension 1 (6/5)
> > !
> > ! gfortran: Array bound mismatch for dimension 1 of array 'f'
> > ! NAG f95:  Rank 1 of array operand has extent 5 instead of 2
> >  a(i:1) = f(b)
> > contains
> >   function f(x)
> >     integer :: x(:),f(size(x))
> >     f = x
> >   end function
> > end
> > 
> 
> The above is clearly invalid, so gfortran can go anything 
> it wants.
> 
> laptop:kargl[236] gfc4x -o z -fcheck=all g.f90 -Wuninitialized 
> g.f90: In function 'MAIN__':
> g.f90:8:0: warning: 'i' may be used uninitialized in this function
> 

Assuming 'i = -4' is missing in the programming, why is the 
runtime bounds check not a sufficient error message?  IMHO,
I think this pr can be closed.

Comment 8 Tobias Burnus 2010-04-16 17:24:44 UTC
(In reply to comment #7)
> Assuming 'i = -4' is missing in the programming

Well, almost any number would do, but "i = -4" is fine.

> why is the runtime bounds check not a sufficient error message?

Because:

  Fortran runtime error: Array bound mismatch for dimension 1 of array 'f'

is less helpful than our other gfortran out-of-bounds error messages; to be in line with those, one should have:

  Fortran runtime error: Array bound mismatch for dimension 1 of array 'f' (5/2)

or something like that which should also the two extends. Knowing the extends it can make it sometimes much easier to find the cause of the wrong extend.

In the comment of comment 5 you see how other gfortran errors look like "(%d/%d)" and how the NAG error message looks like for this case ("extent 5 instead of 2").
Comment 9 Dominique d'Humieres 2010-04-16 17:38:50 UTC
The run time error for

i = 0
a(i:1) = b(0:4)

is

At line 9 of file pr31538_db_2.f90
Fortran runtime error: Array bound mismatch, size mismatch for dimension 1 of array 'a' (2/5)

for 

i = 0
a(i:1) = f(b)

it is

At line 14 of file pr31538_db.f90
Fortran runtime error: Array bound mismatch for dimension 1 of array 'f'

In my opinion also the second case should produce an error similar to the first ones.
Comment 10 kargls 2010-04-16 18:25:44 UTC
(In reply to comment #9)
> The run time error for
> 
> i = 0
> a(i:1) = b(0:4)
> 
> is
> 
> At line 9 of file pr31538_db_2.f90
> Fortran runtime error: Array bound mismatch, size mismatch for dimension 1 of
> array 'a' (2/5)
> 
> for 
> 
> i = 0
> a(i:1) = f(b)
> 
> it is
> 
> At line 14 of file pr31538_db.f90
> Fortran runtime error: Array bound mismatch for dimension 1 of array 'f'
> 
> In my opinion also the second case should produce an error similar to the first
> ones.
> 

How's this?

Unpatched gfortran:

laptop:kargl[205] gfc4x -o z -fcheck=bounds g.f90
laptop:kargl[206] ./z
At line 9 of file g.f90
Fortran runtime error: Array bound mismatch for dimension 1 of array 'f'

With a new patch:

laptop:kargl[211] gfc4x -o z -fcheck=bounds g.f90
laptop:kargl[212] ./z
At line 9 of file g.f90
Fortran runtime error: Dimension 1 of array 'f' has extent 5 instead of 6
laptop:kargl[213] 


Comment 11 kargls 2010-04-17 21:06:10 UTC
Subject: Bug 31538

Author: kargl
Date: Sat Apr 17 21:05:53 2010
New Revision: 158474

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=158474
Log:
2010-04-17  Steven G. Kargl  <kargl@gcc.gnu.org>

   PR fortran/31538
   * gfortran.dg/bounds_check_fail_4.f90: Adjust error message.
   * gfortran.dg/bounds_check_fail_3.f90: Ditto.

2010-04-17  Steven G. Kargl  <kargl@gcc.gnu.org>

   PR fortran/31538
   * fortran/trans-array.c (gfc_conv_ss_startstride): Remove the use of
   gfc_msg_bounds by using 'Array bound mismatch' directly.
   (gfc_trans_dummy_array_bias):  Remove the use of gfc_msg_bounds.  Reword
   error message to include the mismatch in the extent of array bound.
   * fortran/trans.c: Remove gfc_msg_bounds.  It is only used in one place.
   * fortran/trans.h: Remove extern definition of gfc_msg_bounds.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/bounds_check_fail_3.f90
    trunk/gcc/testsuite/gfortran.dg/bounds_check_fail_4.f90

Comment 12 kargls 2010-04-17 22:12:53 UTC
Fixed on 4.5 branch and trunk.
Comment 13 kargls 2010-04-17 22:22:16 UTC
Subject: Bug 31538

Author: kargl
Date: Sat Apr 17 22:22:02 2010
New Revision: 158476

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=158476
Log:
2010-04-17  Steven G. Kargl  <kargl@gcc.gnu.org>

   PR fortran/31538
   * gfortran.dg/bounds_check_fail_4.f90: Adjust error message.
   * gfortran.dg/bounds_check_fail_3.f90: Ditto.

2010-04-17  Steven G. Kargl  <kargl@gcc.gnu.org>

   PR fortran/31538
   * fortran/trans-array.c (gfc_conv_ss_startstride): Remove the use of
   gfc_msg_bounds by using 'Array bound mismatch' directly.
   (gfc_trans_dummy_array_bias):  Remove the use of gfc_msg_bounds.  Reword
   error message to include the mismatch in the extent of array bound.
   * fortran/trans.c: Remove gfc_msg_bounds.  It is only used in one place.
   * fortran/trans.h: Remove extern definition of gfc_msg_bounds.

Modified:
    branches/gcc-4_5-branch/gcc/fortran/ChangeLog
    branches/gcc-4_5-branch/gcc/fortran/trans-array.c
    branches/gcc-4_5-branch/gcc/fortran/trans.c
    branches/gcc-4_5-branch/gcc/fortran/trans.h
    branches/gcc-4_5-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_5-branch/gcc/testsuite/gfortran.dg/bounds_check_fail_3.f90
    branches/gcc-4_5-branch/gcc/testsuite/gfortran.dg/bounds_check_fail_4.f90