cxgo icon indicating copy to clipboard operation
cxgo copied to clipboard

Return Statement Reachability

Open redbow-kimee opened this issue 2 years ago • 1 comments

Possibly related to #10

Go enforces a syntactic reachability analysis for returns, while C never required that returns be reachable and C compilers which do reachability use more sophisticated algorighms. Some situations where the return statement is considered reachable (or at least not an issue) by C are not considered reachable by Go.

It appears that cxgo has implemented a bit of checking to implicitly add missing returns (see postproc.go:fixImplicitReturnStmts). However, I've found a test case which is improperly handled.

#include <stdio.h>

#define TRUE 1
#define FALSE 0

int implicit_return(char* str)
{
    while(TRUE)
    {
        while(TRUE)
        {
            if(*str == '\0') { return 0; }
            else if(*str != 'a') { break; }
            str++;
        }

        printf("%c is not a\n", *str);
        str++;
    }
}

int main()
{
    return implicit_return("aaabaababaaaaa");
}

I believe the last line of this function should have an (actually unreachable) return 0 added to fool Go into believing there is a reachable return 0.

DISTRIBUTION STATEMENT “A”. (Approved for public release. Distribution is unlimited.)

This material is based upon work supported by DARPA under Contract No. HR001122C0047. Any opinions, findings and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of DARPA. DISTRIBUTION STATEMENT “A”. (Approved for public release. Distribution is unlimited.)

redbow-kimee avatar Jan 23 '23 19:01 redbow-kimee

Thank you for providing a reproducer, I will check why reachability analysis fails for this case.

dennwc avatar Jan 24 '23 10:01 dennwc