Fix similar issues with ValueTo* methods in the input framework

This commit is contained in:
Tim Wojtulewicz 2021-04-07 10:59:56 -07:00
parent d6116b0141
commit 04c201393f
4 changed files with 87 additions and 12 deletions

View file

@ -1059,19 +1059,33 @@ Val* Manager::ValueToIndexVal(const Stream* i, int num_fields, const RecordType
}
else
{
auto* l = new ListVal(TYPE_ANY);
auto l = make_intrusive<ListVal>(TYPE_ANY);
for ( int j = 0 ; j < type->NumFields(); j++ )
{
if ( type->GetFieldType(j)->Tag() == TYPE_RECORD )
l->Append({AdoptRef{}, ValueToRecordVal(i, vals,
type->GetFieldType(j)->AsRecordType(), &position, have_error)});
{
auto rv = ValueToRecordVal(i, vals, type->GetFieldType(j)->AsRecordType(),
&position, have_error);
if ( have_error )
break;
l->Append({AdoptRef{}, rv});
}
else
{
l->Append({AdoptRef{}, ValueToVal(i, vals[position], type->GetFieldType(j).get(), have_error)});
auto v = ValueToVal(i, vals[position], type->GetFieldType(j).get(), have_error);
if ( have_error )
break;
l->Append({AdoptRef{}, v});
position++;
}
}
idxval = l;
if ( have_error )
return nullptr;
idxval = l.release();
}
assert ( position == num_fields );
@ -1500,7 +1514,9 @@ int Manager::SendEventStreamEvent(Stream* i, EnumVal* type, const Value* const *
if ( stream->want_record )
{
RecordVal * r = ValueToRecordVal(i, vals, stream->fields, &position, convert_error);
RecordVal* r = ValueToRecordVal(i, vals, stream->fields, &position, convert_error);
if ( ! convert_error )
out_vals.push_back(r);
}
@ -1521,6 +1537,9 @@ int Manager::SendEventStreamEvent(Stream* i, EnumVal* type, const Value* const *
position++;
}
if ( convert_error)
break;
out_vals.push_back(val);
}
}
@ -1883,7 +1902,7 @@ RecordVal* Manager::ValueToRecordVal(const Stream* stream, const Value* const *v
{
assert(position != nullptr); // we need the pointer to point to data.
auto* rec = new RecordVal({NewRef{}, request_type});
auto rec = make_intrusive<RecordVal>(RecordTypePtr{NewRef{}, request_type});
for ( int i = 0; i < request_type->NumFields(); i++ )
{
Val* fieldVal = nullptr;
@ -1907,11 +1926,13 @@ RecordVal* Manager::ValueToRecordVal(const Stream* stream, const Value* const *v
(*position)++;
}
if ( fieldVal )
if ( have_error )
return nullptr;
else if ( fieldVal )
rec->Assign(i, {AdoptRef{}, fieldVal});
}
return rec;
return rec.release();
}
// Count the length of the values used to create a correct length buffer for
@ -2287,15 +2308,18 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, Type* request_type,
auto set_index = make_intrusive<TypeList>(type);
set_index->Append(type);
auto s = make_intrusive<SetType>(std::move(set_index), nullptr);
auto* t = new TableVal(std::move(s));
auto t = make_intrusive<TableVal>(std::move(s));
for ( int j = 0; j < val->val.set_val.size; j++ )
{
Val* assignval = ValueToVal(i, val->val.set_val.vals[j], type.get(), have_error);
if ( have_error )
return nullptr;
t->Assign({AdoptRef{}, assignval}, nullptr);
}
return t;
return t.release();
}
case TYPE_VECTOR:
@ -2308,6 +2332,10 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, Type* request_type,
for ( int j = 0; j < val->val.vector_val.size; j++ )
{
auto el = ValueToVal(i, val->val.vector_val.vals[j], type.get(), have_error);
if ( have_error )
return nullptr;
v->Assign(j, {AdoptRef{}, el});
}

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
warning: Value 'asdf' for stream 'denylist' is not a valid enum.

View file

@ -0,0 +1,10 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
{
[192.168.17.1] = [colors={
Red,
White
}],
[192.168.250.3] = [colors={
Blue
}]
}

View file

@ -0,0 +1,35 @@
# @TEST-EXEC: btest-bg-run zeek zeek -b %INPUT
# @TEST-EXEC: btest-bg-wait 10
# @TEST-EXEC: btest-diff zeek/.stderr
# @TEST-EXEC: btest-diff zeek/.stdout
@TEST-START-FILE denylist.txt
#separator \x09
#fields ip colors
192.168.17.1 Red,White
192.168.27.2 White,asdf
192.168.250.3 Blue
@TEST-END-FILE
# test.zeek
type Idx: record {
ip: addr;
};
type Color: enum { Red, White, Blue, };
type Val: record {
colors: set[Color];
};
global denylist: table[addr] of Val = table();
event zeek_init() {
Input::add_table([$source="../denylist.txt", $name="denylist",
$idx=Idx, $val=Val, $destination=denylist]);
Input::remove("denylist");
}
event Input::end_of_data(name: string, source: string) {
print denylist;
}