Silverfrost Forums

Welcome to our forums

Bounds checking not working with array sections.

21 Nov 2014 8:10 #15127

In the following code you should get a run-time out of bounds error when compiled with /BOUNDS_CHECK (and with /CHECK, /CHECKMATE) but you don't.

The subroutine attempts to print out the sum of the first n values in array a, but contains a bug and prints out the sum of 10 elements. With bounds checking on there should be an error on the call to sum and on the access to the array section (copied to b).

You do get an error if you try to access, say, a(10), but not when you access the section.

Hope this helps.

subroutine add(a, n)
   real a(n), b(10)
   
   ! Should get a run time error here (but you don't)
   print *, 'Sum is ', sum(a(1:10))
   b = a(1:10)
   print *, 'Sum is ', sum(b(1:10))
end

program main
   real b(10)
   b = (/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0/)
   
   ! Print out sum of first 5 values (should be 15.0)
   call add(b, 5)
end
21 Nov 2014 10:49 #15128

David,

I have found that when using FTN95 with /check, there is array size information stored that is more extensive that is implied by the fortran syntax. I can't recall the exact code but something like the following will fail in sdbg when I=11. I have also seen this happen when a part of the array is used, say call suba(aa(7)) would fail for I=5. At least that is what I remember!!

Program aaa
   integer*4 aa(10)
   call suba (aa)
end

subroutine suba (bb)
  integer*4 bb(*)
  do I = 1,11
     bb(I) = I
  end do
end
22 Nov 2014 7:52 #15130

Thanks. I have logged this for investigation.

22 Nov 2014 10:10 #15131

Thanks Paul.

John, thanks too. The compiler is very good at spotting out of bounds errors in assumed size arrays. This bug arises when an array section is accessed which goes out of bounds of the array.

19 Jan 2015 1:19 #15340

This is not so much a bug as a limitation of the current bounds checking feature which currently works on the assumption that the array section size is constant (i.e. a known constant at compile time).

I can put this on the wish list but at a quick glance I don't know how big a task this would be.

19 Jan 2015 5:19 #15343

Thanks for looking.

I assumed that since the bounds checking could diagnose the out of bounds errors for the element array accesses a(5), a(6), a(7), a(8), a(9), a(10) in the above example, it should also be able to diagnose any array sections which included these elements, such as a(5:10) or a(1:10), i.e. do the bounds checking in a loop.

There is probably a simple way to generalize dynamic bounds check to include array sections in a more succinct way. I will have a think, or find out if there is anything published on this and post my findings here (it will least be interesting). It is, of course, implemented in other compilers so we know it is possible in principle.

For now add this it to the wish list and if it proves to difficult too implement then so be it.

Please login to reply.