diff --git a/NEWS b/NEWS index 0a35e5d69d..5b08e81d69 100644 --- a/NEWS +++ b/NEWS @@ -137,6 +137,9 @@ Deprecated Functionality to associated ``Val`` type are now deprecated, the deprecation warning message will advice what new method to use instead. +- Various methods of ``Tag`` classes are deprecated with the warning + message advising what new method to use instead. + Zeek 3.1.0 ========== diff --git a/src/Tag.cc b/src/Tag.cc index e0145afd6d..a964760fcb 100644 --- a/src/Tag.cc +++ b/src/Tag.cc @@ -4,37 +4,40 @@ #include "Val.h" #include "IntrusivePtr.h" -Tag::Tag(EnumType* etype, type_t arg_type, subtype_t arg_subtype) +Tag::Tag(const IntrusivePtr& etype, type_t arg_type, subtype_t arg_subtype) { assert(arg_type > 0); type = arg_type; subtype = arg_subtype; int64_t i = (int64_t)(type) | ((int64_t)subtype << 31); - Ref(etype); - val = etype->GetVal(i).release(); + val = etype->GetVal(i); } -Tag::Tag(EnumVal* arg_val) +Tag::Tag(EnumType* etype, type_t arg_type, subtype_t arg_subtype) + : Tag({NewRef{}, etype}, arg_type, arg_subtype) + { } + +Tag::Tag(IntrusivePtr arg_val) { assert(arg_val); - val = arg_val; - Ref(val); + val = std::move(arg_val); int64_t i = val->InternalInt(); type = i & 0xffffffff; subtype = (i >> 31) & 0xffffffff; } +Tag::Tag(EnumVal* arg_val) + : Tag({NewRef{}, arg_val}) + { } + Tag::Tag(const Tag& other) { type = other.type; subtype = other.subtype; val = other.val; - - if ( val ) - Ref(val); } Tag::Tag() @@ -44,11 +47,7 @@ Tag::Tag() val = nullptr; } -Tag::~Tag() - { - Unref(val); - val = nullptr; - } +Tag::~Tag() = default; Tag& Tag::operator=(const Tag& other) { @@ -56,11 +55,7 @@ Tag& Tag::operator=(const Tag& other) { type = other.type; subtype = other.subtype; - Unref(val); val = other.val; - - if ( val ) - Ref(val); } return *this; @@ -72,26 +67,28 @@ Tag& Tag::operator=(const Tag&& other) noexcept { type = other.type; subtype = other.subtype; - Unref(val); - val = other.val; - other.val = nullptr; + val = std::move(other.val); } return *this; } -EnumVal* Tag::AsEnumVal(EnumType* etype) const +const IntrusivePtr& Tag::AsVal(const IntrusivePtr& etype) const { if ( ! val ) { assert(type == 0 && subtype == 0); - Ref(etype); - val = etype->GetVal(0).release(); + val = etype->GetVal(0); } return val; } +EnumVal* Tag::AsEnumVal(EnumType* etype) const + { + return AsVal({NewRef{}, etype}).get(); + } + std::string Tag::AsString() const { return fmt("%" PRIu32 "/%" PRIu32, type, subtype); diff --git a/src/Tag.h b/src/Tag.h index f10c59cd4b..68e7f34b80 100644 --- a/src/Tag.h +++ b/src/Tag.h @@ -3,6 +3,7 @@ #pragma once #include "zeek-config.h" +#include "IntrusivePtr.h" #include @@ -114,6 +115,9 @@ protected: * * @param etype the script-layer enum type associated with the tag. */ + const IntrusivePtr& AsVal(const IntrusivePtr& etype) const; + + [[deprecated("Remove in v4.1. Use AsVal() instead.")]] EnumVal* AsEnumVal(EnumType* etype) const; /** @@ -127,6 +131,9 @@ protected: * @param subtype The sub type, which is left to an analyzer for * interpretation. By default it's set to zero. */ + Tag(const IntrusivePtr& etype, type_t type, subtype_t subtype = 0); + + [[deprecated("Remove in v4.1. Construct from IntrusivePtr& instead.")]] Tag(EnumType* etype, type_t type, subtype_t subtype = 0); /** @@ -134,10 +141,13 @@ protected: * * @param val An enum value of script type \c Analyzer::Tag. */ + explicit Tag(IntrusivePtr val); + + [[deprecated("Remove in v4.1. Construct from IntrusivePtr instead.")]] explicit Tag(EnumVal* val); private: type_t type; // Main type. subtype_t subtype; // Subtype. - mutable EnumVal* val; // Script-layer value. + mutable IntrusivePtr val; // Script-layer value. }; diff --git a/src/analyzer/Analyzer.cc b/src/analyzer/Analyzer.cc index 85398f19f1..5b8cf67a08 100644 --- a/src/analyzer/Analyzer.cc +++ b/src/analyzer/Analyzer.cc @@ -687,13 +687,8 @@ void Analyzer::ProtocolConfirmation(Tag arg_tag) if ( ! protocol_confirmation ) return; - EnumVal* tval = arg_tag ? arg_tag.AsEnumVal() : tag.AsEnumVal(); - - mgr.Enqueue(protocol_confirmation, - ConnVal(), - IntrusivePtr{NewRef{}, tval}, - val_mgr->Count(id) - ); + const auto& tval = arg_tag ? arg_tag.AsVal() : tag.AsVal(); + mgr.Enqueue(protocol_confirmation, ConnVal(), tval, val_mgr->Count(id)); } void Analyzer::ProtocolViolation(const char* reason, const char* data, int len) @@ -701,27 +696,21 @@ void Analyzer::ProtocolViolation(const char* reason, const char* data, int len) if ( ! protocol_violation ) return; - StringVal* r; + IntrusivePtr r; if ( data && len ) { const char *tmp = copy_string(reason); - r = new StringVal(fmt("%s [%s%s]", tmp, + r = make_intrusive(fmt("%s [%s%s]", tmp, fmt_bytes(data, min(40, len)), len > 40 ? "..." : "")); delete [] tmp; } else - r = new StringVal(reason); + r = make_intrusive(reason); - EnumVal* tval = tag.AsEnumVal(); - - mgr.Enqueue(protocol_violation, - ConnVal(), - IntrusivePtr{NewRef{}, tval}, - val_mgr->Count(id), - IntrusivePtr{AdoptRef{}, r} - ); + const auto& tval = tag.AsVal(); + mgr.Enqueue(protocol_violation, ConnVal(), tval, val_mgr->Count(id), std::move(r)); } void Analyzer::AddTimer(analyzer_timer_func timer, double t, diff --git a/src/analyzer/Manager.cc b/src/analyzer/Manager.cc index acea5cc813..45f6b7792e 100644 --- a/src/analyzer/Manager.cc +++ b/src/analyzer/Manager.cc @@ -572,8 +572,9 @@ void Manager::ScheduleAnalyzer(const IPAddr& orig, const IPAddr& resp, void Manager::ScheduleAnalyzer(const IPAddr& orig, const IPAddr& resp, PortVal* resp_p, Val* analyzer, double timeout) { - EnumVal* ev = analyzer->AsEnumVal(); - return ScheduleAnalyzer(orig, resp, resp_p->Port(), resp_p->PortType(), Tag(ev), timeout); + IntrusivePtr ev{NewRef{}, analyzer->AsEnumVal()}; + return ScheduleAnalyzer(orig, resp, resp_p->Port(), resp_p->PortType(), + Tag(std::move(ev)), timeout); } Manager::tag_set Manager::GetScheduled(const Connection* conn) @@ -624,8 +625,7 @@ bool Manager::ApplyScheduledAnalyzers(Connection* conn, bool init, TransportLaye if ( scheduled_analyzer_applied ) conn->EnqueueEvent(scheduled_analyzer_applied, nullptr, - conn->ConnVal(), - IntrusivePtr{NewRef{}, it->AsEnumVal()}); + conn->ConnVal(), it->AsVal()); DBG_ANALYZER_ARGS(conn, "activated %s analyzer as scheduled", analyzer_mgr->GetComponentName(*it).c_str()); diff --git a/src/analyzer/Tag.cc b/src/analyzer/Tag.cc index 128449175e..228668e15d 100644 --- a/src/analyzer/Tag.cc +++ b/src/analyzer/Tag.cc @@ -6,7 +6,7 @@ const analyzer::Tag analyzer::Tag::Error; analyzer::Tag::Tag(type_t type, subtype_t subtype) - : ::Tag(analyzer_mgr->GetTagEnumType(), type, subtype) + : ::Tag(analyzer_mgr->GetTagType(), type, subtype) { } @@ -16,7 +16,20 @@ analyzer::Tag& analyzer::Tag::operator=(const analyzer::Tag& other) return *this; } +const IntrusivePtr& analyzer::Tag::AsVal() const + { + return ::Tag::AsVal(analyzer_mgr->GetTagType()); + } + EnumVal* analyzer::Tag::AsEnumVal() const { - return ::Tag::AsEnumVal(analyzer_mgr->GetTagEnumType()); + return AsVal().get(); } + +analyzer::Tag::Tag(IntrusivePtr val) + : ::Tag(std::move(val)) + { } + +analyzer::Tag::Tag(EnumVal* val) + : ::Tag({NewRef{}, val}) + { } diff --git a/src/analyzer/Tag.h b/src/analyzer/Tag.h index 46545e4bd1..39cdc3ed45 100644 --- a/src/analyzer/Tag.h +++ b/src/analyzer/Tag.h @@ -83,6 +83,9 @@ public: * * @param etype the script-layer enum type associated with the tag. */ + const IntrusivePtr& AsVal() const; + + [[deprecated("Remove in v4.1. Use AsVal() instead.")]] EnumVal* AsEnumVal() const; static const Tag Error; @@ -109,7 +112,10 @@ protected: * * @param val An enum value of script type \c Analyzer::Tag. */ - explicit Tag(EnumVal* val) : ::Tag(val) {} + explicit Tag(IntrusivePtr val); + + [[deprecated("Remove in v4.1. Construct from IntrusivePtr instead")]] + explicit Tag(EnumVal* val); }; } diff --git a/src/analyzer/analyzer.bif b/src/analyzer/analyzer.bif index efb52e785f..849b75d693 100644 --- a/src/analyzer/analyzer.bif +++ b/src/analyzer/analyzer.bif @@ -41,11 +41,12 @@ function Analyzer::__schedule_analyzer%(orig: addr, resp: addr, resp_p: port, function __name%(atype: Analyzer::Tag%) : string %{ - return make_intrusive(analyzer_mgr->GetComponentName(atype)); + const auto& n = analyzer_mgr->GetComponentName(IntrusivePtr{NewRef{}, atype->AsEnumVal()}); + return make_intrusive(n); %} function __tag%(name: string%) : Analyzer::Tag %{ analyzer::Tag t = analyzer_mgr->GetComponentTag(name->CheckString()); - return IntrusivePtr{NewRef{}, t.AsEnumVal()}; + return t.AsVal(); %} diff --git a/src/analyzer/protocol/pia/PIA.cc b/src/analyzer/protocol/pia/PIA.cc index fe5797d6d1..8e0d7e9295 100644 --- a/src/analyzer/protocol/pia/PIA.cc +++ b/src/analyzer/protocol/pia/PIA.cc @@ -156,12 +156,11 @@ void PIA_UDP::ActivateAnalyzer(analyzer::Tag tag, const Rule* rule) if ( protocol_late_match ) { // Queue late match event - EnumVal *tval = tag ? tag.AsEnumVal() : GetAnalyzerTag().AsEnumVal(); + if ( ! tag ) + tag = GetAnalyzerTag(); - mgr.Enqueue(protocol_late_match, - ConnVal(), - IntrusivePtr{NewRef{}, tval} - ); + const auto& tval = tag.AsVal(); + mgr.Enqueue(protocol_late_match, ConnVal(), tval); } pkt_buffer.state = dpd_late_match_stop ? SKIPPING : MATCHING_ONLY; @@ -304,12 +303,11 @@ void PIA_TCP::ActivateAnalyzer(analyzer::Tag tag, const Rule* rule) if ( protocol_late_match ) { // Queue late match event - EnumVal *tval = tag ? tag.AsEnumVal() : GetAnalyzerTag().AsEnumVal(); + if ( ! tag ) + tag = GetAnalyzerTag(); - mgr.Enqueue(protocol_late_match, - ConnVal(), - IntrusivePtr{NewRef{}, tval} - ); + const auto& tval = tag.AsVal(); + mgr.Enqueue(protocol_late_match, ConnVal(), tval); } stream_buffer.state = dpd_late_match_stop ? SKIPPING : MATCHING_ONLY; diff --git a/src/file_analysis/AnalyzerSet.cc b/src/file_analysis/AnalyzerSet.cc index 1a7941a026..8c5c5eecc2 100644 --- a/src/file_analysis/AnalyzerSet.cc +++ b/src/file_analysis/AnalyzerSet.cc @@ -21,7 +21,7 @@ static void analyzer_del_func(void* v) AnalyzerSet::AnalyzerSet(File* arg_file) : file(arg_file) { auto t = make_intrusive(); - t->Append({NewRef{}, file_mgr->GetTagEnumType()}); + t->Append(file_mgr->GetTagType()); t->Append({NewRef{}, BifType::Record::Files::AnalyzerArgs}); analyzer_hash = new CompositeHash(std::move(t)); analyzer_map.SetDeleteFunc(analyzer_del_func); @@ -164,7 +164,7 @@ bool AnalyzerSet::RemoveMod::Perform(AnalyzerSet* set) HashKey* AnalyzerSet::GetKey(const file_analysis::Tag& t, RecordVal* args) const { ListVal* lv = new ListVal(TYPE_ANY); - lv->Append({NewRef{}, t.AsEnumVal()}); + lv->Append(t.AsVal()); lv->Append({NewRef{}, args}); HashKey* key = analyzer_hash->ComputeHash(lv, true); Unref(lv); diff --git a/src/file_analysis/Manager.cc b/src/file_analysis/Manager.cc index f3face2d79..f9e843ddf3 100644 --- a/src/file_analysis/Manager.cc +++ b/src/file_analysis/Manager.cc @@ -422,13 +422,9 @@ string Manager::GetFileID(const analyzer::Tag& tag, Connection* c, bool is_orig) DBG_LOG(DBG_FILE_ANALYSIS, "Raise get_file_handle() for protocol analyzer %s", analyzer_mgr->GetComponentName(tag).c_str()); - EnumVal* tagval = tag.AsEnumVal(); + const auto& tagval = tag.AsVal(); - mgr.Enqueue(get_file_handle, - IntrusivePtr{NewRef{}, tagval}, - c->ConnVal(), - val_mgr->Bool(is_orig) - ); + mgr.Enqueue(get_file_handle, tagval, c->ConnVal(), val_mgr->Bool(is_orig)); mgr.Drain(); // need file handle immediately so we don't have to buffer data return current_file_id; } diff --git a/src/file_analysis/Tag.cc b/src/file_analysis/Tag.cc index 30958ed605..1789600f26 100644 --- a/src/file_analysis/Tag.cc +++ b/src/file_analysis/Tag.cc @@ -8,7 +8,7 @@ using namespace file_analysis; const file_analysis::Tag file_analysis::Tag::Error; file_analysis::Tag::Tag(type_t type, subtype_t subtype) - : ::Tag(file_mgr->GetTagEnumType(), type, subtype) + : ::Tag(file_mgr->GetTagType(), type, subtype) { } @@ -18,7 +18,20 @@ file_analysis::Tag& file_analysis::Tag::operator=(const file_analysis::Tag& othe return *this; } +const IntrusivePtr& file_analysis::Tag::AsVal() const + { + return ::Tag::AsVal(file_mgr->GetTagType()); + } + EnumVal* file_analysis::Tag::AsEnumVal() const { - return ::Tag::AsEnumVal(file_mgr->GetTagEnumType()); + return AsVal().get(); } + +file_analysis::Tag::Tag(IntrusivePtr val) + : ::Tag(std::move(val)) + { } + +file_analysis::Tag::Tag(EnumVal* val) + : ::Tag({NewRef{}, val}) + { } diff --git a/src/file_analysis/Tag.h b/src/file_analysis/Tag.h index 6f5dae6b03..8c434e20e6 100644 --- a/src/file_analysis/Tag.h +++ b/src/file_analysis/Tag.h @@ -82,6 +82,9 @@ public: * * @param etype the script-layer enum type associated with the tag. */ + const IntrusivePtr& AsVal() const; + + [[deprecated("Remove in v4.1. Use AsVal() instead.")]] EnumVal* AsEnumVal() const; static const Tag Error; @@ -107,7 +110,10 @@ protected: * * @param val An enum value of script type \c Files::Tag. */ - explicit Tag(EnumVal* val) : ::Tag(val) {} + explicit Tag(IntrusivePtr val); + + [[deprecated("Remove in v4.1. Construct from IntrusivePtr instead.")]] + explicit Tag(EnumVal* val); }; } diff --git a/src/file_analysis/file_analysis.bif b/src/file_analysis/file_analysis.bif index 24222a7a45..91bc6929aa 100644 --- a/src/file_analysis/file_analysis.bif +++ b/src/file_analysis/file_analysis.bif @@ -68,7 +68,8 @@ function Files::__stop%(file_id: string%): bool ## :zeek:see:`Files::analyzer_name`. function Files::__analyzer_name%(tag: Files::Tag%) : string %{ - return make_intrusive(file_mgr->GetComponentName(tag)); + const auto& n = file_mgr->GetComponentName(IntrusivePtr{NewRef{}, tag->AsEnumVal()}); + return make_intrusive(n); %} ## :zeek:see:`Files::file_exists`. diff --git a/src/input/Tag.cc b/src/input/Tag.cc index a2c0708583..cbdcd59aae 100644 --- a/src/input/Tag.cc +++ b/src/input/Tag.cc @@ -6,7 +6,7 @@ const input::Tag input::Tag::Error; input::Tag::Tag(type_t type, subtype_t subtype) - : ::Tag(input_mgr->GetTagEnumType(), type, subtype) + : ::Tag(input_mgr->GetTagType(), type, subtype) { } @@ -16,7 +16,20 @@ input::Tag& input::Tag::operator=(const input::Tag& other) return *this; } +const IntrusivePtr& input::Tag::AsVal() const + { + return ::Tag::AsVal(input_mgr->GetTagType()); + } + EnumVal* input::Tag::AsEnumVal() const { - return ::Tag::AsEnumVal(input_mgr->GetTagEnumType()); + return AsVal().get(); } + +input::Tag::Tag(IntrusivePtr val) + : ::Tag(std::move(val)) + { } + +input::Tag::Tag(EnumVal* val) + : ::Tag({NewRef{}, val}) + { } diff --git a/src/input/Tag.h b/src/input/Tag.h index 86f85ac36b..20a163da23 100644 --- a/src/input/Tag.h +++ b/src/input/Tag.h @@ -83,6 +83,9 @@ public: * * @param etype the script-layer enum type associated with the tag. */ + const IntrusivePtr& AsVal() const; + + [[deprecated("Remove in v4.1. Use AsVal() instead.")]] EnumVal* AsEnumVal() const; static const Tag Error; @@ -108,7 +111,10 @@ protected: * * @param val An enum value of script type \c Input::Reader. */ - explicit Tag(EnumVal* val) : ::Tag(val) {} + explicit Tag(IntrusivePtr val); + + [[deprecated("Remove in v4.1. Construct from IntrusivePtr isntead.")]] + explicit Tag(EnumVal* val); }; } diff --git a/src/logging/Tag.cc b/src/logging/Tag.cc index 2ab2844e25..69d9e0ad67 100644 --- a/src/logging/Tag.cc +++ b/src/logging/Tag.cc @@ -6,7 +6,7 @@ const logging::Tag logging::Tag::Error; logging::Tag::Tag(type_t type, subtype_t subtype) - : ::Tag(log_mgr->GetTagEnumType(), type, subtype) + : ::Tag(log_mgr->GetTagType(), type, subtype) { } @@ -22,7 +22,20 @@ logging::Tag& logging::Tag::operator=(const logging::Tag&& other) noexcept return *this; } +const IntrusivePtr& logging::Tag::AsVal() const + { + return ::Tag::AsVal(log_mgr->GetTagType()); + } + EnumVal* logging::Tag::AsEnumVal() const { - return ::Tag::AsEnumVal(log_mgr->GetTagEnumType()); + return AsVal().get(); } + +logging::Tag::Tag(IntrusivePtr val) + : ::Tag(std::move(val)) + { } + +logging::Tag::Tag(EnumVal* val) + : ::Tag({NewRef{}, val}) + { } diff --git a/src/logging/Tag.h b/src/logging/Tag.h index 29c7fd6cd4..78aee1a3d0 100644 --- a/src/logging/Tag.h +++ b/src/logging/Tag.h @@ -88,6 +88,9 @@ public: * * @param etype the script-layer enum type associated with the tag. */ + const IntrusivePtr& AsVal() const; + + [[deprecated("Remove in v4.1. Use AsVal() instead.")]] EnumVal* AsEnumVal() const; static const Tag Error; @@ -113,7 +116,10 @@ protected: * * @param val An enum value of script type \c Log::Writer. */ - explicit Tag(EnumVal* val) : ::Tag(val) {} + explicit Tag(IntrusivePtr val); + + [[deprecated("Remove in v4.1. Construct from IntrusivePtr instead.")]] + explicit Tag(EnumVal* val); }; } diff --git a/src/plugin/ComponentManager.h b/src/plugin/ComponentManager.h index 2448597004..3c66fe9dd7 100644 --- a/src/plugin/ComponentManager.h +++ b/src/plugin/ComponentManager.h @@ -52,6 +52,9 @@ public: /** * @return The enum type associated with the script-layer "Tag". */ + const IntrusivePtr& GetTagType() const; + + [[deprecated("Remove in v4.1. Use GetTagType() instead.")]] EnumType* GetTagEnumType() const; /** @@ -68,6 +71,9 @@ public: * @param val A component's enum value. * @return The canonical component name. */ + const std::string& GetComponentName(IntrusivePtr val) const; + + [[deprecated("Remove in v4.1. Use IntrusivePtr argument instead.")]] const std::string& GetComponentName(Val* val) const; /** @@ -156,6 +162,12 @@ std::list ComponentManager::GetComponents() const return rval; } +template +const IntrusivePtr& ComponentManager::GetTagType() const + { + return tag_enum_type; + } + template EnumType* ComponentManager::GetTagEnumType() const { @@ -180,10 +192,16 @@ const std::string& ComponentManager::GetComponentName(T tag) const return error; } +template +const std::string& ComponentManager::GetComponentName(IntrusivePtr val) const + { + return GetComponentName(T(std::move(val))); + } + template const std::string& ComponentManager::GetComponentName(Val* val) const { - return GetComponentName(T(val->AsEnumVal())); + return GetComponentName(T({NewRef{}, val->AsEnumVal()})); } template @@ -239,12 +257,12 @@ void ComponentManager::RegisterComponent(C* component, components_by_name.insert(std::make_pair(cname, component)); components_by_tag.insert(std::make_pair(component->Tag(), component)); components_by_val.insert(std::make_pair( - component->Tag().AsEnumVal()->InternalInt(), component)); + component->Tag().AsVal()->InternalInt(), component)); // Install an identfier for enum value std::string id = fmt("%s%s", prefix.c_str(), cname.c_str()); tag_enum_type->AddName(module, id.c_str(), - component->Tag().AsEnumVal()->InternalInt(), true, + component->Tag().AsVal()->InternalInt(), true, nullptr); } diff --git a/src/zeekygen/Target.cc b/src/zeekygen/Target.cc index 9aae9b82fe..34e6f3519a 100644 --- a/src/zeekygen/Target.cc +++ b/src/zeekygen/Target.cc @@ -35,7 +35,7 @@ static void write_plugin_section_heading(FILE* f, const plugin::Plugin* p) static void write_analyzer_component(FILE* f, const analyzer::Component* c) { - EnumType* atag = analyzer_mgr->GetTagEnumType(); + const auto& atag = analyzer_mgr->GetTagType(); string tag = fmt("ANALYZER_%s", c->CanonicalName().c_str()); if ( atag->Lookup("Analyzer", tag.c_str()) < 0 ) @@ -46,7 +46,7 @@ static void write_analyzer_component(FILE* f, const analyzer::Component* c) static void write_analyzer_component(FILE* f, const file_analysis::Component* c) { - EnumType* atag = file_mgr->GetTagEnumType(); + const auto& atag = file_mgr->GetTagType(); string tag = fmt("ANALYZER_%s", c->CanonicalName().c_str()); if ( atag->Lookup("Files", tag.c_str()) < 0 )