GH-1558: Fix reading vector of enum types from config files

This commit is contained in:
Jon Siwek 2021-05-14 16:51:26 -07:00
parent e35888a994
commit 45b2d3b4f2
4 changed files with 35 additions and 1 deletions

View file

@ -575,7 +575,8 @@ Val* Value::ValueToVal(const std::string& source, const Value* val, bool& have_e
{
TypePtr type;
if ( val->val.vector_val.size == 0 && val->subtype == TYPE_VOID )
if ( val->val.vector_val.size == 0 &&
( val->subtype == TYPE_VOID || val->subtype == TYPE_ENUM ) )
// don't know type - unspecified table.
type = base_type(TYPE_ANY);
else
@ -583,6 +584,24 @@ Val* Value::ValueToVal(const std::string& source, const Value* val, bool& have_e
// all entries have to have the same type...
if ( val->subtype == TYPE_VOID )
type = base_type(val->val.vector_val.vals[0]->type);
else if ( val->subtype == TYPE_ENUM )
{
// Enums are not a base-type, so need to look it up.
const auto& sv = val->val.vector_val.vals[0]->val.string_val;
std::string enum_name(sv.data, sv.length);
const auto& enum_id = detail::global_scope()->Find(enum_name);
if ( ! enum_id )
{
reporter->Warning("Value '%s' of source '%s' is not a valid enum.",
enum_name.data(), source.c_str());
have_error = true;
return nullptr;
}
type = enum_id->GetType();
}
else
type = base_type(val->subtype);
}

View file

@ -1,3 +1,5 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
warning: Value 'asdf' for source 'thread ../configfile/Input::READER_CONFIG' is not a valid enum.
error: SendEvent for event InputConfig::new_value failed
warning: Value '1234' for source 'thread ../configfile/Input::READER_CONFIG' is not a valid enum.
error: SendEvent for event InputConfig::new_value failed

View file

@ -6,3 +6,6 @@ Green
{
}
[Green]
[Red]
[]

View file

@ -6,6 +6,9 @@
@TEST-START-FILE configfile
mycolors Red,asdf,Blue
nocolors
color_vec Green
bad_color_vec Green,1234,Blue
no_color_vec
@TEST-END-FILE
@load base/frameworks/config
@ -15,6 +18,10 @@ type Color: enum { Red, Green, Blue, };
option mycolors = set(Red, Green);
option nocolors = set(Red, Green);
option color_vec: vector of Color = { Red };
option bad_color_vec: vector of Color = { Red };
option no_color_vec: vector of Color = { Red };
event zeek_init()
{ Config::read_config("../configfile"); }
@ -22,5 +29,8 @@ event Input::end_of_data(name: string, source:string)
{
print mycolors;
print nocolors;
print color_vec;
print bad_color_vec;
print no_color_vec;
terminate();
}