Menu ▾ ▴

#4104 Function typedef returning char ** is mishandled with __reentrant

closed-fixed
None
Front-end
5
2026-10-07
2026-10-02
No

1: Sample code that reproduces the problem.

typedef char **PointerReturnFunc(void) __reentrant;
PointerReturnFunc *return_func;
void test(void) { return_func = 0; }

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

./bin/sdcc -mmcs51 -S bug.c

3: SDCC version tested

4.5.24 #16456 (Mac OS X ppc) built from e2796713c3ff11ff9bac33c0f99a90711a2d6684

Affected in testing on mcs51, mcs51-large, mcs51-stack-auto, ds390, hc08, s08, mos6502, huc6280, pdk13, pdk14, and pdk15.

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

bug.c:3: syntax error: token -> '=' ; column 31

PointerReturnFunc should be a function type returning a pointer to a pointer to char. return_func should therefore be a function-pointer object, to which a null pointer constant can be assigned. SDCC instead reports a syntax error at that assignment.

The following equivalent declaration using an intermediate typedef compiles without a diagnostic on each affected target:

typedef char *CharPointer;
typedef CharPointer *PointerReturnFunc(void) __reentrant;
PointerReturnFunc *return_func;
void test(void) { return_func = 0; }

Removing __reentrant from the direct declaration also makes the original example compile on mcs51 and z80. The failure therefore involves the combination of the direct return-type declaration and SDCC's calling-convention attribute, rather than the plain ISO C function typedef alone.

GCC 16.1.0, Clang 23.1.0 and TCC 0.9.27 all accept the program if compiled with -D__reentrant=. Obviously, I cannot test SDCC's calling-convention attribute with those compilers.
https://godbolt.org/z/dfv4c3f89

Related

Bugs: #4004

Discussion

  • Christopher Bazley

    Please find a candidate patch attached. It's a one line change in the compiler itself, but comes with a lot of tests.

     
  • Philipp Klaus Krause

    Thanks. Fixed in [r16975] via your patch.

     
  • Philipp Klaus Krause

    • status: open --> closed-fixed
    • assigned_to: Philipp Klaus Krause
     

Log in to post a comment.