Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
← Back to Debugging
Debugging | Intermediate | COBOL

Fix STRING Overflow

Problem Description

STRING produces truncated result:
01 WS-RESULT PIC X(10).
STRING "HELLO" " " "WORLD" INTO WS-RESULT.

Expected Output

Should show "HELLO WORLD" (11 chars)

Hints

Result field must be large enough for concatenated string.

Solution

IDENTIFICATION DIVISION. PROGRAM-ID. FIXSTR. DATA DIVISION. WORKING-STORAGE SECTION. * BUG: WS-RESULT too small (10 chars for 11 char result) * FIX: Make it at least 11 characters 01 WS-RESULT PIC X(20). 01 WS-PTR PIC 9(2) VALUE 1. PROCEDURE DIVISION. MOVE SPACES TO WS-RESULT. STRING "HELLO" DELIMITED SIZE " " DELIMITED SIZE "WORLD" DELIMITED SIZE INTO WS-RESULT WITH POINTER WS-PTR ON OVERFLOW DISPLAY "STRING TOO LONG!" END-STRING. DISPLAY "RESULT: [" WS-RESULT "]". STOP RUN.

Explanation:

Result field must fit entire concatenated string. Use ON OVERFLOW to detect.