diff --git a/src/Func.cc b/src/Func.cc index 7464f76ece..64e3ed2a88 100644 --- a/src/Func.cc +++ b/src/Func.cc @@ -332,8 +332,11 @@ ScriptFunc::ScriptFunc(std::string _name, FuncTypePtr ft, sort(bodies.begin(), bodies.end()); - current_body = bodies[0].stmts; - current_priority = bodies[0].priority; + if ( bodies.size() > 0 ) + { + current_body = bodies[0].stmts; + current_priority = bodies[0].priority; + } } ScriptFunc::~ScriptFunc() @@ -579,15 +582,22 @@ void ScriptFunc::ReplaceBody(const StmtPtr& old_body, StmtPtr new_body) { bool found_it = false; - for ( auto& body : bodies ) - if ( body.stmts.get() == old_body.get() ) + for ( auto body = bodies.begin(); body != bodies.end(); ++body ) + if ( body->stmts.get() == old_body.get() ) { - body.stmts = new_body; - current_priority = body.priority; + if ( new_body ) + { + body->stmts = new_body; + current_priority = body->priority; + } + else + bodies.erase(body); + found_it = true; + break; } - ASSERT(found_it); + // ASSERT(found_it); current_body = new_body; } diff --git a/src/Func.h b/src/Func.h index c1871270cb..db82bc8447 100644 --- a/src/Func.h +++ b/src/Func.h @@ -237,10 +237,16 @@ public: const std::vector& new_inits, size_t new_frame_size, int priority) override; - // Replace the given current instance of a function body with - // a new one. + /** + * Replaces the given current instance of a function body with + * a new one. If new_body is nil then the current instance is + * deleted with no replacement. + * + * @param old_body Body to replace. + * @param new_body New body to use; can be nil. + */ void ReplaceBody(const detail::StmtPtr& old_body, - detail::StmtPtr new_body); + detail::StmtPtr new_body); StmtPtr CurrentBody() const { return current_body; } int CurrentPriority() const { return current_priority; } @@ -316,7 +322,7 @@ private: StmtPtr current_body; // ... and its priority. - int current_priority; + int current_priority = 0; }; using built_in_func = BifReturnVal (*)(Frame* frame, const Args* args);