diff --git a/src/ScriptValidation.cc b/src/ScriptValidation.cc index 1f6832c775..d13bc63c48 100644 --- a/src/ScriptValidation.cc +++ b/src/ScriptValidation.cc @@ -22,11 +22,11 @@ public: stmt_depths[stmt->Tag()] += 1; if ( stmt->Tag() == STMT_BREAK && ! BreakStmtIsValid() ) - Error(stmt, "break statement used outside of for, while or " - "switch statement and not within a hook."); + Report(stmt, "break statement used outside of for, while or " + "switch statement and not within a hook."); if ( stmt->Tag() == STMT_NEXT && ! NextStmtIsValid() ) - Error(stmt, "next statement used outside of for or while statement."); + Report(stmt, "next statement used outside of for or while statement."); return TC_CONTINUE; } @@ -63,6 +63,8 @@ public: return TC_CONTINUE; } + void SetHookDepth(int hd) { hook_depth = hd; } + bool IsValid() const { return valid_script; } private: @@ -84,11 +86,7 @@ private: void Report(const Stmt* stmt, const char* msg) { if ( report ) - { - zeek::reporter->PushLocation(stmt->GetLocationInfo()); - zeek::reporter->Warning("%s", msg); - zeek::reporter->PopLocation(); - } + Error(stmt, msg); valid_script = false; } @@ -105,10 +103,15 @@ void script_validation() traverse_all(&bn_cb); } -bool script_is_valid(const Stmt* stmt) +bool script_is_valid(const Stmt* stmt, bool is_in_hook) { BreakNextScriptValidation bn_cb(false); + + if ( is_in_hook ) + bn_cb.SetHookDepth(1); + stmt->Traverse(&bn_cb); + return bn_cb.IsValid(); } diff --git a/src/ScriptValidation.h b/src/ScriptValidation.h index 05fc3a3ded..db0d86b820 100644 --- a/src/ScriptValidation.h +++ b/src/ScriptValidation.h @@ -13,8 +13,12 @@ class Stmt; void script_validation(); /** - * Returns true if the given script statement (body) is valid. + * Returns true if the given script statement (body) is valid. The + * second argument indicates whether the statement is the body of a hook. + * + * Unlike script_validation(), does not report any errors, just returns + * whether they are present. */ -bool script_is_valid(const Stmt* s); +bool script_is_valid(const Stmt* s, bool is_in_hook); } diff --git a/src/script_opt/ZAM/Support.cc b/src/script_opt/ZAM/Support.cc index 83d8828351..f813f86758 100644 --- a/src/script_opt/ZAM/Support.cc +++ b/src/script_opt/ZAM/Support.cc @@ -35,7 +35,8 @@ bool is_ZAM_compilable(const ProfileFunc* pf, const char** reason) } auto b = pf->ProfiledBody(); - if ( b && ! script_is_valid(b) ) + auto is_hook = pf->ProfiledFunc()->Flavor() == FUNC_FLAVOR_HOOK; + if ( b && ! script_is_valid(b, is_hook) ) { if ( reason ) *reason = "invalid script body";