Menu ▾ ▴

#4249 Dereferenced post-increment advances a pointer to an array by one byte

open
nobody
None
other
5
2026-11-09
2026-11-08
No

1: Sample code that reproduces the problem

extern void sink(char *);
static char array[64];

int test(void)
{
    char (*p)[64] = &array;
    sink(*p++);
    return p == &array + 1;
}

2: Exact command used to reproduce the problem

sdcc -mz80 --std-c99 --nostdinc -S bug.c -o bug.asm

3: SDCC version tested

SDCC 4.6.4 #17140 (Linux), tested on 8 November 2026.

4: Observed and expected behaviour

test should return 1 if sink returns normally without undefined behaviour. The generated code instead compares array + 1 with array + 64, so it returns 0. The complete generated body of test is:

_test::
        ld      hl, #_array
        call    _sink
        ld      hl, #_array + 1
        ld      de, #(_array + 64)
        xor     a, a
        sbc     hl, de
        jr      nz, 00104$
        inc     a
00104$:
        ld      e, a
        ld      d, #0x00
        ret

p points to an array of 64 characters. Incrementing it must advance it past that whole array, rather than past its first character. The conversion of the array expression *p++ to a pointer for the call must not change the type or increment of p.

Relevant clauses of C99, as reproduced in N1256:

  • 6.5.2.4 paragraph 2: postfix increment yields the previous value and increments the operand, subject to the pointer-arithmetic rules.
  • 6.5.6 paragraphs 7–8: a standalone object is treated as an array of length one for pointer arithmetic; adding one produces a pointer past that object. Here that object is the entire 64-character array.
  • 6.3.2.1 paragraph 3: the array expression supplied as the argument is converted to a pointer to its first element.
  • 6.5.2.2 paragraph 10: argument evaluation is followed by a sequence point before the function is called.

5: Additional checks

Compiler Explorer comparison: SDCC trunk, SDCC 4.6.0, GCC trunk and Clang trunk.

Both SDCC versions produce the incorrect one-byte increment with -mz80 --std-c99. GCC and Clang at -std=c99 -O2 compile test to return 1 after calling sink.

This was found while testing single evaluation of an array argument to the strb_t macros. The reduced example requires neither macros nor library headers. SDCC 4.6.0 also exhibits the problem, so I have not identified it as a recent regression.

Related

Wiki: SDCC 4.7.0 Release

Discussion

  • Christopher Bazley

    Attaching a candidate bugfix created using Codex that with need to be reviewed by another developer as per the rules.

     
  • Christopher Bazley

    The reported program does fail, but post-increment is not the faulty operation.
    Raw intermediate code increments p by 64. The expression &array + 1 instead
    advances by one byte. The same error occurs in a function that simply returns
    &array + 1, without an increment or a call. Checking (char *)p against
    array + sizeof array confirms that the dereferenced post-increment works.

    Array operand generation converts the array to a pointer to its first element.
    Unary address-of then accepts that pointer without restoring the pointer-to-array
    type. Use the decorated AST type for the result of unary & on an array.

    Tests check array address addition, post-increment independently, the original
    comparison, addresses of rows, &*p, subtraction and pointer difference.

     

Log in to post a comment.