Michał - 2026-11-11

Analysis

Affected code

  • Crash site (STM8): src/stm8/gen.c
    • genReturn() takes the scalar path because IS_STRUCT (operandType (IC_LEFT (ic))) is false for the pointer-typed operand
    • aopRet() returns NULL for struct-returning functions
    • genMove(): wassert (result) fires (line 2773 in 4.6.0 #16555), then genMove_o() dereferences NULL → SIGSEGV
  • Crash site (Z80): src/z80/gen.c, line 6373 in 4.6.0 #16555 (not analysed, presumably the same pattern)
  • Root cause: src/SDCCopt.c, replaceRegEqv(), the "Convert struct / union to pointer" loop

iCode dumps (--dump-i-code)

In repro.dumpraw0 member access yields a struct-typed iTemp that holds an address, used by RETURN:

:::text
iTemp1 {struct __00000000 fixed} = iTemp0 {struct __00000001 near* fixed} + 0x0
ret iTemp1 {struct __00000000 fixed}

In repro.dumpraw1 iTemp1 has become a pointer, including in RETURN:

:::text
iTemp1 {struct __00000000 near* fixed} = (struct __00000000 near* fixed)iTemp0
ret iTemp1 {struct __00000000 near* fixed}

For return g; (named struct variable) RETURN gets _g {struct __00000000 fixed} and compiles fine.

Where the type changes

Between DUMP_RAW0 and DUMP_RAW1 only replaceRegEqv() and createLoopRegions() run. In replaceRegEqv():

:::c
// Convert struct / union to pointer.
if(ic->op != ADDRESS_OF && ic->op != RETURN && ic->op != CALL && ic->op != PCALL)
  {
    ...
    if (ic->result && IS_AGGREGATE (operandType (ic->result)))
      setOperandType (ic->result, aggrToPtr (operandType (ic->result), false));
  }

RETURN iCodes are skipped, but the iCode that defines iTemp1 (iTemp1 = iTemp0 + 0x0) is not, so iTemp1 is retyped to a pointer. RETURN uses the same iTemp, so its operand becomes struct near* as well. Named variables are unaffected because they have no defining iCode.

genReturn() then sees a 2-byte non-struct operand, takes case 2, and calls:

:::c
genMove (aopRet (currFunc->type), left->aop, true, true, true);   /* aopRet() == NULL */

→ wassert (result) in genMove() → NULL dereference → SIGSEGV.
Fixing genReturn() alone by forcing the bigreturn path would copy the 2-byte address instead of the struct contents.

Relation to [#4131]

This looks like the same conversion described in [#4131] ("later conversion … changed the shared temporary's type into a pointer"), there triggered by the conditional operator. A fix in this loop, or handling a pointer-typed operand in RETURN for struct-returning functions by copying the pointed-to object, would likely cover both tickets.

Source inspected: a revision slightly different from the 4.6.0 binary; wassert (result) in genMove() was at line 2759 there, the conversion loop at lines ~1811–1819 of SDCCopt.c.

 

Related

Bugs: #4131