In native code (gnt , dll , exe , so) Micro Focus COBOL will optimize subscript access. This optimization is controlled via the BOUNDOPT compiler directive. The optimizations can impact code migrated from other platforms.
Consider this case where a table is passed (by the caller) in Linkage to the callee. The caller code is:-
working-storage section.
01 ws-data.
03 ws-data1 pic x(80).
03 ws-data2 pic x(80).
03 ws-data3 pic x(80).
03 ws-data4 pic x(80).
03 ws-data5 pic x(80).
03 ws-data6 pic x(80).
03 ws-data7 pic x(80).
03 ws-data8 pic x(80).
03 ws-data9 pic x(80).
03 ws-data10 pic x(80).
03 ws-data11 pic x(80).
03 ws-data12 pic x(80).
03 ws-data13 pic x(80).
03 ws-data14 pic x(80).
03 ws-data15 pic x(80).
03 ws-data16 pic x(80).
03 ws-data17 pic x(80).
03 ws-data18 pic x(80).
03 ws-data19 pic x(80).
03 ws-data20 pic x(80).
01 ws-table.
03 ws-table-element pic x(80) occurs 100.
procedure division.
start-it section.
initialize ws-data
initialize ws-table
call "callee" using ws-table
if ws-data20 not = spaces
display "Error"
end-if
goback.
The callee code is:-
$set nobound boundopt
working-storage section.
01 ws-sub pic 9(4).
linkage section.
01 lnk-table.
03 lnk-table-element pic x(80) occurs 2.
procedure division using lnk-table.
start-it section.
perform varying ws-sub from 1 by 1 until ws-sub > 100
move all "A" to lnk-table-element(ws-sub)
end-perform
goback.
When control is returned to the caller variable ws-data20 contains all "A"s. Although a table with 100 items was passed the table is defined in the callee as occurs 2. While this is poor practice this code can produce the desired results on some platforms.
The corruption occurs when table subscript 10 is used. The native compiler optimizes access to the array and only uses the most significant byte of the subscript due to the occurs 2. This causes an array update at element 0. This corrupts the ws-data20 variable.
To get the desired results in this case the NOBOUNDOPT directive needs to be used. When this is used ws-data20 is unaffected and all 100 elements of the array are populated.
#tablecorruptionsubscriptoptimization



