Menu ▾ ▴

#881 Wrong redefinition target for same named variables

GC 3.2
closed
5 - default
2023-05-05
2023-03-03
Chris G
No

The following example code compiled on 3.1, but does not on 3.2 anymore, resulting in the error "REDEFINES must follow the original definition".

       01 WS.
          05 A             PIC X.
          05 A REDEFINES A PIC X.
          05 FILLER        PIC X.

While the example looks like it should not compile because one has effectively defined an unreachable variable A (since even a fully qualified name would be ambiguous, e.g. A in WS), such a situation can arise when using copybooks with the REPLACING phrase, as an example:

COPYBOOK.cpy:

          05 PREFIX1-A                     PIC X.
          05 PREFIX2-A REDEFINES PREFIX1-A PIC X.

TESTPGM.cbl:

       01 WS.
          COPY 'COPYBOOK' REPLACING ==PREFIX1-A== BY ==A==
                                    ==PREFIX2-A== BY ==A==.
          05 FILLER  PIC X.

The FILLER in the examples is only used to trigger the error, because the code which would otherwise raise the error directly accesses f->redefines->sister which would otherwise be NULL (field.c:1322). The attached patch prevents the redefining variable from finding itself as a redefinition target, because otherwise with f = 05 A REDEFINES A ... f == f.redefines would be true.

1 Attachments

Related

Commit: [r5374]

Discussion

  • Simon Sobisch

    Simon Sobisch - 2023-05-05
    • labels: --> cobc, redefines
    • status: open --> closed
    • assigned_to: Simon Sobisch
    • Group: unclassified --> GC 3.2
     
  • Simon Sobisch

    Simon Sobisch - 2023-05-05

    Thank you for raising this issue. I can confirm that older versions did not raise even a warning by default here (you only get the -Wredefinition on explicit request) and that this is also valid COBOL "per standard" - as long as you don't try to access A, everything should be fine.

    Your fix is also fine - applied with swapping the order, ChangeLog entry and changelog in [r5036].

     

    Related

    Commit: [r5036]


Log in to post a comment.