COBOL Troubleshooting
S0C4 protection exception
S0C4 is a protection exception: the program tried to read or write a storage address it does not own. It usually means a wild subscript, pointer or branch.
What it means
- S0C4 (protection exception) means the program touched memory outside its own region.
- In COBOL the usual suspects are a table subscript out of range or an uninitialized pointer.
- It can also come from branching to a bad address after storage gets overlaid.
Common causes
- OCCURS subscript larger than the table size, or zero/negative.
- Reference modification with a start position past the end of the field.
- Uninitialized pointer used in SET ADDRESS or CALL.
- Storage overlay: a previous MOVE wrote past a field and corrupted a save area or pointer.
- Calling a subprogram that was never loaded (bad load module).
How to diagnose
- From the dump, note the failing address and the registers; map the PSW to the compiler listing offset.
- Check every subscript and reference-modification near the failing statement.
- Look at the registers for addresses that look like data (e.g. packed fields) rather than code addresses.
- If it fails intermittently, suspect a subscript driven by input data.
How to fix it
- Bounds-check subscripts before use: IF WS-IDX > 0 AND WS-IDX <= TABLE-SIZE.
- Initialize pointers to NULL and test them before dereferencing.
- Fix the overlaying MOVE — the S0C4 is often the victim, not the culprit.
- Recompile with SSRANGE to catch subscript errors in test before production.
Example: Bounds-check every subscript
IF WS-IDX > 0 AND WS-IDX <= MAX-ITEMS
MOVE ITEM-PRICE (WS-IDX) TO WS-PRICE
ELSE
DISPLAY "Bad subscript: " WS-IDX
MOVE 16 TO RETURN-CODE
STOP RUN
END-IF.
Compile test builds with the SSRANGE option to trap these before production.
Interview tip
- Q: How would you troubleshoot a S0C4 protection exception in production?
- A: I would map the failing PSW address to the compiler listing, inspect subscripts and pointers near that statement, check the dump registers for a wild address, and fix the out-of-range subscript or the storage overlay causing it.
Prevention checklist
- Always validate subscripts against the OCCURS size.
- Use SSRANGE in test compiles.
- Initialize pointers and check for NULL before use.
