Deprecate VectorVal::Assign methods taking raw Val*

And adapt usages to pass in to alternate method taking IntrusivePtr
This commit is contained in:
Jon Siwek 2020-05-21 15:31:04 -07:00
parent 2cbf36721c
commit de1e3d7d6d
15 changed files with 77 additions and 54 deletions

3
NEWS
View file

@ -211,6 +211,9 @@ Deprecated Functionality
- ``TableVal::Lookup()`` is deprecated, use ``TableVal::Find()`` or - ``TableVal::Lookup()`` is deprecated, use ``TableVal::Find()`` or
``TableVal::FindOrDefault()``. ``TableVal::FindOrDefault()``.
- ``VectorVal::Assign`` methods taking raw ``Val*`` are deprecated, use the
methods that take ``IntrusivePtr``.
Zeek 3.1.0 Zeek 3.1.0
========== ==========

View file

@ -345,9 +345,9 @@ VectorVal* BroString:: VecToPolicy(Vec* vec)
for ( unsigned int i = 0; i < vec->size(); ++i ) for ( unsigned int i = 0; i < vec->size(); ++i )
{ {
BroString* string = (*vec)[i]; BroString* string = (*vec)[i];
StringVal* val = new StringVal(string->Len(), auto val = make_intrusive<StringVal>(string->Len(),
(const char*) string->Bytes()); (const char*) string->Bytes());
result->Assign(i+1, val); result->Assign(i+1, std::move(val));
} }
return result.release(); return result.release();

View file

