Menu ▾ ▴

#4085 Undiagnosed constraint violations on arithmetic operations

closed-fixed
None
Front-end
5
17 hours ago
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

Related

Wiki: SDCC 4.7.0 Release

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 2026-10-09
    • Philipp Klaus Krause

      Looks ok.

       
      • Christopher Bazley

        Christopher Bazley - 17 hours ago

        Thanks for the review. Fix committed as [r17213].

        Regression test results:

        Summary for 'ucz80'                  :      0 failures, 37454 tests, 6523 test cases,  7491282 bytes, 1601379452 ticks
        

        Valdiag test results:

        Summary for 'pdk15'                  :      0 failures,  1825 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'ds390'                  :      0 failures,  1828 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'f8'                     :      0 failures,  1825 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'hc08'                   :      0 failures,  1827 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'huc6280'                :      0 failures,  1826 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'mcs51-large'            :      0 failures,  1840 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'mcs51-stack-auto'       :      0 failures,  1841 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'mcs51'                  :      0 failures,  1842 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'mos6502'                :      0 failures,  1827 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'pdk13'                  :      0 failures,  1829 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'pdk14'                  :      0 failures,  1827 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'pdk15'                  :      0 failures,  1825 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'r2k'                    :      0 failures,  1827 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'r4k'                    :      0 failures,  1827 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 's08'                    :      0 failures,  1827 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'sm83'                   :      0 failures,  1827 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'stm8'                   :      0 failures,  1829 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'tlcs90'                 :      0 failures,  1827 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'z180'                   :      0 failures,  1827 tests, 1405 test cases,        0 bytes,          0 ticks
        Summary for 'z80'                    :      0 failures,  1828 tests, 1405 test cases,        0 bytes,          0 ticks
        
         

        Related

        Commit: [r17213]

  • Christopher Bazley

    Christopher Bazley - 17 hours ago
    • assigned_to: Christopher Bazley
     
  • Christopher Bazley

    Christopher Bazley - 17 hours ago
    • status: open --> closed-fixed
     

Log in to post a comment.