From 44f558df7b5a85bae40945de653bcb2448e0a7f4 Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Wed, 17 Mar 2021 13:40:31 -0700 Subject: [PATCH] Clean up iterator usage in Analyzer --- src/analyzer/Analyzer.cc | 45 +++++++++++++++++---------------- src/analyzer/Analyzer.h | 19 ++++++-------- src/analyzer/protocol/tcp/TCP.h | 1 - 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/analyzer/Analyzer.cc b/src/analyzer/Analyzer.cc index d635f43bfc..efdc8a0670 100644 --- a/src/analyzer/Analyzer.cc +++ b/src/analyzer/Analyzer.cc @@ -301,17 +301,17 @@ void Analyzer::ForwardPacket(int len, const u_char* data, bool is_orig, AppendNewChildren(); // Pass to all children. - analyzer_list::iterator next; - for ( analyzer_list::iterator i = children.begin(); - i != children.end(); i = next ) + for ( auto i = children.begin(); i != children.end(); ) { Analyzer* current = *i; - next = ++i; if ( ! (current->finished || current->removing ) ) + { current->NextPacket(len, data, is_orig, seq, ip, caplen); + ++i; + } else - DeleteChild(--i); + i = DeleteChild(i); } AppendNewChildren(); @@ -324,17 +324,17 @@ void Analyzer::ForwardStream(int len, const u_char* data, bool is_orig) AppendNewChildren(); - analyzer_list::iterator next; - for ( analyzer_list::iterator i = children.begin(); - i != children.end(); i = next ) + for ( auto i = children.begin(); i != children.end(); ) { Analyzer* current = *i; - next = ++i; if ( ! (current->finished || current->removing ) ) + { current->NextStream(len, data, is_orig); + ++i; + } else - DeleteChild(--i); + i = DeleteChild(i); } AppendNewChildren(); @@ -347,17 +347,17 @@ void Analyzer::ForwardUndelivered(uint64_t seq, int len, bool is_orig) AppendNewChildren(); - analyzer_list::iterator next; - for ( analyzer_list::iterator i = children.begin(); - i != children.end(); i = next ) + for ( auto i = children.begin(); i != children.end(); ) { Analyzer* current = *i; - next = ++i; if ( ! (current->finished || current->removing ) ) + { current->NextUndelivered(seq, len, is_orig); + ++i; + } else - DeleteChild(--i); + i = DeleteChild(i); } AppendNewChildren(); @@ -367,17 +367,17 @@ void Analyzer::ForwardEndOfData(bool orig) { AppendNewChildren(); - analyzer_list::iterator next; - for ( analyzer_list::iterator i = children.begin(); - i != children.end(); i = next ) + for ( auto i = children.begin(); i != children.end(); ) { Analyzer* current = *i; - next = ++i; if ( ! (current->finished || current->removing ) ) + { current->NextEndOfData(orig); + ++i; + } else - DeleteChild(--i); + i = DeleteChild(i); } AppendNewChildren(); @@ -541,7 +541,7 @@ Analyzer* Analyzer::FindChild(const char* name) return tag ? FindChild(tag) : nullptr; } -void Analyzer::DeleteChild(analyzer_list::iterator i) +analyzer_list::iterator Analyzer::DeleteChild(analyzer_list::iterator i) { Analyzer* child = *i; @@ -557,8 +557,9 @@ void Analyzer::DeleteChild(analyzer_list::iterator i) DBG_LOG(DBG_ANALYZER, "%s deleted child %s 3", fmt_analyzer(this).c_str(), fmt_analyzer(child).c_str()); - children.erase(i); + auto next = children.erase(i); delete child; + return next; } void Analyzer::AddSupportAnalyzer(SupportAnalyzer* analyzer) diff --git a/src/analyzer/Analyzer.h b/src/analyzer/Analyzer.h index ae41f9e354..304af30595 100644 --- a/src/analyzer/Analyzer.h +++ b/src/analyzer/Analyzer.h @@ -38,7 +38,7 @@ class AnalyzerTimer; class SupportAnalyzer; class OutputHandler; -using analyzer_list = std::list; +using analyzer_list = std::vector; typedef uint32_t ID; typedef void (Analyzer::*analyzer_timer_func)(double t); @@ -685,8 +685,9 @@ protected: private: // Internal method to eventually delete a child analyzer that's - // already Done(). - void DeleteChild(analyzer_list::iterator i); + // already Done(). Returns an iterator to the next element, following + // std::list::erase()'s semantics. + analyzer_list::iterator DeleteChild(analyzer_list::iterator i); // Helper for the ctors. void CtorInit(const Tag& tag, Connection* conn); @@ -727,31 +728,27 @@ private: * Internal convenience macro to iterate over the list of child analyzers. */ #define LOOP_OVER_CHILDREN(var) \ - for ( zeek::analyzer::analyzer_list::iterator var = children.begin(); \ - var != children.end(); var++ ) + for ( auto var = children.begin(); var != children.end(); ++var ) /** * Internal convenience macro to iterate over the constant list of child * analyzers. */ #define LOOP_OVER_CONST_CHILDREN(var) \ - for ( zeek::analyzer::analyzer_list::const_iterator var = children.begin(); \ - var != children.end(); var++ ) + for ( auto var = children.cbegin(); var != children.cend(); ++var ) /** * Convenience macro to iterate over a given list of child analyzers. */ #define LOOP_OVER_GIVEN_CHILDREN(var, the_kids) \ - for ( zeek::analyzer::analyzer_list::iterator var = the_kids.begin(); \ - var != the_kids.end(); var++ ) + for ( auto var = the_kids.begin(); var != the_kids.end(); ++var ) /** * Convenience macro to iterate over a given constant list of child * analyzers. */ #define LOOP_OVER_GIVEN_CONST_CHILDREN(var, the_kids) \ - for ( zeek::analyzer::analyzer_list::const_iterator var = the_kids.begin(); \ - var != the_kids.end(); var++ ) + for ( auto var = the_kids.cbegin(); var != the_kids.cend(); ++var ) /** * Support analyzer preprocess input before it reaches an analyzer's main diff --git a/src/analyzer/protocol/tcp/TCP.h b/src/analyzer/protocol/tcp/TCP.h index e84db71993..13c87df7fc 100644 --- a/src/analyzer/protocol/tcp/TCP.h +++ b/src/analyzer/protocol/tcp/TCP.h @@ -174,7 +174,6 @@ private: TCP_Endpoint* orig; TCP_Endpoint* resp; - using analyzer_list = std::list; analyzer_list packet_children; unsigned int first_packet_seen: 2;