https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126106

            Bug ID: 126106
           Summary: gcobol ICE (segfault) on a table SORT with no KEY
                    phrase
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: cobol
          Assignee: unassigned at gcc dot gnu.org
          Reporter: peeterjoot at protonmail dot com
  Target Milestone: ---

Created attachment 64935
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64935&action=edit
sort-table-no-key.cob (also inline in the bug report)

# Summary

`gcobol` crashes with an internal compiler error (segmentation fault) on a
table (format-2)
`SORT` that has no `ASCENDING`/`DESCENDING KEY` phrase at all, e.g. `SORT TBL`.
Note that
`gcobol` *accepts* an operand-less key phrase (`SORT TBL DESCENDING KEY.`), so
this is
specifically the case where the entire key phrase is absent.

# Environment

- `gcobol (GCC) 17.0.0 20260604 (experimental)`

# Steps to reproduce

Compile the following program:

```cobol
       IDENTIFICATION DIVISION.
       PROGRAM-ID. CRASH3.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 G.
           02 TBL OCCURS 5 TIMES.
           03 X PIC X.
           03 Y PIC 9.
       PROCEDURE DIVISION.
           SORT TBL
           .
           STOP RUN
           .
```

```
gcobol -ffixed-form -c sort-table-no-key.cob -o /dev/null
```

# Actual result

```
cobol1: internal compiler error: Segmentation fault
0x239b373 internal_error(char const*, ...)
        gcc/diagnostic-global-context.cc:787
0x109185b crash_signal
        gcc/toplev.cc:325
0x9d9bdc cbl_key_t::cbl_key_t(cbl_occurs_key_t const&)
        gcc/cobol/symbols.cc:5085
0x98375f yyparse()
```

The crash is in `cbl_key_t::cbl_key_t(cbl_occurs_key_t const&)`
(`cobol/symbols.cc:5085`),
reached from the parser while building the sort key for the keyless `SORT`.

# Expected result

Either accept `SORT TBL` (sorting on the whole table element, as the
operand-less
`DESCENDING KEY` form does) or emit a diagnostic that a key is required — but
not a compiler
crash.

# Notes

Reduced from a larger program produced by a COBOL source-to-source transformer.
The original
statement was `SORT TBL DESCENDING KEY.` (an operand-less key phrase, which
gcobol accepts);
the transformer erroneously dropped the empty `DESCENDING KEY`, leaving the
bare `SORT TBL`
that triggers this crash. The crash is independent of how the input was
produced.

Reply via email to