I'm doing a simple PERFORM VARYING WS-IND FROM 1 BY 1
Bellow is my code:
PERFORM VARYING WS-INDICE FROM 1 BY 1
UNTIL WS-INDICE > 9
OR TEST-OBJEC(WS-INDICE) = SPACES
MOVE TEST-OBJEC(WS-INDICE) TO WS-VAL-OBJEC(WS-INDICE)
MOVE TEST-DATE(WS-INDICE) TO WS-DAT-OBJEC(WS-INDICE)
END-PERFORM
Index was declared like this:
01 WS-INDICE PIC 9(01).
Bellow the declaration of my array
10 TEST-GROUP
OCCURS 009.
15 TEST-OBJEC PICTURE X(01).
15 TEST-DATE PICTURE X(8).
10 FILLER PICTURE X(56).
My problem that the WS-INDICE arrive until 9 and restart from 0.
Problem description
the WS-INDICE arrive until 9 and restart from 0
Code
VARYING WS-INDICE FROM 1 BY 1
UNTIL WS-INDICE > 9
Problem reason
A compiler that both does not warn about conditions that may never be true and additional also overflows (the COBOL standard says "raise exception and don't change value").
... because of the definition WS-INDICE PIC 9(01).
This variable cannot hold more than one digit should never be greater than 9 (leaving out the possibility that someone moves x'99'
to that, inserting invalid data).
Adjust the definition to use a PIC 99
and everything is fine.
And possibly consider to use constants instead:
10 TEST-GROUP
- OCCURS 009.
+ OCCURS WS-INDICE-MAX.
PERFORM VARYING WS-INDICE FROM 1 BY 1
- UNTIL WS-INDICE > 9
+ UNTIL WS-INDICE > WS-INDICE-MAX
For this to work you'd need support for either a common PC extension "level 78 constants" or a compiler that supports "COBOL 2002 constants":
78 WS-INDICE-MAX VALUE 9.
01 integer-constant CONSTANT AS 9.
... and possibly consider to use another compiler for better warnings (you don't need to use that otherwise), for example with GnuCOBOL 3+ and cobc -W -frelax-syntax-checks -fsyntax-only
:
warning: 'WS-INDICE' may not be GREATER THAN 9