Skip to content

Commit

Permalink
cxgo: Fix panic in switch with unreachable stmt. Fixes #80.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Nov 9, 2024
1 parent 1bd2a8e commit ce10c14
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
7 changes: 6 additions & 1 deletion c_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,12 @@ func (s *CSwitchStmt) addStmts(stmts []CStmt) {
s.addStmts(sub)
}
} else {
last := s.Cases[len(s.Cases)-1]
ci := len(s.Cases) - 1
if ci < 0 {
// statement before any cases - ignore
continue
}
last := s.Cases[ci]
last.Stmts = append(last.Stmts, st)
}
}
Expand Down
56 changes: 56 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,62 @@ func foo(a int32) {
foo(6)
}
}
`,
},
{
name: "switch unreachable",
src: `
void foo(int a) {
switch (a) {
a++;
case 1:
foo(1);
break;
a++;
case 2:
foo(2);
default:
foo(0);
case 3:
foo(3);
break;
case 4:
case 5:
foo(5);
return;
a++;
case 6:
foo(6);
}
}
`,
exp: `
func foo(a int32) {
switch a {
case 1:
foo(1)
break
a++
fallthrough
case 2:
foo(2)
fallthrough
default:
foo(0)
fallthrough
case 3:
foo(3)
case 4:
fallthrough
case 5:
foo(5)
return
a++
fallthrough
case 6:
foo(6)
}
}
`,
},
{
Expand Down

0 comments on commit ce10c14

Please sign in to comment.