Menu ▾ ▴

#4211 Comma expression in a case label crashes SDCC

closed-fixed
None
Front-end
5
1 day ago
3 days ago
No

1: Sample code that reproduces the problem.

void f(int x)
{
  switch (x)
  {
    case (0, 1):
      break;
  }
}

2: Exact command used to run SDCC on this sample code.

sdcc -mz80 --std=c2y --nostdinc -S bug.c -o bug.asm

3: SDCC version tested.

SDCC 4.6.4 #17068 (Linux), tested 23 October 2026.

4: Observed and expected behaviour.

SDCC exits with status 1 after printing:

Caught signal 11: SIGSEGV

No source-level constraint diagnostic is emitted.

N3886 6.8.5.3 paragraph 3 requires an integer constant expression or a constant range expression for a case label. (0, 1) is neither. SDCC should diagnose the invalid case label and recover without crashing.

Reference: N3886.

5: Additional checks.

The corresponding integer case label and a conditional expression containing a comma only in an unevaluated branch compile without diagnostics on all five targets. GCC 13.3.0 rejects the invalid label and accepts both valid cases.

All invalid cases reproduce on Z80, MCS51, STM8, PDK14 and MOS6502 in C99, C11, C23 and C2Y modes. GCC was invoked with -std=c2x -pedantic-errors -fsyntax-only. GCC 16.2 and Clang were not tested.

Discussion

  • Christopher Bazley

    Please consider the attached candidate bugfix created by Codex. One of the tests provokes an irrelevant warning from GCC. I'm not sure whether it's better to use IGNORE(GCC) (as in the patch) or #pragma GCC diagnostic ignored "-Wunused-value". The latter is a lot more code.

     

    Last edit: Christopher Bazley 1 day ago
  • Maarten Brock

    Maarten Brock - 1 day ago

    Since you require C2y in the valdiag test, I think you need to make an exception for compilers that do not claim C2y compliance.

    Other than that it looks OK. I don't mind the IGNORE(GCC).

     

    Last edit: Maarten Brock 1 day ago
    • Christopher Bazley

      Thanks for your review, @maartenbrock. Case ranges are a longstanding feature of GCC, which is presumably why they do not cause problems in the test.

       
      • Maarten Brock

        Maarten Brock - 1 day ago

        Assume that one day we might be testing with other compilers as well.
        Or we might tell GCC not to use its extensions.

         
  • Christopher Bazley

    • status: open --> closed-fixed
    • assigned_to: Christopher Bazley
     
  • Christopher Bazley

    Fix committed as [r17090] with minor valdiag test modifications to avoid assuming support for case range expressions.

     

    Related

    Commit: [r17090]


Log in to post a comment.