Menu ▾ ▴

#4085 Undiagnosed constraint violations on arithmetic operations

open
nobody
None
Front-end
5
2026-09-28
2026-09-27
No

1: Sample code that reproduces the problem.

/* N3886 6.5.7p2: the other operand of pointer addition is integer. */
int *pointer;
float floating;
int *test1(void) {
    return pointer + floating; /* ERROR */
}

/* N3886 6.5.7p3: pointer subtraction requires compatible pointed-to types. */
int *pointer;
char *other;
long test2(void) {
    return pointer - other; /* ERROR */
}

/* N3886 6.5.7p3: pointed-to object types must also be complete. */
struct incomplete;
struct incomplete *left, *right;
long test3(void) {
    return left - right; /* ERROR */
}

2: Exact command used to run SDCC on this sample code
./bin/sdcc -c bug.c

3: SDCC version tested

mcs51/z80/z180/r2k/r2ka/r3ka/r4k/r5k/r6k/sm83/tlcs90/ez80/z80n/r800/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502/mos65c02/huc6280/f8/f8l TD- 4.6.2 #0 (macOS ARM64)

4: Copy of the error message or incorrect output, or a clear description of the observed versus expected behavior.

Every arithmetic operation violates a constraint. According to the ISO C standard, all constraint violations must be diagnosed. Clang, GCC and TCC report the constraint violations but SDCC does not. See https://godbolt.org/z/M1azch6Kd

Discussion

  • Christopher Bazley

    Arithmetic on type void * also violates the ISO C constraint. Clang reports 'warning: arithmetic on pointers to void is a GNU extension [-Wgnu-pointer-arith]' for arithmetic on type void *. It's not clear to me whether SDCC deliberately permits this behaviour as an extension or not. Strictly speaking, it should still produce a diagnostic in some kind of strict mode (if it has one).

     
    • Philipp Klaus Krause

      Currently it is (intentionally) allowed, but the (for some cases) missing warnings are a bug. A patch is being discussed at [bugs:#4056]

       

      Related

      Bugs: #4056


      Last edit: Maarten Brock 2026-10-04
  • Christopher Bazley

    I'm attaching the relevant one of five candidate patches for bugs 4085...4089.

    For testing purposes, all five were applied in numeric order to the baseline of f68fca8a4368ece9fe93b21f2e0341282b104359 from the git mirror. The following commands then completed successfully:

    make -C device/lib clean
    make -C device/lib -j2
    make -j2
    make -C support/regression clean-results
    make -C support/regression -j2 test-ucz80
    make -C support/valdiag clean
    make -C support/valdiag -j2
    

    Test results:

    • ucz80 regression: 0 failures, 36,745 tests, 6,409 test cases;
    • valdiag: 0 failures on all 19 configured targets, with 377 test cases per target.
     

    Last edit: Christopher Bazley 6 days ago

Log in to post a comment.