Fix an S0C4 Protection Exception in a z/OS Program
Diagnose an S0C4 in a COBOL program on z/OS: read the reason code, find the statement, and check subscripts, linkage parameters and storage with SSRANGE.

An S0C4 is the abend that tells you a program touched storage it should not have. It is less specific than an S0C7, which names bad numeric data, and it covers more ground: a table subscript past the end, a pointer that was never set, a parameter list that does not match, a file that was not open, a program that was called but never loaded. The code is the symptom, and the reason code and the failing statement are the diagnosis. This post gives a method for reading an S0C4 in a COBOL program on z/OS 3.1 with Enterprise COBOL for z/OS 6.4: what the reason codes mean, how to find the statement and the address that was wrong, the five usual causes with an example of each, and the compiler and runtime options that turn an S0C4 into a clear message. The family of codes it belongs to is in the complete guide to z/OS abend codes and how to fix them.
What the code means
S0C4 is the completion code for the protection exception and, on z/OS, also the code for some addressing errors, and the reason code tells them apart. In the job log:
IEF450I PAYRPT RUNPAY - ABEND=S0C4 U0000 REASON=00000004The reason code of a program-check abend S0C4 is the cause of the exception:
| Reason | Exception | Meaning |
|---|---|---|
| 4 | Protection | The program tried to read or write storage that is protected from it: storage it does not own, or storage with a different key |
| 10 | Segment translation | The address is in a region of the address space that has no storage behind it at all |
| 11 | Page translation | The address is in a region that is not backed by an allocated page |
In practice, reason 4 means the address pointed into somebody else's protected storage, such as the system's or another subsystem's, and reasons 10 and 11 mean it pointed at a place with nothing there. A very low address, near zero, is typical of a pointer that was never set. A very high or random-looking address is typical of a damaged pointer or a wild subscript. The address itself is therefore the second clue, after the code.
Step 1: find the statement and the address
For a COBOL program under Language Environment, the runtime message reports the exception and the place:
CEE3204S The system detected a protection exception (System Completion Code=0C4).
From compile unit TBLLOAD at entry point TBLLOAD at statement 142
at compile unit offset +000004B0 at address 1B7F5AB0.The statement number points to the line in the compile listing, given OFFSET or LIST. The dump has the registers: the failing instruction reads or writes through a register, and the value of that register is the bad address. For example, the dump shows the failing instruction as an MVC (move characters) with the base register 9, and register 9 holds 00000000, so the program was working at an address built from a null base.
Two facts to extract from the dump are the registers at the failure and the program's storage layout from the listing's data map. Together they tell you which data item the instruction was using, and what its address should have been.
Step 2: classify the cause
Five causes cover most S0C4 abends in COBOL, and each has a signature.
1. A subscript out of range
The program indexes a table past its end. The reference lands in whatever follows the table, which may be another field, unused storage or protected storage.
IDENTIFICATION DIVISION.
PROGRAM-ID. SUBSCR.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-ENTRY PIC X(10) OCCURS 5 TIMES.
01 WS-IDX PIC S9(4) COMP VALUE 9000.
PROCEDURE DIVISION.
MOVE 'ABC' TO WS-ENTRY(WS-IDX)
GOBACK.The table has 5 entries, and the program stores into entry 9,000, which is 90,000 bytes past the start of the table. Without a range check, the store goes to that address, and if the page is not the program's or does not exist, the result is an S0C4. If the subscript had been only slightly past the end, the store would have damaged the next field and the abend would come later and elsewhere, which is the harder case.
With the SSRANGE compiler option on and the CHECK(ON) runtime option, the program ends at the statement with a message instead:
IGZ0006S The reference to table WS-ENTRY by verb number 1 on line 9
addressed an area outside the region of the table.The message names the table and the line, which is the whole diagnosis.
2. An unset or invalid pointer or address
A SET ADDRESS OF, a POINTER item or a LINKAGE SECTION item whose address was never set points at storage 0 or garbage. In COBOL, any LINKAGE SECTION record has no storage of its own: it maps storage supplied by the caller. If the caller did not pass it, or passed fewer parameters than the program declares, the address is whatever was in the parameter list.
3. A parameter mismatch in a CALL
The calling program passes two parameters and the called program declares three in its PROCEDURE DIVISION USING. The third one's address is read from past the end of the parameter list, which is garbage, and the first reference to it abends. A mismatch in length is just as bad: the caller passes a 10-byte field and the callee treats it as 100 bytes, so moves past the end overwrite storage.
* caller
CALL 'SUBPGM' USING WS-A WS-B
* callee
PROCEDURE DIVISION USING LS-A LS-B LS-C.Check every CALL against the called program's USING list, in count, order and size. A shared copybook for the parameter block prevents drift.
4. A file that is not open, or an I/O on a closed file
A READ or WRITE on a file that was never opened, or was already closed, can raise an abend, or return a file status that the program ignored and then used a record area that was never filled. The usual signature is that the abend happens at the first file statement. Check the file status after OPEN as a rule.
5. A called program that is not loaded or not found
A dynamic CALL to a program name that cannot be located raises S806 when the load fails. A static CALL to a module that the linkage editor left unresolved gives a branch to address zero at run time, an S0C4 or S0C1 depending on where the zeros point. Check the link-edit listing for unresolved external references, which are warnings that are easy to miss.
Step 3: confirm with the dump
With a suspect from the list, confirm it. For a subscript, find the index value in the dump and compare it with the table's OCCURS. For a pointer, check whether the register used by the failing instruction holds zero or a value outside the program's storage ranges. For a call, compare the parameter list address in register 1 at the time of the call, and count the words. The listing's data map gives the address of every working-storage item, so a register value can be matched to a field name or shown to match none.
If the failing code is in a called module, the traceback in the CEEDUMP shows the calling chain, from the failing program back to the main program with the statement number in each, which tells you which call passed the bad parameter. A request for the dump is TERMTHDACT(DUMP) or a call to CEE3DMP from a handler.
The fix
Each cause has a direct fix.
Subscript. Validate the index before use:
IF WS-IDX >= 1 AND WS-IDX <= 5. Size the table to the data, or process in a loop that stops at the count of entries actually loaded. Compile withSSRANGEin test.Pointer. Initialize pointers to
NULLand test them before use, and setADDRESS OFfor every linkage item you use.Parameters. Align the
CALLandUSINGlists, and share a copybook for the layout. Where a program takes optional parameters, check the parameter count and useOMITTEDtests.Files. Check file status after
OPEN,READandWRITE, and stop on an unexpected value.Calls. Resolve all externals at link-edit, put the load library on
STEPLIB, and prefer a dynamicCALLwithON EXCEPTIONfor modules that may be absent.
Options that make S0C4 easier
The best defense is to make the bad reference fail at the statement with a message.
SSRANGE makes the compiler generate subscript and reference-modification checks. At run time, CHECK(ON) in the Language Environment options enables them, and a violation produces IGZ0006S or a related message and an abend at the offending statement. The cost is a compare per reference, so use it in test builds and decide about production case by case.
STORAGE(00,NONE,NONE,...) among the Language Environment STORAGE options sets initial storage to a known value, so an uninitialized field is zero, not random, and makes the pointer case fail predictably. HEAPCHK checks heap storage control blocks for overlays.
TEST and the z/OS Debugger let you run the program to the failing line and inspect the table and the index, which is the most direct way to see a bad subscript.
When the S0C4 is not in your code
Sometimes the failing instruction is in a system or vendor module, and your program is the caller. The traceback shows that the top frame is not yours. Then the question is what your program passed in: a bad parameter that the module followed to a protected address. If your parameters are correct, the problem belongs to the module's owner, who needs the dump, the job log and the module's modification level.
An S0C4 whose address is plausible and whose statement looks harmless is often a storage overlay: an earlier bad write damaged a pointer or a count, and this statement is the victim. Look for what writes to the damaged field, check subscripts in the program with SSRANGE, and review recent changes to the code that handles tables.
The reason codes and messages are in the z/OS MVS System Codes and Language Environment Runtime Messages manuals on the IBM Documentation site.


