The default behavior of the ecobol compiler is to not perform any extra code to verify if the index provided while accessing a table element is within the bounds. Also the default behavior of the java runtime causes an ArrayIndexOutOfBounds java exception.
However there may be situations where the program may depend on external data for index value in which case you might need to trap and debug such cases. Also some COBOL programming languages allow for out of bounds access to continue without any exception.
To support these situations you have the compiler option -out:indexcheck and the default option -out:noindexcheck.
- -out:noindexcheck (default) indicates that no extra code be generated for index checking.
- -out:indexcheck indicates that the compiler will generate extra code to check that the index used is within range.
The runtime has the CHECKINDEX option which is to be used along with compiler option -out:indexcheck. It's value determines what happens when a index out of bounds situation is detected.
- CHECKINDEX=TRUE will cause the runtime to throw an ArrayIndexOutOfBoundsVariable CobolException and terminate the program.
- CHECKINDEX=FALSE (default) When set to false (default), it causes the runtime to return a ArrayIndexOutOfBoundsVariable object as the referenced item and continues execution.
ArrayIndexOutOfBoundsVariable represents a garbage/undetermined value. Its default behavior is to return 0/null when accessed and throw a CobolException if an attempt is made to move a value to it.
The runtime has the option GARBAGE which is used to determine the actions associated with the creation and interaction with such an ArrayIndexOutOfBoundsVariable object. The GARBAGE runtime may be set as a JVM argument (-DGARBAGE=xxx), as a program parameter (GARBAGE=xxx), as a JCL PARM parameter (GARBAGE=xxx) or in the cobol.config runtime set of parameters referenced by the program. The values are:
- GARBAGE=DEFAULT causes access of the ArrayIndexOutOfBoundsVariable to throw an exception.
- GARBAGE=VERBOSE is similar to the DEFAULT behavior but also logs a message if logging is set to true. The messages get logged when a ArrayIndexOutOfBounds error occurs and also when the ArrayIndexOutOfBoundsVariable throws an exception.
- GARBAGE=QUIET (default) s same as DEFAULT but does not throw an exception when a value is moved to the object.
So if you wish to identify and remove index out of bounds situations, first compile your application with -out:indexcheck. Then run your application. If you wish to trap each individual error as it occurs, make sure you specify the runtime option CHECKINDEX. The CHECKINDEX=TRUE will cause the runtime to throw an exception and stop the program each time it encounters an ArrayIndexOutOfBounds situation.
If you wish to run the entire application and just get a listing of all the locations at which the ArrayIndexOutOfBounds occurs then set the runtime option CHECKINDEX=FALSE along with GARBAGE=VERBOSE and LOG=TRUE. This will give a listing of all locations at which the out-of-bounds error occurs as well as locations where the ArrayIndexOutOfBoundsVariable may raise an exception.
Once you have identified and remove the error causing cases, you can then recompile the application with the default -out:noindexcheck.
For applications that require out-of-bounds cases to be allowed, you can retain the compiler option -out:indexcheck and use the runtime option CHECKINDEX=FALSE.
Note: Unlike other systems there is no way to step outside the bounds of an array and either access or modify other variables -- the Java semantics prevent this. The indexcheck compile-time setting and CHECKINDEX and GARBAGE runtime settings simply allow you to determine what happens should your program contains such errors.