@ -1572,7 +1572,7 @@ IntrusivePtr<Val> BoolExpr::Eval(Frame* f) const
{ {
result = make_intrusive<VectorVal>(GetType<VectorType>()); result = make_intrusive<VectorVal>(GetType<VectorType>());
result->Resize(vector_v->Size()); result->Resize(vector_v->Size());
result->AssignRepeat(0, result->Size(), scalar_v.get()); result->AssignRepeat(0, result->Size(), std::move(scalar_v));
} }
else else
result = std::move(vector_v); result = std::move(vector_v);
@ -1937,7 +1937,7 @@ IntrusivePtr<Val> CondExpr::Eval(Frame* f) const
if ( local_cond ) if ( local_cond )
{ {
Val* v = local_cond->IsZero() ? b->Lookup(i) : a->Lookup(i); Val* v = local_cond->IsZero() ? b->Lookup(i) : a->Lookup(i);
result->Assign(i, v ? v->Ref() : nullptr); result->Assign(i, v ? IntrusivePtr{NewRef{}, v} : nullptr);
} }
else else
result->Assign(i, nullptr); result->Assign(i, nullptr);
@ -2595,7 +2595,7 @@ IntrusivePtr<Val> IndexExpr::Eval(Frame* f) const
if ( v_v2->Lookup(i)->AsBool() ) if ( v_v2->Lookup(i)->AsBool() )
{ {
auto a = v_v1->Lookup(i); auto a = v_v1->Lookup(i);
v_result->Assign(v_result->Size() + 1, a ? a->Ref() : nullptr); v_result->Assign(v_result->Size() + 1, a ? IntrusivePtr{NewRef{}, a} : nullptr);
} }
} }
} }
@ -2608,7 +2608,7 @@ IntrusivePtr<Val> IndexExpr::Eval(Frame* f) const
for ( unsigned int i = 0; i < v_v2->Size(); ++i ) for ( unsigned int i = 0; i < v_v2->Size(); ++i )
{ {
auto a = v_v1->Lookup(v_v2->Lookup(i)->CoerceToInt()); auto a = v_v1->Lookup(v_v2->Lookup(i)->CoerceToInt());
v_result->Assign(i, a ? a->Ref() : nullptr); v_result->Assign(i, a ? IntrusivePtr{NewRef{}, a} : nullptr);
} }
} }
@ -2659,7 +2659,7 @@ IntrusivePtr<Val> IndexExpr::Fold(Val* v1, Val* v2) const
for ( int idx = first; idx < last; idx++ ) for ( int idx = first; idx < last; idx++ )
{ {
auto a = vect->Lookup(idx); auto a = vect->Lookup(idx);
result->Assign(idx - first, a ? a->Ref() : nullptr); result->Assign(idx - first, a ? IntrusivePtr{NewRef{}, a} : nullptr);
} }
} }
@ -2758,7 +2758,7 @@ void IndexExpr::Assign(Frame* f, IntrusivePtr<Val> v)
for ( auto idx = 0u; idx < v_vect->Size(); idx++, first++ ) for ( auto idx = 0u; idx < v_vect->Size(); idx++, first++ )
v1_vect->Insert(first, v_vect->Lookup(idx)->Ref()); v1_vect->Insert(first, v_vect->Lookup(idx)->Ref());
} }
else if ( ! v1_vect->Assign(v2.get(), std::move(v)) ) else if ( ! v1_vect->Assign(lv->Idx(0)->CoerceToUnsigned(), std::move(v)) )
{ {
v = std::move(v_extra); v = std::move(v_extra);

View file

@ -3080,18 +3080,13 @@ bool VectorVal::Assign(unsigned int index, IntrusivePtr<Val> element)
return true; return true;
} }
bool VectorVal::Assign(unsigned int index, Val* element)
{
return Assign(index, {AdoptRef{}, element});
}
bool VectorVal::AssignRepeat(unsigned int index, unsigned int how_many, bool VectorVal::AssignRepeat(unsigned int index, unsigned int how_many,
Val* element) IntrusivePtr<Val> element)
{ {
ResizeAtLeast(index + how_many); ResizeAtLeast(index + how_many);
for ( unsigned int i = index; i < index + how_many; ++i ) for ( unsigned int i = index; i < index + how_many; ++i )
if ( ! Assign(i, {NewRef{}, element}) ) if ( ! Assign(i, element) )
return false; return false;
return true; return true;

View file

@ -1245,25 +1245,44 @@ public:
IntrusivePtr<Val> SizeVal() const override; IntrusivePtr<Val> SizeVal() const override;
// Returns false if the type of the argument was wrong. /**
// The vector will automatically grow to accomodate the index. * Assigns an element to a given vector index.
// * @param index The index to assign.
* @param element The element value to assign.
* @return True if the element was successfully assigned, or false if
* the element was the wrong type.
*/
bool Assign(unsigned int index, IntrusivePtr<Val> element);
// Note: does NOT Ref() the element! Remember to do so unless // Note: does NOT Ref() the element! Remember to do so unless
// the element was just created and thus has refcount 1. // the element was just created and thus has refcount 1.
// [[deprecated("Remove in v4.1. Assign an IntrusivePtr instead.")]]
bool Assign(unsigned int index, IntrusivePtr<Val> element); bool Assign(unsigned int index, Val* element)
bool Assign(unsigned int index, Val* element); { return Assign(index, {AdoptRef{}, element}); }
// Note: the following nullptr method can also go upon removing the above.
void Assign(unsigned int index, std::nullptr_t)
{ Assign(index, IntrusivePtr<Val>{}); }
template<typename E> [[deprecated("Remove in v4.1. Assign using integer index and IntrusivePtr element.")]]
bool Assign(Val* index, E&& element) bool Assign(Val* index, Val* element)
{ {
return Assign(index->AsListVal()->Idx(0)->CoerceToUnsigned(), return Assign(index->AsListVal()->Idx(0)->CoerceToUnsigned(),
std::forward<E>(element)); {AdoptRef{}, element});
} }
// Assigns the value to how_many locations starting at index. /**
* Assigns a given value to multiple indices in the vector.
* @param index The starting index to assign to.
* @param how_many The number of indices to assign, counting from *index*.
* @return True if the elements were successfully assigned, or false if
* the element was the wrong type.
*/
bool AssignRepeat(unsigned int index, unsigned int how_many, bool AssignRepeat(unsigned int index, unsigned int how_many,
Val* element); IntrusivePtr<Val> element);
[[deprecated("Remove in v4.1. Assign an IntrusivePtr instead.")]]
bool AssignRepeat(unsigned int index, unsigned int how_many, Val* element)
{ return AssignRepeat(index, how_many, {NewRef{}, element}); }
// Add this value to the given value (if appropriate). // Add this value to the given value (if appropriate).
// Returns true if succcessful. // Returns true if succcessful.

View file

@ -750,11 +750,11 @@ refine flow DHCP_Flow += {
for ( auto ptrsubopt = ${v.relay_agent_inf}->begin(); for ( auto ptrsubopt = ${v.relay_agent_inf}->begin();
ptrsubopt != ${v.relay_agent_inf}->end(); ++ptrsubopt ) ptrsubopt != ${v.relay_agent_inf}->end(); ++ptrsubopt )
{ {
auto r = new RecordVal(zeek::BifType::Record::DHCP::SubOpt); auto r = make_intrusive<RecordVal>(zeek::BifType::Record::DHCP::SubOpt);
r->Assign(0, val_mgr->Count((*ptrsubopt)->code())); r->Assign(0, val_mgr->Count((*ptrsubopt)->code()));
r->Assign(1, to_stringval((*ptrsubopt)->value())); r->Assign(1, to_stringval((*ptrsubopt)->value()));
relay_agent_sub_opt->Assign(i, r); relay_agent_sub_opt->Assign(i, std::move(r));
++i; ++i;
} }

View file

@ -36,26 +36,26 @@ IntrusivePtr<VectorVal> proc_padata(const KRB_PA_Data_Sequence* data, const BroA
break; break;
case PA_PW_SALT: case PA_PW_SALT:
{ {
RecordVal * type_val = new RecordVal(zeek::BifType::Record::KRB::Type_Value); auto type_val = make_intrusive<RecordVal>(zeek::BifType::Record::KRB::Type_Value);
type_val->Assign(0, val_mgr->Count(element->data_type())); type_val->Assign(0, val_mgr->Count(element->data_type()));
type_val->Assign(1, to_stringval(element->pa_data_element()->pa_pw_salt()->encoding()->content())); type_val->Assign(1, to_stringval(element->pa_data_element()->pa_pw_salt()->encoding()->content()));
vv->Assign(vv->Size(), type_val); vv->Assign(vv->Size(), std::move(type_val));
break; break;
} }
case PA_ENCTYPE_INFO: case PA_ENCTYPE_INFO:
{ {
RecordVal * type_val = new RecordVal(zeek::BifType::Record::KRB::Type_Value); auto type_val = make_intrusive<RecordVal>(zeek::BifType::Record::KRB::Type_Value);
type_val->Assign(0, val_mgr->Count(element->data_type())); type_val->Assign(0, val_mgr->Count(element->data_type()));
type_val->Assign(1, to_stringval(element->pa_data_element()->pf_enctype_info()->salt())); type_val->Assign(1, to_stringval(element->pa_data_element()->pf_enctype_info()->salt()));
vv->Assign(vv->Size(), type_val); vv->Assign(vv->Size(), std::move(type_val));
break; break;
} }
case PA_ENCTYPE_INFO2: case PA_ENCTYPE_INFO2:
{ {
RecordVal * type_val = new RecordVal(zeek::BifType::Record::KRB::Type_Value); auto type_val = make_intrusive<RecordVal>(zeek::BifType::Record::KRB::Type_Value);
type_val->Assign(0, val_mgr->Count(element->data_type())); type_val->Assign(0, val_mgr->Count(element->data_type()));
type_val->Assign(1, to_stringval(element->pa_data_element()->pf_enctype_info2()->salt())); type_val->Assign(1, to_stringval(element->pa_data_element()->pf_enctype_info2()->salt()));
vv->Assign(vv->Size(), type_val); vv->Assign(vv->Size(), std::move(type_val));
break; break;
} }
case PA_PW_AS_REQ: case PA_PW_AS_REQ:
@ -110,10 +110,10 @@ IntrusivePtr<VectorVal> proc_padata(const KRB_PA_Data_Sequence* data, const BroA
{ {
if ( ! is_error && element->pa_data_element()->unknown()->meta()->length() > 0 ) if ( ! is_error && element->pa_data_element()->unknown()->meta()->length() > 0 )
{ {
RecordVal * type_val = new RecordVal(zeek::BifType::Record::KRB::Type_Value); auto type_val = make_intrusive<RecordVal>(zeek::BifType::Record::KRB::Type_Value);
type_val->Assign(0, val_mgr->Count(element->data_type())); type_val->Assign(0, val_mgr->Count(element->data_type()));
type_val->Assign(1, to_stringval(element->pa_data_element()->unknown()->content())); type_val->Assign(1, to_stringval(element->pa_data_element()->unknown()->content()));
vv->Assign(vv->Size(), type_val); vv->Assign(vv->Size(), std::move(type_val));
} }
break; break;
} }

View file

@ -765,7 +765,7 @@ IntrusivePtr<RecordVal> NFS_Interp::nfs3_readdir_reply(bool isplus, const u_char
while ( extract_XDR_uint32(buf,n) ) while ( extract_XDR_uint32(buf,n) )
{ {
RecordVal *entry = new RecordVal(zeek::BifType::Record::NFS3::direntry_t); auto entry = make_intrusive<RecordVal>(zeek::BifType::Record::NFS3::direntry_t);
entry->Assign(0, ExtractUint64(buf,n)); // fileid entry->Assign(0, ExtractUint64(buf,n)); // fileid
entry->Assign(1, nfs3_filename(buf,n)); // fname entry->Assign(1, nfs3_filename(buf,n)); // fname
entry->Assign(2, ExtractUint64(buf,n)); // cookie entry->Assign(2, ExtractUint64(buf,n)); // cookie
@ -776,7 +776,7 @@ IntrusivePtr<RecordVal> NFS_Interp::nfs3_readdir_reply(bool isplus, const u_char
entry->Assign(4, nfs3_post_op_fh(buf,n)); entry->Assign(4, nfs3_post_op_fh(buf,n));
} }
entries->Assign(pos, entry); entries->Assign(pos, std::move(entry));
pos++; pos++;
} }

View file

@ -161,10 +161,10 @@ refine connection Handshake_Conn += {
{ {
for ( unsigned int i = 0; i < supported_signature_algorithms->size(); ++i ) for ( unsigned int i = 0; i < supported_signature_algorithms->size(); ++i )
{ {
RecordVal* el = new RecordVal(zeek::BifType::Record::SSL::SignatureAndHashAlgorithm); auto el = make_intrusive<RecordVal>(zeek::BifType::Record::SSL::SignatureAndHashAlgorithm);
el->Assign(0, val_mgr->Count((*supported_signature_algorithms)[i]->HashAlgorithm())); el->Assign(0, val_mgr->Count((*supported_signature_algorithms)[i]->HashAlgorithm()));
el->Assign(1, val_mgr->Count((*supported_signature_algorithms)[i]->SignatureAlgorithm())); el->Assign(1, val_mgr->Count((*supported_signature_algorithms)[i]->SignatureAlgorithm()));
slist->Assign(i, el); slist->Assign(i, std::move(el));
} }
} }
@ -498,10 +498,10 @@ refine connection Handshake_Conn += {
{ {
for ( auto&& identity : *(identities->identities()) ) for ( auto&& identity : *(identities->identities()) )
{ {
RecordVal* el = new RecordVal(zeek::BifType::Record::SSL::PSKIdentity); auto el = make_intrusive<RecordVal>(zeek::BifType::Record::SSL::PSKIdentity);
el->Assign(0, make_intrusive<StringVal>(identity->identity().length(), (const char*) identity->identity().data())); el->Assign(0, make_intrusive<StringVal>(identity->identity().length(), (const char*) identity->identity().data()));
el->Assign(1, val_mgr->Count(identity->obfuscated_ticket_age())); el->Assign(1, val_mgr->Count(identity->obfuscated_ticket_age()));
slist->Assign(slist->Size(), el); slist->Assign(slist->Size(), std::move(el));
} }
} }

View file

@ -1358,7 +1358,7 @@ int TCP_Analyzer::ParseTCPOptions(const struct tcphdr* tcp, bool is_orig)
{ {
auto option_list = make_intrusive<VectorVal>(zeek::BifType::Vector::TCP::OptionList); auto option_list = make_intrusive<VectorVal>(zeek::BifType::Vector::TCP::OptionList);
auto add_option_data = [](RecordVal* rv, const u_char* odata, int olen) auto add_option_data = [](const IntrusivePtr<RecordVal>& rv, const u_char* odata, int olen)
{ {
if ( olen <= 2 ) if ( olen <= 2 )
return; return;
@ -1372,7 +1372,7 @@ int TCP_Analyzer::ParseTCPOptions(const struct tcphdr* tcp, bool is_orig)
{ {
auto kind = o[0]; auto kind = o[0];
auto length = kind < 2 ? 1 : o[1]; auto length = kind < 2 ? 1 : o[1];
auto option_record = new RecordVal(zeek::BifType::Record::TCP::Option); auto option_record = make_intrusive<RecordVal>(zeek::BifType::Record::TCP::Option);
option_list->Assign(option_list->Size(), option_record); option_list->Assign(option_list->Size(), option_record);
option_record->Assign(0, val_mgr->Count(kind)); option_record->Assign(0, val_mgr->Count(kind));
option_record->Assign(1, val_mgr->Count(length)); option_record->Assign(1, val_mgr->Count(length));

View file

@ -102,7 +102,7 @@ function Broker::__peers%(%): PeerInfos
const auto& pi = zeek::id::find_type<RecordType>("Broker::PeerInfo"); const auto& pi = zeek::id::find_type<RecordType>("Broker::PeerInfo");
const auto& ei = zeek::id::find_type<RecordType>("Broker::EndpointInfo"); const auto& ei = zeek::id::find_type<RecordType>("Broker::EndpointInfo");
const auto& ni = zeek::id::find_type<RecordType>("Broker::NetworkInfo"); const auto& ni = zeek::id::find_type<RecordType>("Broker::NetworkInfo");
auto peer_info = new RecordVal(pi); auto peer_info = make_intrusive<RecordVal>(pi);
auto endpoint_info = make_intrusive<RecordVal>(ei); auto endpoint_info = make_intrusive<RecordVal>(ei);
auto network_info = make_intrusive<RecordVal>(ni); auto network_info = make_intrusive<RecordVal>(ni);
auto n = p.peer.network; auto n = p.peer.network;
@ -125,7 +125,7 @@ function Broker::__peers%(%): PeerInfos
peer_info->Assign(0, std::move(endpoint_info)); peer_info->Assign(0, std::move(endpoint_info));
peer_info->Assign(1, zeek::BifType::Enum::Broker::PeerStatus->GetVal(ps)); peer_info->Assign(1, zeek::BifType::Enum::Broker::PeerStatus->GetVal(ps));
rval->Assign(i, peer_info); rval->Assign(i, std::move(peer_info));
++i; ++i;
} }

View file

@ -362,7 +362,7 @@ void file_analysis::X509::ParseSAN(X509_EXTENSION* ext)
#else #else
const char* name = (const char*) ASN1_STRING_get0_data(gen->d.ia5); const char* name = (const char*) ASN1_STRING_get0_data(gen->d.ia5);
#endif #endif
StringVal* bs = new StringVal(name); auto bs = make_intrusive<StringVal>(name);
switch ( gen->type ) switch ( gen->type )
{ {
@ -370,21 +370,21 @@ void file_analysis::X509::ParseSAN(X509_EXTENSION* ext)
if ( names == nullptr ) if ( names == nullptr )
names = make_intrusive<VectorVal>(zeek::id::string_vec); names = make_intrusive<VectorVal>(zeek::id::string_vec);
names->Assign(names->Size(), bs); names->Assign(names->Size(), std::move(bs));
break; break;
case GEN_URI: case GEN_URI:
if ( uris == nullptr ) if ( uris == nullptr )
uris = make_intrusive<VectorVal>(zeek::id::string_vec); uris = make_intrusive<VectorVal>(zeek::id::string_vec);
uris->Assign(uris->Size(), bs); uris->Assign(uris->Size(), std::move(bs));
break; break;
case GEN_EMAIL: case GEN_EMAIL:
if ( emails == nullptr ) if ( emails == nullptr )
emails = make_intrusive<VectorVal>(zeek::id::string_vec); emails = make_intrusive<VectorVal>(zeek::id::string_vec);
emails->Assign(emails->Size(), bs); emails->Assign(emails->Size(), std::move(bs));
break; break;
} }
} }

View file

@ -2275,7 +2275,10 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, BroType* request_typ
auto v = make_intrusive<VectorVal>(std::move(vt)); auto v = make_intrusive<VectorVal>(std::move(vt));
for ( int j = 0; j < val->val.vector_val.size; j++ ) for ( int j = 0; j < val->val.vector_val.size; j++ )
v->Assign(j, ValueToVal(i, val->val.vector_val.vals[j], type.get(), have_error)); {
auto el = ValueToVal(i, val->val.vector_val.vals[j], type.get(), have_error);
v->Assign(j, {AdoptRef{}, el});
}
return v.release(); return v.release();
} }

View file

@ -209,7 +209,7 @@ IntrusivePtr<VectorVal> TopkVal::GetTopK(int k) const // returns vector
while ( eit != (*it)->elements.end() ) while ( eit != (*it)->elements.end() )
{ {
//printf("Size: %ld\n", (*it)->elements.size()); //printf("Size: %ld\n", (*it)->elements.size());
t->Assign(read, (*eit)->value->Ref()); t->Assign(read, (*eit)->value);
read++; read++;
eit++; eit++;
} }

View file

@ -588,7 +588,10 @@ Val* Value::ValueToVal(const std::string& source, const Value* val, bool& have_e
auto v = make_intrusive<VectorVal>(std::move(vt)); auto v = make_intrusive<VectorVal>(std::move(vt));
for ( int j = 0; j < val->val.vector_val.size; j++ ) for ( int j = 0; j < val->val.vector_val.size; j++ )
v->Assign(j, ValueToVal(source, val->val.vector_val.vals[j], have_error)); {
auto el = ValueToVal(source, val->val.vector_val.vals[j], have_error);
v->Assign(j, {AdoptRef{}, el});
}
return v.release(); return v.release();
} }