Protecting against Stack Overflow

Another task attribute related to process stack size is STACKLIMIT. The STACKLIMIT value places a limit on the size to which the process stack can be stretched. If a process cannot proceed further without exceeding this limit, the system discontinues it and returns the error message “STACK OVERFLOW.”

The STACKLIMIT task attribute defaults to a value of 6000 words for most programs. (The system uses a higher default for certain types of programs that are likely to need a larger stack size.) It is unlikely that any process stack will reach the default STACKLIMIT size if it is running as intended. The “STACK OVERFLOW” error usually indicates that a process has entered into an infinite loop of recursive procedure calls. Because each procedure call adds an activation record to the process stack, the process stack quickly exceeds the STACKLIMIT value.

The PERFORM statement in COBOL also adds to the size of the process stack, and an infinite loop of recursive PERFORM statements also causes a “STACK OVERFLOW” error. (This is true even though a PERFORM statement does not create an activation record. A PERFORM statement adds a different type of structure to the process stack.) By contrast, GO TO statements do not increase the process stack size and so cannot cause a “STACK OVERFLOW” error.

If a process receives a “STACK OVERFLOW” error, and you determine that the process was running as intended, then you can remedy the problem by assigning a higher value to the STACKLIMIT task attribute before initiating the process. The highest value STACKLIMIT can be set to about 64000 words.

Because a process stack is built exclusively in save memory, the save memory restrictions discussed in the next subsection also effectively limit the size of the process stack.