Deprecate VectorVal::Lookup(), replace with At()

This commit is contained in:
Jon Siwek 2020-05-21 17:03:46 -07:00
parent 69533bcbc6
commit a384bb8b81
16 changed files with 74 additions and 69 deletions

2
NEWS
View file

@ -216,6 +216,8 @@ Deprecated Functionality
- ``VectorVal::Assign`` and ``Insert`` methods taking raw ``Val*`` are - ``VectorVal::Assign`` and ``Insert`` methods taking raw ``Val*`` are
deprecated, use the methods that take ``IntrusivePtr``. deprecated, use the methods that take ``IntrusivePtr``.
- ``VectorVal::Lookup()`` is deprecated, use ``VectorVal::At()``.
Zeek 3.1.0 Zeek 3.1.0
========== ==========

View file

@ -360,7 +360,7 @@ BroString::Vec* BroString::VecFromPolicy(VectorVal* vec)
// VectorVals start at index 1! // VectorVals start at index 1!
for ( unsigned int i = 1; i <= vec->Size(); ++i ) for ( unsigned int i = 1; i <= vec->Size(); ++i )
{ {
Val* v = vec->Lookup(i); // get the RecordVal const auto& v = vec->At(i); // get the RecordVal
if ( ! v ) if ( ! v )
continue; continue;

View file

@ -266,7 +266,7 @@ char* CompositeHash::SingleValHash(bool type_check, char* kp0,
kp1 = reinterpret_cast<char*>(kp+1); kp1 = reinterpret_cast<char*>(kp+1);
for ( unsigned int i = 0; i < vv->Size(); ++i ) for ( unsigned int i = 0; i < vv->Size(); ++i )
{ {
Val* val = vv->Lookup(i); const auto& val = vv->At(i);
unsigned int* kp = AlignAndPadType<unsigned int>(kp1); unsigned int* kp = AlignAndPadType<unsigned int>(kp1);
*kp = i; *kp = i;
kp1 = reinterpret_cast<char*>(kp+1); kp1 = reinterpret_cast<char*>(kp+1);
@ -277,7 +277,8 @@ char* CompositeHash::SingleValHash(bool type_check, char* kp0,
if ( val ) if ( val )
{ {
if ( ! (kp1 = SingleValHash(type_check, kp1, if ( ! (kp1 = SingleValHash(type_check, kp1,
vt->Yield().get(), val, false)) ) vt->Yield().get(), val.get(),
false)) )
return nullptr; return nullptr;
} }
} }
@ -564,12 +565,12 @@ int CompositeHash::SingleTypeKeySize(BroType* bt, const Val* v,
VectorVal* vv = const_cast<VectorVal*>(v->AsVectorVal()); VectorVal* vv = const_cast<VectorVal*>(v->AsVectorVal());
for ( unsigned int i = 0; i < vv->Size(); ++i ) for ( unsigned int i = 0; i < vv->Size(); ++i )
{ {
Val* val = vv->Lookup(i); const auto& val = vv->At(i);
sz = SizeAlign(sz, sizeof(unsigned int)); sz = SizeAlign(sz, sizeof(unsigned int));
sz = SizeAlign(sz, sizeof(unsigned int)); sz = SizeAlign(sz, sizeof(unsigned int));
if ( val ) if ( val )
sz = SingleTypeKeySize(bt->AsVectorType()->Yield().get(), sz = SingleTypeKeySize(bt->AsVectorType()->Yield().get(),
val, type_check, sz, false, val.get(), type_check, sz, false,
calc_static_size); calc_static_size);
if ( ! sz ) return 0; if ( ! sz ) return 0;
} }

View file

@ -366,8 +366,8 @@ IntrusivePtr<Val> UnaryExpr::Eval(Frame* f) const
for ( unsigned int i = 0; i < v_op->Size(); ++i ) for ( unsigned int i = 0; i < v_op->Size(); ++i )
{ {
Val* v_i = v_op->Lookup(i); const auto& v_i = v_op->At(i);
result->Assign(i, v_i ? Fold(v_i) : nullptr); result->Assign(i, v_i ? Fold(v_i.get()) : nullptr);
} }
return result; return result;
@ -457,8 +457,8 @@ IntrusivePtr<Val> BinaryExpr::Eval(Frame* f) const
for ( unsigned int i = 0; i < v_op1->Size(); ++i ) for ( unsigned int i = 0; i < v_op1->Size(); ++i )
{ {
if ( v_op1->Lookup(i) && v_op2->Lookup(i) ) if ( v_op1->At(i) && v_op2->At(i) )
v_result->Assign(i, Fold(v_op1->Lookup(i), v_op2->Lookup(i))); v_result->Assign(i, Fold(v_op1->At(i).get(), v_op2->At(i).get()));
else else
v_result->Assign(i, nullptr); v_result->Assign(i, nullptr);
// SetError("undefined element in vector operation"); // SetError("undefined element in vector operation");
@ -474,9 +474,9 @@ IntrusivePtr<Val> BinaryExpr::Eval(Frame* f) const
for ( unsigned int i = 0; i < vv->Size(); ++i ) for ( unsigned int i = 0; i < vv->Size(); ++i )
{ {
if ( Val* vv_i = vv->Lookup(i) ) if ( const auto& vv_i = vv->At(i) )
v_result->Assign(i, is_vec1 ? Fold(vv_i, v2.get()) v_result->Assign(i, is_vec1 ? Fold(vv_i.get(), v2.get())
: Fold(v1.get(), vv_i)); : Fold(v1.get(), vv_i.get()));
else else
v_result->Assign(i, nullptr); v_result->Assign(i, nullptr);
@ -971,10 +971,10 @@ IntrusivePtr<Val> IncrExpr::Eval(Frame* f) const
for ( unsigned int i = 0; i < v_vec->Size(); ++i ) for ( unsigned int i = 0; i < v_vec->Size(); ++i )
{ {
Val* elt = v_vec->Lookup(i); const auto& elt = v_vec->At(i);
if ( elt ) if ( elt )
v_vec->Assign(i, DoSingleEval(f, elt)); v_vec->Assign(i, DoSingleEval(f, elt.get()));
else else
v_vec->Assign(i, nullptr); v_vec->Assign(i, nullptr);
} }
@ -1600,8 +1600,8 @@ IntrusivePtr<Val> BoolExpr::Eval(Frame* f) const
for ( unsigned int i = 0; i < vec_v1->Size(); ++i ) for ( unsigned int i = 0; i < vec_v1->Size(); ++i )
{ {
Val* op1 = vec_v1->Lookup(i); const auto& op1 = vec_v1->At(i);
Val* op2 = vec_v2->Lookup(i); const auto& op2 = vec_v2->At(i);
if ( op1 && op2 ) if ( op1 && op2 )
{ {
bool local_result = (tag == EXPR_AND_AND) ? bool local_result = (tag == EXPR_AND_AND) ?
@ -1932,12 +1932,12 @@ IntrusivePtr<Val> CondExpr::Eval(Frame* f) const
for ( unsigned int i = 0; i < cond->Size(); ++i ) for ( unsigned int i = 0; i < cond->Size(); ++i )
{ {
Val* local_cond = cond->Lookup(i); const auto& local_cond = cond->At(i);
if ( local_cond ) if ( local_cond )
{ {
Val* v = local_cond->IsZero() ? b->Lookup(i) : a->Lookup(i); const auto& v = local_cond->IsZero() ? b->At(i) : a->At(i);
result->Assign(i, v ? IntrusivePtr{NewRef{}, v} : nullptr); result->Assign(i, v);
} }
else else
result->Assign(i, nullptr); result->Assign(i, nullptr);
@ -2592,11 +2592,8 @@ 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 )
{ {
if ( v_v2->Lookup(i)->AsBool() ) if ( v_v2->At(i)->AsBool() )
{ v_result->Assign(v_result->Size() + 1, v_v1->At(i));
auto a = v_v1->Lookup(i);
v_result->Assign(v_result->Size() + 1, a ? IntrusivePtr{NewRef{}, a} : nullptr);
}
} }
} }
else else
@ -2606,10 +2603,7 @@ IntrusivePtr<Val> IndexExpr::Eval(Frame* f) const
// Probably only do this if *all* are negative. // Probably only do this if *all* are negative.
v_result->Resize(v_v2->Size()); v_result->Resize(v_v2->Size());
for ( unsigned int i = 0; i < v_v2->Size(); ++i ) for ( unsigned int i = 0; i < v_v2->Size(); ++i )
{ v_result->Assign(i, v_v1->At(v_v2->At(i)->CoerceToInt()));
auto a = v_v1->Lookup(v_v2->Lookup(i)->CoerceToInt());
v_result->Assign(i, a ? IntrusivePtr{NewRef{}, a} : nullptr);
}
} }
return v_result; return v_result;
@ -2642,7 +2636,7 @@ IntrusivePtr<Val> IndexExpr::Fold(Val* v1, Val* v2) const
const ListVal* lv = v2->AsListVal(); const ListVal* lv = v2->AsListVal();
if ( lv->Length() == 1 ) if ( lv->Length() == 1 )
v = {NewRef{}, vect->Lookup(v2)}; v = vect->At(lv->Idx(0)->CoerceToUnsigned());
else else
{ {
size_t len = vect->Size(); size_t len = vect->Size();
@ -2657,10 +2651,7 @@ IntrusivePtr<Val> IndexExpr::Fold(Val* v1, Val* v2) const
result->Resize(sub_length); result->Resize(sub_length);
for ( int idx = first; idx < last; idx++ ) for ( int idx = first; idx < last; idx++ )
{ result->Assign(idx - first, vect->At(idx));
auto a = vect->Lookup(idx);
result->Assign(idx - first, a ? IntrusivePtr{NewRef{}, a} : nullptr);
}
} }
return result; return result;
@ -2756,7 +2747,7 @@ void IndexExpr::Assign(Frame* f, IntrusivePtr<Val> v)
VectorVal* v_vect = v->AsVectorVal(); VectorVal* v_vect = v->AsVectorVal();
for ( auto idx = 0u; idx < v_vect->Size(); idx++, first++ ) for ( auto idx = 0u; idx < v_vect->Size(); idx++, first++ )
v1_vect->Insert(first, {NewRef{}, v_vect->Lookup(idx)}); v1_vect->Insert(first, v_vect->At(idx));
} }
else if ( ! v1_vect->Assign(lv->Idx(0)->CoerceToUnsigned(), std::move(v)) ) else if ( ! v1_vect->Assign(lv->Idx(0)->CoerceToUnsigned(), std::move(v)) )
{ {
@ -3509,8 +3500,8 @@ IntrusivePtr<Val> ArithCoerceExpr::Fold(Val* v) const
for ( unsigned int i = 0; i < vv->Size(); ++i ) for ( unsigned int i = 0; i < vv->Size(); ++i )
{ {
if ( Val* elt = vv->Lookup(i) ) if ( const auto& elt = vv->At(i) )
result->Assign(i, FoldSingleVal(elt, t)); result->Assign(i, FoldSingleVal(elt.get(), t));
else else
result->Assign(i, nullptr); result->Assign(i, nullptr);
} }
@ -3992,7 +3983,7 @@ IntrusivePtr<Val> InExpr::Fold(Val* v1, Val* v2) const
bool res; bool res;
if ( is_vector(v2) ) if ( is_vector(v2) )
res = (bool)v2->AsVectorVal()->Lookup(v1); res = (bool)v2->AsVectorVal()->At(v1->AsListVal()->Idx(0)->CoerceToUnsigned());
else else
res = (bool)v2->AsTableVal()->Find({NewRef{}, v1}); res = (bool)v2->AsTableVal()->Find({NewRef{}, v1});

View file

@ -103,7 +103,7 @@ BroSubstring::Vec* BroSubstring::VecFromPolicy(VectorVal* vec)
// VectorVals start at index 1! // VectorVals start at index 1!
for ( unsigned int i = 1; i <= vec->Size(); ++i ) for ( unsigned int i = 1; i <= vec->Size(); ++i )
{ {
Val* v = vec->Lookup(i); // get the RecordVal const auto& v = vec->At(i); // get the RecordVal
if ( ! v ) if ( ! v )
continue; continue;
@ -113,7 +113,7 @@ BroSubstring::Vec* BroSubstring::VecFromPolicy(VectorVal* vec)
const VectorVal* aligns = v->AsRecordVal()->GetField(1)->AsVectorVal(); const VectorVal* aligns = v->AsRecordVal()->GetField(1)->AsVectorVal();
for ( unsigned int j = 1; j <= aligns->Size(); ++j ) for ( unsigned int j = 1; j <= aligns->Size(); ++j )
{ {
const RecordVal* align = aligns->AsVectorVal()->Lookup(j)->AsRecordVal(); const RecordVal* align = aligns->AsVectorVal()->At(j)->AsRecordVal();
const BroString* str = align->GetField(0)->AsString(); const BroString* str = align->GetField(0)->AsString();
int index = align->GetField(1)->AsCount(); int index = align->GetField(1)->AsCount();
substr->AddAlignment(str, index); substr->AddAlignment(str, index);

View file

@ -1225,7 +1225,7 @@ IntrusivePtr<Val> ForStmt::DoExec(Frame* f, Val* v, stmt_flow_type& flow) const
for ( auto i = 0u; i <= vv->Size(); ++i ) for ( auto i = 0u; i <= vv->Size(); ++i )
{ {
// Skip unassigned vector indices. // Skip unassigned vector indices.
if ( ! vv->Lookup(i) ) if ( ! vv->At(i) )
continue; continue;
// Set the loop variable to the current index, and make // Set the loop variable to the current index, and make

View file

@ -632,7 +632,7 @@ static void BuildJSON(threading::formatter::JSON::NullDoubleWriter& writer, Val*
auto* vval = val->AsVectorVal(); auto* vval = val->AsVectorVal();
size_t size = vval->SizeVal()->AsCount(); size_t size = vval->SizeVal()->AsCount();
for (size_t i = 0; i < size; i++) for (size_t i = 0; i < size; i++)
BuildJSON(writer, vval->Lookup(i), only_loggable, re); BuildJSON(writer, vval->At(i).get(), only_loggable, re);
writer.EndArray(); writer.EndArray();
break; break;
@ -3132,17 +3132,19 @@ bool VectorVal::AddTo(Val* val, bool /* is_first_init */) const
auto last_idx = v->Size(); auto last_idx = v->Size();
for ( auto i = 0u; i < Size(); ++i ) for ( auto i = 0u; i < Size(); ++i )
v->Assign(last_idx++, {NewRef{}, Lookup(i)}); v->Assign(last_idx++, At(i));
return true; return true;
} }
Val* VectorVal::Lookup(unsigned int index) const const IntrusivePtr<Val>& VectorVal::At(unsigned int index) const
{ {
if ( index >= val.vector_val->size() ) static IntrusivePtr<Val> nil;
return nullptr;
return (*val.vector_val)[index].get(); if ( index >= val.vector_val->size() )
return nil;
return (*val.vector_val)[index];
} }
unsigned int VectorVal::Resize(unsigned int new_num_elements) unsigned int VectorVal::Resize(unsigned int new_num_elements)

View file

@ -1285,14 +1285,23 @@ public:
// Returns true if succcessful. // Returns true if succcessful.
bool AddTo(Val* v, bool is_first_init) const override; bool AddTo(Val* v, bool is_first_init) const override;
// Returns nil if no element was at that value. /**
// Lookup does NOT grow the vector to this size. * Returns the element at a given index or nullptr if it does not exist.
// The Val* variant assumes that the index Val* has been type-checked. * @param index The position in the vector of the element to return.
Val* Lookup(unsigned int index) const; * @return The element at the given index or nullptr if the index
* does not exist (it's greater than or equal to vector's current size).
*/
const IntrusivePtr<Val>& At(unsigned int index) const;
[[deprecated("Remove in v4.1. Use At().")]]
Val* Lookup(unsigned int index) const
{ return At(index).get(); }
[[deprecated("Remove in v4.1. Use At().")]]
Val* Lookup(Val* index) Val* Lookup(Val* index)
{ {
bro_uint_t i = index->AsListVal()->Idx(0)->CoerceToUnsigned(); bro_uint_t i = index->AsListVal()->Idx(0)->CoerceToUnsigned();
return Lookup(static_cast<unsigned int>(i)); return At(static_cast<unsigned int>(i)).get();
} }
unsigned int Size() const { return val.vector_val->size(); } unsigned int Size() const { return val.vector_val->size(); }

View file

@ -400,11 +400,11 @@ refine connection SSH_Conn += {
{ {
for ( unsigned int j = 0; j < server_list->Size(); ++j ) for ( unsigned int j = 0; j < server_list->Size(); ++j )
{ {
if ( *(client_list->Lookup(i)->AsStringVal()->AsString()) == *(server_list->Lookup(j)->AsStringVal()->AsString()) ) if ( *(client_list->At(i)->AsStringVal()->AsString()) == *(server_list->At(j)->AsStringVal()->AsString()) )
{ {
kex_algorithm_.free(); kex_algorithm_.free();
kex_algorithm_.init((const uint8 *) client_list->Lookup(i)->AsStringVal()->Bytes(), kex_algorithm_.init((const uint8 *) client_list->At(i)->AsStringVal()->Bytes(),
client_list->Lookup(i)->AsStringVal()->Len()); client_list->At(i)->AsStringVal()->Len());
// UNTESTED // UNTESTED
if ( update_kex_state_if_equal("rsa1024-sha1", KEX_RSA) ) if ( update_kex_state_if_equal("rsa1024-sha1", KEX_RSA) )

View file

@ -947,12 +947,12 @@ broker::expected<broker::data> bro_broker::val_to_data(const Val* v)
for ( auto i = 0u; i < vec->Size(); ++i ) for ( auto i = 0u; i < vec->Size(); ++i )
{ {
auto item_val = vec->Lookup(i); const auto& item_val = vec->At(i);
if ( ! item_val ) if ( ! item_val )
continue; continue;
auto item = val_to_data(item_val); auto item = val_to_data(item_val.get());
if ( ! item ) if ( ! item )
return broker::ec::invalid_data; return broker::ec::invalid_data;

View file

@ -396,7 +396,7 @@ bool Manager::PublishEvent(string topic, RecordVal* args)
for ( auto i = 0u; i < vv->Size(); ++i ) for ( auto i = 0u; i < vv->Size(); ++i )
{ {
const auto& val = vv->Lookup(i)->AsRecordVal()->GetField(0); const auto& val = vv->At(i)->AsRecordVal()->GetField(0);
auto data_val = static_cast<DataVal*>(val.get()); auto data_val = static_cast<DataVal*>(val.get());
xs.emplace_back(data_val->data); xs.emplace_back(data_val->data);
} }

View file

@ -35,13 +35,13 @@ STACK_OF(X509)* x509_get_untrusted_stack(VectorVal* certs_vec)
for ( int i = 1; i < (int) certs_vec->Size(); ++i ) // start at 1 - 0 is host cert for ( int i = 1; i < (int) certs_vec->Size(); ++i ) // start at 1 - 0 is host cert
{ {
Val *sv = certs_vec->Lookup(i); const auto& sv = certs_vec->At(i);
if ( ! sv ) if ( ! sv )
continue; continue;
// Fixme: check type // Fixme: check type
X509* x = ((file_analysis::X509Val*) sv)->GetCertificate(); X509* x = ((file_analysis::X509Val*) sv.get())->GetCertificate();
if ( ! x ) if ( ! x )
{ {
sk_X509_free(untrusted_certs); sk_X509_free(untrusted_certs);
@ -230,14 +230,14 @@ function x509_ocsp_verify%(certs: x509_opaque_vector, ocsp_reply: string, root_c
// host certificate // host certificate
unsigned int index = 0; // to prevent overloading to 0pointer unsigned int index = 0; // to prevent overloading to 0pointer
Val *sv = certs_vec->Lookup(index); const auto& sv = certs_vec->At(index);
if ( ! sv ) if ( ! sv )
{ {
builtin_error("undefined value in certificate vector"); builtin_error("undefined value in certificate vector");
return x509_result_record(-1, "undefined value in certificate vector"); return x509_result_record(-1, "undefined value in certificate vector");
} }
file_analysis::X509Val* cert_handle = (file_analysis::X509Val*) sv; file_analysis::X509Val* cert_handle = (file_analysis::X509Val*) sv.get();
X509* cert = cert_handle->GetCertificate(); X509* cert = cert_handle->GetCertificate();
if ( ! cert ) if ( ! cert )
@ -516,13 +516,13 @@ function x509_verify%(certs: x509_opaque_vector, root_certs: table_string_of_str
// host certificate // host certificate
unsigned int index = 0; // to prevent overloading to 0pointer unsigned int index = 0; // to prevent overloading to 0pointer
Val *sv = certs_vec->Lookup(index); const auto& sv = certs_vec->At(index);
if ( !sv ) if ( !sv )
{ {
builtin_error("undefined value in certificate vector"); builtin_error("undefined value in certificate vector");
return x509_result_record(-1, "undefined value in certificate vector"); return x509_result_record(-1, "undefined value in certificate vector");
} }
file_analysis::X509Val* cert_handle = (file_analysis::X509Val*) sv; file_analysis::X509Val* cert_handle = (file_analysis::X509Val*) sv.get();
X509* cert = cert_handle->GetCertificate(); X509* cert = cert_handle->GetCertificate();
if ( ! cert ) if ( ! cert )

View file

@ -1033,7 +1033,7 @@ threading::Value* Manager::ValToLogVal(Val* val, BroType* ty)
for ( int i = 0; i < lval->val.vector_val.size; i++ ) for ( int i = 0; i < lval->val.vector_val.size; i++ )
{ {
lval->val.vector_val.vals[i] = lval->val.vector_val.vals[i] =
ValToLogVal(vec->Lookup(i), ValToLogVal(vec->At(i).get(),
vec->GetType()->Yield().get()); vec->GetType()->Yield().get());
} }

View file

@ -102,7 +102,7 @@ function join_string_vec%(vec: string_vec, sep: string%): string
if ( i > 0 ) if ( i > 0 )
d.Add(sep->CheckString(), 0); d.Add(sep->CheckString(), 0);
Val* e = v->Lookup(i); const auto& e = v->At(i);
// If the element is empty, skip it. // If the element is empty, skip it.
if ( ! e ) if ( ! e )

View file

@ -1012,7 +1012,7 @@ Supervisor::NodeConfig Supervisor::NodeConfig::FromRecord(const RecordVal* node)
for ( auto i = 0u; i < scripts_val->Size(); ++i ) for ( auto i = 0u; i < scripts_val->Size(); ++i )
{ {
auto script = scripts_val->Lookup(i)->AsStringVal()->ToStdString(); auto script = scripts_val->At(i)->AsStringVal()->ToStdString();
rval.scripts.emplace_back(std::move(script)); rval.scripts.emplace_back(std::move(script));
} }

View file

@ -815,7 +815,7 @@ function paraglob_init%(v: any%) : opaque of paraglob
VectorVal* vv = v->AsVectorVal(); VectorVal* vv = v->AsVectorVal();
for ( unsigned int i = 0; i < vv->Size(); ++i ) for ( unsigned int i = 0; i < vv->Size(); ++i )
{ {
const BroString* s = vv->Lookup(i)->AsString(); const BroString* s = vv->At(i)->AsString();
patterns.push_back(std::string(reinterpret_cast<const char*>(s->Bytes()), s->Len())); patterns.push_back(std::string(reinterpret_cast<const char*>(s->Bytes()), s->Len()));
} }
@ -1291,7 +1291,7 @@ function any_set%(v: any%) : bool
VectorVal* vv = v->AsVectorVal(); VectorVal* vv = v->AsVectorVal();
for ( unsigned int i = 0; i < vv->Size(); ++i ) for ( unsigned int i = 0; i < vv->Size(); ++i )
if ( vv->Lookup(i) && vv->Lookup(i)->AsBool() ) if ( vv->At(i) && vv->At(i)->AsBool() )
return val_mgr->True(); return val_mgr->True();
return val_mgr->False(); return val_mgr->False();
@ -1320,7 +1320,7 @@ function all_set%(v: any%) : bool
VectorVal* vv = v->AsVectorVal(); VectorVal* vv = v->AsVectorVal();
for ( unsigned int i = 0; i < vv->Size(); ++i ) for ( unsigned int i = 0; i < vv->Size(); ++i )
if ( ! vv->Lookup(i) || ! vv->Lookup(i)->AsBool() ) if ( ! vv->At(i) || ! vv->At(i)->AsBool() )
return val_mgr->False(); return val_mgr->False();
return val_mgr->True(); return val_mgr->True();