GH-955: Prohibit fallthrough in typecasting switch cases

This commit is contained in:
Jon Siwek 2020-08-26 12:55:10 -07:00
parent ff0aa6b050
commit 3368958ac1
4 changed files with 101 additions and 5 deletions

View file

@ -484,6 +484,23 @@ static StmtTag get_last_stmt_tag(const Stmt* stmt)
return get_last_stmt_tag(stmts->Stmts()[len - 1]);
}
class FallthroughFinder : public TraversalCallback {
TraversalCode PreStmt(const Stmt* stmt) override
{
if ( stmt->Tag() == STMT_SWITCH )
// Don't search within nested switch-statements.
return TC_ABORTSTMT;
if ( stmt->Tag() != STMT_FALLTHROUGH )
return TC_CONTINUE;
reporter->PushLocation(stmt->GetLocationInfo());
reporter->Error("invalid 'fallthrough' in type-casting 'case' block");
reporter->PopLocation();
return TC_CONTINUE;
}
};
Case::Case(ListExprPtr arg_expr_cases, IDPList* arg_type_cases,
StmtPtr arg_s)
: expr_cases(std::move(arg_expr_cases)), type_cases(arg_type_cases),
@ -493,14 +510,26 @@ Case::Case(ListExprPtr arg_expr_cases, IDPList* arg_type_cases,
if ( t != STMT_BREAK && t != STMT_FALLTHROUGH && t != STMT_RETURN )
Error("case block must end in break/fallthrough/return statement");
if ( type_cases && Body() )
for ( const auto& id : *type_cases )
if ( id->Name() )
{
FallthroughFinder ff;
Body()->Traverse(&ff);
break;
}
}
Case::~Case()
{
for ( const auto& id : *type_cases )
Unref(id);
if ( type_cases )
{
for ( const auto& id : *type_cases )
Unref(id);
delete type_cases;
delete type_cases;
}
}
void Case::Describe(ODesc* d) const

View file

@ -1638,13 +1638,13 @@ case_list:
case:
TOK_CASE expr_list ':' stmt_list
{ $$ = new zeek::detail::Case({zeek::AdoptRef{}, $2}, 0, {zeek::AdoptRef{}, $4}); }
{ $$ = new zeek::detail::Case({zeek::AdoptRef{}, $2}, nullptr, {zeek::AdoptRef{}, $4}); }
|
TOK_CASE case_type_list ':' stmt_list
{ $$ = new zeek::detail::Case(nullptr, $2, {zeek::AdoptRef{}, $4}); }
|
TOK_DEFAULT ':' stmt_list
{ $$ = new zeek::detail::Case(nullptr, 0, {zeek::AdoptRef{}, $3}); }
{ $$ = new zeek::detail::Case(nullptr, nullptr, {zeek::AdoptRef{}, $3}); }
;
case_type_list: