cxgo icon indicating copy to clipboard operation
cxgo copied to clipboard

panic: runtime error: index out of range [-1]

Open Yeaseen opened this issue 4 months ago • 0 comments

Source C code:

#include <stdio.h>
int global_int = 1;
int main() {
    int value = 1;
    switch (value) {
        global_int++;
        case 1:
            printf("Case 1\n");
        case 2:
            printf("Case 2\n");
            break;
        default:
            printf("Default case\n");
            break;
    }
    return 0;
}

cxgo fails to handle the unreachable part of this c code global_int++; which should just be omitted in the go code or placed as it is.

panic: runtime error: index out of range [-1]
goroutine 1 [running]:
github.com/gotranspile/cxgo.(*CSwitchStmt).addStmts(0xc000352210, {0xc000118e70?, 0x20?, 0x7100000000000000?})
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/c_stmt.go:361 +0x2f0
github.com/gotranspile/cxgo.(*translator).NewCSwitchStmt(...)
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/c_stmt.go:331
github.com/gotranspile/cxgo.(*translator).convertSelStmt(0xc0001d3e40, 0xc0001f44b0)
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/convert.go:1233 +0x15f
github.com/gotranspile/cxgo.(*translator).convertStmt(0x826ec0?, 0xc00030c450?)
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/convert.go:1361 +0x8f
github.com/gotranspile/cxgo.(*translator).convertCompStmt(0xc0001d3e40, 0x0?)
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/convert.go:617 +0x9c
github.com/gotranspile/cxgo.(*translator).convertCompBlockStmt(0xc0001d3e40, 0xc00013512c?)
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/convert.go:627 +0x25
github.com/gotranspile/cxgo.(*translator).convertFuncDef(0xc0001d3e40, 0xc0000c3c70)
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/convert.go:128 +0x27a
github.com/gotranspile/cxgo.(*translator).translateC(0xc0001d3e40, {0x7fffffffdd01?, 0xc00013cea0?}, 0xc0001dd800)
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/translate.go:346 +0x166
github.com/gotranspile/cxgo.(*translator).translate(0xc0001d3e40, {0x7fffffffdd01, 0x8}, 0x4?)
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/translate.go:295 +0x4b
github.com/gotranspile/cxgo.TranslateAST({0x7fffffffdd01, _}, _, _, {{0x0, 0x0}, {0x87b64d, 0x4}, {0xc000134c50, 0x9}, ...})
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/translate.go:191 +0x98
github.com/gotranspile/cxgo.Translate({0x0, _}, {_, _}, {_, _}, _, {{0x0, 0x0}, {0x87b64d, ...}, ...})
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/translate.go:105 +0x233
main.init.0.func1(0xc000136780?, {0xc0001188d0?, 0x1?, 0x1?})
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/cmd/cxgo/file.go:40 +0x19a
github.com/spf13/cobra.(*Command).execute(0xc000136780, {0xc0001188a0, 0x1, 0x1})
        /home/yeaseen/go/pkg/mod/github.com/spf13/[email protected]/command.go:826 +0x67c
github.com/spf13/cobra.(*Command).ExecuteC(0xb7e5e0)
        /home/yeaseen/go/pkg/mod/github.com/spf13/[email protected]/command.go:914 +0x2ee
github.com/spf13/cobra.(*Command).Execute(...)
        /home/yeaseen/go/pkg/mod/github.com/spf13/[email protected]/command.go:864
main.main()
        /home/yeaseen/go/pkg/mod/github.com/gotranspile/[email protected]/cmd/cxgo/main.go:63 +0x25

However, the following C code is handled perfectly by cxgo, even though it has the same unreachable statement at the end of the switch case.

#include <stdio.h>
int global_int = 1;
int main() {
    int value = 1;
    switch (value) {
        ;
        case 1:
            printf("Case 1\n");
        case 2:
            printf("Case 2\n");
            break;
        default:
            printf("Default case\n");
            break;
        global_int++;
    }
    return 0;
}

Interestingly. cxgo can handle the no-op statement before case 1 statement.

Yeaseen avatar Oct 17 '24 02:10 Yeaseen