SDCC 3.4.0 #8981 (Apr 5 2014) (Linux) SDCC 3.4.3 #9233 (May 17 2015) (Linux, snapshot)
When there are two nested if statements in a loop, that the second "if" statement does either "return" or "break", such as
while (1) { if (cond1) { if (cond2) break; else return NULL; } }
Although above structure compiles fine, but under some complex cases, it will crash the compiler.
#include <stdlib.h> int flag = 1; char *input(void) { char input_str[] = "abcd"; char *ptr = input_str; char *result = malloc(sizeof(char)); while (1) { if (*ptr == '\0') { if (flag == 0) { break; } else { return NULL; } } ptr++; } return result; }
Save above code as "crash.c" and compile it by
$ sdcc -mz80 --std-c99 -o crash.ihx crash.c
Although it is a common, valid and legal program, the compiler will print false-negative warnings and crash at once.
crash.c:22: warning 126: unreachable code crash.c:17: warning 126: unreachable code Caught signal 11: SIGSEGV
The crash.c also make sdcc crasehes with ds390, stm8 and hc08, on mingw & cygwin.
The crash happened in
void create_cfg_naddr(cfg_t &cfg, iCode start_ic, ebbIndex ebbi) @ SDCCnaddr.cc, the call to eBBWithEntryLabel() sometimes returned NULL, but there was no such check.
Even I added the check, sdcc failed during assembling .asm to .rel,
crash.asm:176: Error: undefined symbol encountered during assembly
crash.asm:179: Error: undefined symbol encountered during assembly
removing crash.rel
Anything in SDCCnaddr.cc should not affect the resulting code, unless the code uses user-defined named address spaces. There might still be a crash, ike in this case, but the failure during assembly can't be caused by something in SDCCnaddr.cc. I guess there are other palces in sdcc that assume eBBWithEntryLabel() not to return 0.
Philipp
Fixed by Erik in revision #9708.
Duplicate to bug #2453.