Quantcast
Channel: Visual COBOL Knowledge Base
Viewing all articles
Browse latest Browse all 214

Multi-dimensional Tables: Subscript out of Range

$
0
0
Current Revision posted to Visual COBOL Knowledge Base by NickCook on 10/11/2016 2:47:46 PM

Problem:

When attempting to access a multi-dimensional table outside of its subscript range, a “subscript out of range” error is not always generated at runtime.

Given an array M_TABLE:

01    M_TABLE OCCURS 8.

02   D-TABLE OCCURS 8.

       03   TABLE_DATA   PIC X(5).

And the Procedure Division statement:

MOVE “DATA”   TO TABLE_DATA (x, y).

 

If the values of x and y were 1 and 9 respectively, a ‘subscript out of range’ error is not generated at runtime even though the value in y is greater than the second dimension of the table.

 

The compiler directives that affect subscripting are SSRANGE and BOUND.

http://documentation.microfocus.com/help/topic/com.microfocus.eclipse.infocenter.visualcobol.vs2013/HRCDRHCDIR6Q.html?resultof=%22%73%73%72%61%6e%67%65%22%20

http://documentation.microfocus.com/help/topic/com.microfocus.eclipse.infocenter.enterprisedeveloper.vs2012/HRCDRHCDIR1H.html?resultof=%22%62%6f%75%6e%64%22%20

 

The comments in the BOUND section state:

For multi-dimensional tables, only the composite subscript is checked. If any of the individual subscripts or indices is beyond its limit, but the reference remains within the table, no error is produced.

 

In the example above, as the combined subscript (ie 1,9) is still within the physical bounds of the array, no error is produced at runtime, and the data will still be stored within the tables boundaries. This action means that any data already stored in the table could be overwritten and lost. Unpredictable results may be obtained when this area of the table is accessed further in the program.

Resolution

As the Micro Focus compiler and run time processor can allow an individual subscript of a multi-dimensional table to be outside the range of its own occurrence, it is recommended that checking of subscripts is always coded to ensure this situation does not occur.

For example, in the above table:

 

IF (x < 1 OR > 8)

OR (y < 1 OR > 8)

THEN

               PERFORM SUBSCRIPT_RANGE_ERROR

               GO TO EXIT_PARA

END_IF.


Viewing all articles
Browse latest Browse all 214

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>