forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Bounds checking not working with array sections.

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Fri Nov 21, 2014 9:10 pm    Post subject: Bounds checking not working with array sections. Reply with quote

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.

Code:

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

_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Fri Nov 21, 2014 11:49 pm    Post subject: Reply with quote

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!!

Code:
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
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7923
Location: Salford, UK

PostPosted: Sat Nov 22, 2014 8:52 am    Post subject: Reply with quote

Thanks. I have logged this for investigation.
Back to top
View user's profile Send private message AIM Address
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Sat Nov 22, 2014 11:10 am    Post subject: Reply with quote

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.
_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7923
Location: Salford, UK

PostPosted: Mon Jan 19, 2015 2:19 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Mon Jan 19, 2015 6:19 pm    Post subject: Reply with quote

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.
_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group