Menu

#2386 Compiler Prints False Warning and Crashes in some cases when "if (cond1) { if (cond2) break; else return ptr; }" is in a loop

closed-fixed
None
Front-end
5
2016-08-04
2015-05-18
Tom Li
No

Tested Versions

SDCC 3.4.0 #8981 (Apr  5 2014) (Linux)
SDCC 3.4.3 #9233 (May 17 2015) (Linux, snapshot)

Descriptions

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.

Reproducing

#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

Discussion

  • Ben Shi

    Ben Shi - 2015-05-18

    The crash.c also make sdcc crasehes with ds390, stm8 and hc08, on mingw & cygwin.

     
  • Ben Shi

    Ben Shi - 2015-05-18

    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

     
    • Philipp Klaus Krause

      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

       
  • Ben Shi

    Ben Shi - 2016-08-04
    • status: open --> closed-fixed
    • assigned_to: Erik Petrich
     
  • Ben Shi

    Ben Shi - 2016-08-04

    Fixed by Erik in revision #9708.

    Duplicate to bug #2453.

     

Log in to post a comment.