diff --git a/src/Stmt.cc b/src/Stmt.cc index 18c66b00c8..d0b9bac64e 100644 --- a/src/Stmt.cc +++ b/src/Stmt.cc @@ -200,8 +200,9 @@ static IntrusivePtr lookup_enum_val(const char* module_name, const char static void print_log(const std::vector>& vals) { - auto plval = lookup_enum_val("Log", "PRINTLOG"); - auto record = make_intrusive(zeek::id::find_type("Log::PrintLogInfo")); + static auto plval = lookup_enum_val("Log", "PRINTLOG"); + static auto lpli = zeek::id::find_type("Log::PrintLogInfo"); + auto record = make_intrusive(lpli); auto vec = make_intrusive(zeek::id::string_vec); for ( const auto& val : vals ) diff --git a/src/Type.cc b/src/Type.cc index cb4f604cda..d5d17f7a3f 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -1231,20 +1231,17 @@ EnumType::enum_name_list EnumType::Names() const return n; } -IntrusivePtr EnumType::GetVal(bro_int_t i) +const IntrusivePtr& EnumType::GetVal(bro_int_t i) { auto it = vals.find(i); - IntrusivePtr rval; if ( it == vals.end() ) { - rval = make_intrusive(IntrusivePtr{NewRef{}, this}, i); - vals[i] = rval; + auto ev = make_intrusive(IntrusivePtr{NewRef{}, this}, i); + return vals.emplace(i, std::move(ev)).first->second; } - else - rval = it->second; - return rval; + return it->second; } void EnumType::DescribeReST(ODesc* d, bool roles_only) const diff --git a/src/Type.h b/src/Type.h index 5787424c58..0351c8a6ee 100644 --- a/src/Type.h +++ b/src/Type.h @@ -723,7 +723,7 @@ public: void DescribeReST(ODesc* d, bool roles_only = false) const override; - IntrusivePtr GetVal(bro_int_t i); + const IntrusivePtr& GetVal(bro_int_t i); protected: void AddNameInternal(const std::string& module_name, diff --git a/src/analyzer/protocol/rpc/MOUNT.cc b/src/analyzer/protocol/rpc/MOUNT.cc index 501f5a8563..ab5193725d 100644 --- a/src/analyzer/protocol/rpc/MOUNT.cc +++ b/src/analyzer/protocol/rpc/MOUNT.cc @@ -79,7 +79,7 @@ bool MOUNT_Interp::RPC_BuildReply(RPC_CallInfo* c, BifEnum::rpc_status rpc_statu double last_time, int reply_len) { EventHandlerPtr event = nullptr; - Val* reply = nullptr; + IntrusivePtr reply; BifEnum::MOUNT3::status_t mount_status = BifEnum::MOUNT3::MNT3_OK; bool rpc_success = ( rpc_status == BifEnum::RPC_SUCCESS ); @@ -114,19 +114,17 @@ bool MOUNT_Interp::RPC_BuildReply(RPC_CallInfo* c, BifEnum::rpc_status rpc_statu break; case BifEnum::MOUNT3::PROC_MNT: - reply = mount3_mnt_reply(buf, n, mount_status); + reply = {AdoptRef{}, mount3_mnt_reply(buf, n, mount_status)}; event = mount_proc_mnt; break; case BifEnum::MOUNT3::PROC_UMNT: - reply = nullptr; n = 0; mount_status = BifEnum::MOUNT3::MNT3_OK; event = mount_proc_umnt; break; case BifEnum::MOUNT3::PROC_UMNT_ALL: - reply = nullptr; n = 0; mount_status = BifEnum::MOUNT3::MNT3_OK; event = mount_proc_umnt; @@ -139,7 +137,7 @@ bool MOUNT_Interp::RPC_BuildReply(RPC_CallInfo* c, BifEnum::rpc_status rpc_statu // Otherwise DeliverRPC would complain about // excess_RPC. n = 0; - reply = zeek::BifType::Enum::MOUNT3::proc_t->GetVal(c->Proc()).release(); + reply = zeek::BifType::Enum::MOUNT3::proc_t->GetVal(c->Proc()); event = mount_proc_not_implemented; } else @@ -148,9 +146,7 @@ bool MOUNT_Interp::RPC_BuildReply(RPC_CallInfo* c, BifEnum::rpc_status rpc_statu if ( rpc_success && ! buf ) { - // There was a parse error. We have to unref the reply. (see - // also comments in RPC_BuildCall. - Unref(reply); + // There was a parse error. reply = nullptr; return false; } @@ -172,12 +168,11 @@ bool MOUNT_Interp::RPC_BuildReply(RPC_CallInfo* c, BifEnum::rpc_status rpc_statu vl.emplace_back(AdoptRef{}, request); if ( reply ) - vl.emplace_back(AdoptRef{}, reply); + vl.emplace_back(reply); analyzer->EnqueueConnEvent(event, std::move(vl)); } - else - Unref(reply); + return true; } @@ -221,7 +216,8 @@ zeek::Args MOUNT_Interp::event_common_vl(RPC_CallInfo *c, EnumVal* MOUNT_Interp::mount3_auth_flavor(const u_char*& buf, int& n) { BifEnum::MOUNT3::auth_flavor_t t = (BifEnum::MOUNT3::auth_flavor_t)extract_XDR_uint32(buf, n); - return zeek::BifType::Enum::MOUNT3::auth_flavor_t->GetVal(t).release(); + auto rval = zeek::BifType::Enum::MOUNT3::auth_flavor_t->GetVal(t); + return rval.release(); } StringVal* MOUNT_Interp::mount3_fh(const u_char*& buf, int& n) diff --git a/src/analyzer/protocol/rpc/NFS.cc b/src/analyzer/protocol/rpc/NFS.cc index 3e343f2e73..c97bcf7b3c 100644 --- a/src/analyzer/protocol/rpc/NFS.cc +++ b/src/analyzer/protocol/rpc/NFS.cc @@ -251,7 +251,8 @@ bool NFS_Interp::RPC_BuildReply(RPC_CallInfo* c, BifEnum::rpc_status rpc_status, // Otherwise DeliverRPC would complain about // excess_RPC. n = 0; - reply = zeek::BifType::Enum::NFS3::proc_t->GetVal(c->Proc()).release(); + auto ev = zeek::BifType::Enum::NFS3::proc_t->GetVal(c->Proc()); + reply = ev.release(); event = nfs_proc_not_implemented; } else @@ -438,13 +439,15 @@ RecordVal* NFS_Interp::nfs3_fattr(const u_char*& buf, int& n) EnumVal* NFS_Interp::nfs3_time_how(const u_char*& buf, int& n) { BifEnum::NFS3::time_how_t t = (BifEnum::NFS3::time_how_t)extract_XDR_uint32(buf, n); - return zeek::BifType::Enum::NFS3::time_how_t->GetVal(t).release(); + auto rval = zeek::BifType::Enum::NFS3::time_how_t->GetVal(t); + return rval.release(); } EnumVal* NFS_Interp::nfs3_ftype(const u_char*& buf, int& n) { BifEnum::NFS3::file_type_t t = (BifEnum::NFS3::file_type_t)extract_XDR_uint32(buf, n); - return zeek::BifType::Enum::NFS3::file_type_t->GetVal(t).release(); + auto rval = zeek::BifType::Enum::NFS3::file_type_t->GetVal(t); + return rval.release(); } RecordVal* NFS_Interp::nfs3_wcc_attr(const u_char*& buf, int& n) @@ -533,7 +536,8 @@ RecordVal* NFS_Interp::nfs3_pre_op_attr(const u_char*& buf, int& n) EnumVal *NFS_Interp::nfs3_stable_how(const u_char*& buf, int& n) { BifEnum::NFS3::stable_how_t stable = (BifEnum::NFS3::stable_how_t)extract_XDR_uint32(buf, n); - return zeek::BifType::Enum::NFS3::stable_how_t->GetVal(stable).release(); + auto rval = zeek::BifType::Enum::NFS3::stable_how_t->GetVal(stable); + return rval.release(); } RecordVal* NFS_Interp::nfs3_lookup_reply(const u_char*& buf, int& n, BifEnum::NFS3::status_t status) diff --git a/src/broker/Data.cc b/src/broker/Data.cc index de449b820f..395afc4cc1 100644 --- a/src/broker/Data.cc +++ b/src/broker/Data.cc @@ -196,7 +196,8 @@ struct val_converter { if ( i == -1 ) return nullptr; - return etype->GetVal(i).release(); + auto rval = etype->GetVal(i); + return rval.release(); } return nullptr; diff --git a/src/broker/Store.cc b/src/broker/Store.cc index 0e6bf5b4f6..c66b993ebd 100644 --- a/src/broker/Store.cc +++ b/src/broker/Store.cc @@ -20,7 +20,8 @@ EnumVal* query_status(bool success) failure_val = store_query_status->Lookup("Broker", "FAILURE"); } - return store_query_status->GetVal(success ? success_val : failure_val).release(); + auto rval = store_query_status->GetVal(success ? success_val : failure_val); + return rval.release(); } void StoreHandleVal::ValDescribe(ODesc* d) const diff --git a/src/input/Manager.cc b/src/input/Manager.cc index 9c0060801d..4b80d657a1 100644 --- a/src/input/Manager.cc +++ b/src/input/Manager.cc @@ -1072,8 +1072,8 @@ void Manager::SendEntry(ReaderFrontend* reader, Value* *vals) else if ( i->stream_type == EVENT_STREAM ) { - EnumVal* type = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW).release(); - readFields = SendEventStreamEvent(i, type, vals); + auto type = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW); + readFields = SendEventStreamEvent(i, type.release(), vals); } else if ( i->stream_type == ANALYSIS_STREAM ) @@ -1166,7 +1166,7 @@ int Manager::SendEntryTable(Stream* i, const Value* const *vals) // call stream first to determine if we really add / change the entry if ( stream->pred && ! convert_error ) { - EnumVal* ev; + IntrusivePtr ev; int startpos = 0; bool pred_convert_error = false; predidx = ValueToRecordVal(i, vals, stream->itype, &startpos, pred_convert_error); @@ -1177,15 +1177,15 @@ int Manager::SendEntryTable(Stream* i, const Value* const *vals) if ( ! pred_convert_error ) { if ( updated ) - ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_CHANGED).release(); + ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_CHANGED); else - ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW).release(); + ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW); bool result; if ( stream->num_val_fields > 0 ) // we have values - result = CallPred(stream->pred, 3, ev, predidx->Ref(), valval->Ref()); + result = CallPred(stream->pred, 3, ev.release(), predidx->Ref(), valval->Ref()); else // no values - result = CallPred(stream->pred, 2, ev, predidx->Ref()); + result = CallPred(stream->pred, 2, ev.release(), predidx->Ref()); if ( result == false ) { @@ -1265,7 +1265,6 @@ int Manager::SendEntryTable(Stream* i, const Value* const *vals) if ( stream->event ) { - EnumVal* ev; int startpos = 0; Val* predidx = ValueToRecordVal(i, vals, stream->itype, &startpos, convert_error); @@ -1278,20 +1277,20 @@ int Manager::SendEntryTable(Stream* i, const Value* const *vals) else if ( updated ) { // in case of update send back the old value. assert ( stream->num_val_fields > 0 ); - ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_CHANGED).release(); + auto ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_CHANGED); assert ( oldval != nullptr ); - SendEvent(stream->event, 4, stream->description->Ref(), ev, predidx, oldval.release()); + SendEvent(stream->event, 4, stream->description->Ref(), ev.release(), predidx, oldval.release()); } else { - ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW).release(); + auto ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW); if ( stream->num_val_fields == 0 ) { Ref(stream->description); - SendEvent(stream->event, 3, stream->description->Ref(), ev, predidx); + SendEvent(stream->event, 3, stream->description->Ref(), ev.release(), predidx); } else - SendEvent(stream->event, 4, stream->description->Ref(), ev, predidx, valval->Ref()); + SendEvent(stream->event, 4, stream->description->Ref(), ev.release(), predidx, valval->Ref()); } } @@ -1335,9 +1334,8 @@ void Manager::EndCurrentSend(ReaderFrontend* reader) while ( ( ih = stream->lastDict->NextEntry(lastDictIdxKey, c) ) ) { IntrusivePtr val; - - Val* predidx = nullptr; - EnumVal* ev = nullptr; + IntrusivePtr predidx; + IntrusivePtr ev; int startpos = 0; if ( stream->pred || stream->event ) @@ -1346,25 +1344,21 @@ void Manager::EndCurrentSend(ReaderFrontend* reader) assert(idx != nullptr); val = stream->tab->Lookup(idx.get()); assert(val != nullptr); - predidx = ListValToRecordVal(idx.get(), stream->itype, &startpos); - ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_REMOVED).release(); + predidx = {AdoptRef{}, ListValToRecordVal(idx.get(), stream->itype, &startpos)}; + ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_REMOVED); } if ( stream->pred ) { // ask predicate, if we want to expire this element... - Ref(ev); - Ref(predidx); - - bool result = CallPred(stream->pred, 3, ev, predidx, IntrusivePtr{val}.release()); + bool result = CallPred(stream->pred, 3, ev->Ref(), predidx->Ref(), + val->Ref()); if ( result == false ) { // Keep it. Hence - we quit and simply go to the next entry of lastDict // ah well - and we have to add the entry to currDict... - Unref(predidx); - Unref(ev); stream->currDict->Insert(lastDictIdxKey, stream->lastDict->RemoveEntry(lastDictIdxKey)); delete lastDictIdxKey; continue; @@ -1372,17 +1366,8 @@ void Manager::EndCurrentSend(ReaderFrontend* reader) } if ( stream->event ) - { - Ref(predidx); - Ref(ev); - SendEvent(stream->event, 4, stream->description->Ref(), ev, predidx, IntrusivePtr{val}.release()); - } - - if ( predidx ) // if we have a stream or an event... - Unref(predidx); - - if ( ev ) - Unref(ev); + SendEvent(stream->event, 4, stream->description->Ref(), ev->Ref(), + predidx->Ref(), val->Ref()); stream->tab->Delete(ih->idxkey); stream->lastDict->Remove(lastDictIdxKey); // delete in next line @@ -1454,8 +1439,8 @@ void Manager::Put(ReaderFrontend* reader, Value* *vals) else if ( i->stream_type == EVENT_STREAM ) { - EnumVal* type = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW).release(); - readFields = SendEventStreamEvent(i, type, vals); + auto type = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW); + readFields = SendEventStreamEvent(i, type.release(), vals); } else if ( i->stream_type == ANALYSIS_STREAM ) @@ -1581,7 +1566,7 @@ int Manager::PutTable(Stream* i, const Value* const *vals) // predicate if we want the update or not if ( stream->pred ) { - EnumVal* ev; + IntrusivePtr ev; int startpos = 0; bool pred_convert_error = false; Val* predidx = ValueToRecordVal(i, vals, stream->itype, &startpos, pred_convert_error); @@ -1591,18 +1576,18 @@ int Manager::PutTable(Stream* i, const Value* const *vals) else { if ( updated ) - ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_CHANGED).release(); + ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_CHANGED); else - ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW).release(); + ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW); bool result; if ( stream->num_val_fields > 0 ) // we have values { Ref(valval); - result = CallPred(stream->pred, 3, ev, predidx, valval); + result = CallPred(stream->pred, 3, ev.release(), predidx, valval); } else // no values - result = CallPred(stream->pred, 2, ev, predidx); + result = CallPred(stream->pred, 2, ev.release(), predidx); if ( result == false ) { @@ -1619,7 +1604,6 @@ int Manager::PutTable(Stream* i, const Value* const *vals) if ( stream->event ) { - EnumVal* ev; int startpos = 0; bool event_convert_error = false; Val* predidx = ValueToRecordVal(i, vals, stream->itype, &startpos, event_convert_error); @@ -1632,20 +1616,20 @@ int Manager::PutTable(Stream* i, const Value* const *vals) { // in case of update send back the old value. assert ( stream->num_val_fields > 0 ); - ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_CHANGED).release(); + auto ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_CHANGED); assert ( oldval != nullptr ); SendEvent(stream->event, 4, stream->description->Ref(), - ev, predidx, oldval.release()); + ev.release(), predidx, oldval.release()); } else { - ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW).release(); + auto ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_NEW); if ( stream->num_val_fields == 0 ) SendEvent(stream->event, 4, stream->description->Ref(), - ev, predidx); + ev.release(), predidx); else SendEvent(stream->event, 4, stream->description->Ref(), - ev, predidx, valval->Ref()); + ev.release(), predidx, valval->Ref()); } } @@ -1724,9 +1708,9 @@ bool Manager::Delete(ReaderFrontend* reader, Value* *vals) Unref(predidx); else { - EnumVal* ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_REMOVED).release(); + auto ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_REMOVED); - streamresult = CallPred(stream->pred, 3, ev, predidx, IntrusivePtr{val}.release()); + streamresult = CallPred(stream->pred, 3, ev.release(), predidx, IntrusivePtr{val}.release()); if ( streamresult == false ) { @@ -1743,8 +1727,8 @@ bool Manager::Delete(ReaderFrontend* reader, Value* *vals) { Ref(idxval); assert(val != nullptr); - EnumVal* ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_REMOVED).release(); - SendEvent(stream->event, 4, stream->description->Ref(), ev, idxval, IntrusivePtr{val}.release()); + auto ev = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_REMOVED); + SendEvent(stream->event, 4, stream->description->Ref(), ev.release(), idxval, IntrusivePtr{val}.release()); } } @@ -1758,8 +1742,8 @@ bool Manager::Delete(ReaderFrontend* reader, Value* *vals) else if ( i->stream_type == EVENT_STREAM ) { - EnumVal* type = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_REMOVED).release(); - readVals = SendEventStreamEvent(i, type, vals); + auto type = zeek::BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_REMOVED); + readVals = SendEventStreamEvent(i, type.release(), vals); success = true; } @@ -2385,7 +2369,8 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, BroType* request_typ return nullptr; } - return request_type->AsEnumType()->GetVal(index).release(); + auto rval = request_type->AsEnumType()->GetVal(index); + return rval.release(); } default: @@ -2583,7 +2568,8 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, bool& have_error) co return nullptr; } - return t->GetVal(intval).release(); + auto rval = t->GetVal(intval); + return rval.release(); } default: @@ -2711,19 +2697,19 @@ void Manager::ErrorHandler(const Stream* i, ErrorType et, bool reporter_send, co // send our script level error event if ( i->error_event ) { - EnumVal* ev; + IntrusivePtr ev; switch (et) { case ErrorType::INFO: - ev = zeek::BifType::Enum::Reporter::Level->GetVal(BifEnum::Reporter::INFO).release(); + ev = zeek::BifType::Enum::Reporter::Level->GetVal(BifEnum::Reporter::INFO); break; case ErrorType::WARNING: - ev = zeek::BifType::Enum::Reporter::Level->GetVal(BifEnum::Reporter::WARNING).release(); + ev = zeek::BifType::Enum::Reporter::Level->GetVal(BifEnum::Reporter::WARNING); break; case ErrorType::ERROR: - ev = zeek::BifType::Enum::Reporter::Level->GetVal(BifEnum::Reporter::ERROR).release(); + ev = zeek::BifType::Enum::Reporter::Level->GetVal(BifEnum::Reporter::ERROR); break; default: @@ -2732,7 +2718,7 @@ void Manager::ErrorHandler(const Stream* i, ErrorType et, bool reporter_send, co } StringVal* message = new StringVal(buf); - SendEvent(i->error_event, 3, i->description->Ref(), message, ev); + SendEvent(i->error_event, 3, i->description->Ref(), message, ev.release()); } if ( reporter_send ) diff --git a/src/logging/Manager.cc b/src/logging/Manager.cc index e351ee2fe3..cbaf7c53e7 100644 --- a/src/logging/Manager.cc +++ b/src/logging/Manager.cc @@ -1290,7 +1290,7 @@ void Manager::SendAllWritersTo(const broker::endpoint_info& ei) i != stream->writers.end(); i++ ) { WriterFrontend* writer = i->second->writer; - auto writer_val = et->GetVal(i->first.first); + const auto& writer_val = et->GetVal(i->first.first); broker_mgr->PublishLogCreate((*s)->id, writer_val.get(), *i->second->info,