Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

table overflow when using PERFORM VARYING

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading