Skip to content

Commit ce54461

Browse files
committed
Added a function to copy one array to another without causing bound errors. This function was to be used in place of the do loops that use common size and diliberately access outside the range
1 parent 70f5355 commit ce54461

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

trickBoundsChecking.F90

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
! Used for the COMMON_INIT macro in model_macros_f.h
3+
! As is, bounds checks don't work on the code it generates
4+
! but aparently it's still right.
5+
! According to Michel Valin, its a very dirty trick to
6+
! generate initialization code for common blocks
7+
! with an unknown and variable number of elements using
8+
! a macro generator.
9+
10+
subroutine fill_common(what,value,n)
11+
IMPLICIT NONE
12+
integer, dimension(-1:n), intent(INOUT) :: what
13+
integer, INTENT(IN) :: n
14+
integer, intent(IN) :: value
15+
16+
! Except the last two lines, this is all debug stuff
17+
18+
integer :: i
19+
character(len=1024) :: tmp
20+
character(len=50) :: tmp2
21+
write(tmp, *) "In fill_common: value,n=", value,n, ", what="
22+
do i=-1,n
23+
write(tmp2,'(A,I2,A)') ", n=", i, ':'
24+
tmp=trim(tmp)//tmp2
25+
write(tmp2,*) what(i)
26+
tmp=trim(tmp)//tmp2
27+
enddo
28+
call fastdebug(tmp, __LINE__)
29+
30+
what(1:n)=value
31+
return
32+
end
33+
34+
! This is used for situations like,
35+
! "do i=1,COMMON_SIZE(...)" which will trigger bounds errors
36+
! (as this is part of GM's indexing tricks to try to access
37+
! the first element of a COMMON variable)
38+
! So, replace the do loop with a call to this function
39+
subroutine copy_with_offset_1(src,dest,n)
40+
IMPLICIT NONE
41+
42+
integer, intent(IN), dimension(0:n) :: src
43+
integer, intent(OUT), dimension(1:n) :: dest
44+
integer, intent(IN) :: n
45+
46+
dest(1:n) = src(1:n)
47+
48+
return
49+
endsubroutine copy_with_offset_1

0 commit comments

Comments
 (0)