Skip to content

Commit

Permalink
fix(userspace/libsinsp): check operator compatibility with list field…
Browse files Browse the repository at this point in the history
… types

Signed-off-by: Jason Dellaluce <[email protected]>
  • Loading branch information
jasondellaluce committed May 10, 2024
1 parent 3993021 commit d0a8d67
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion userspace/libsinsp/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ static inline void check_op_type_compatibility(sinsp_filter_check& c)
{
std::string err;
auto fi = c.get_transformed_field_info();
if (fi && !flt_is_comparable(c.m_cmpop, fi->m_type, err))
if (fi && !flt_is_comparable(c.m_cmpop, fi->m_type, fi->is_list(), err))
{
throw sinsp_exception("filter error: " + err);
}
Expand Down
51 changes: 45 additions & 6 deletions userspace/libsinsp/filter_compare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ std::string std::to_string(cmpop c)
}
};

static bool flt_is_comparable_numeric(cmpop op, std::string& err)
static inline bool flt_is_comparable_numeric(cmpop op, std::string& err)
{
switch(op)
{
Expand All @@ -226,7 +226,7 @@ static bool flt_is_comparable_numeric(cmpop op, std::string& err)
}
}

static bool flt_is_comparable_bool(cmpop op, std::string& err)
static inline bool flt_is_comparable_bool(cmpop op, std::string& err)
{
switch(op)
{
Expand All @@ -245,7 +245,7 @@ static bool flt_is_comparable_bool(cmpop op, std::string& err)
}
}

static bool flt_is_comparable_string(cmpop op, std::string& err)
static inline bool flt_is_comparable_string(cmpop op, std::string& err)
{
switch(op)
{
Expand Down Expand Up @@ -275,7 +275,7 @@ static bool flt_is_comparable_string(cmpop op, std::string& err)
}
}

static bool flt_is_comparable_buffer(cmpop op, std::string& err)
static inline bool flt_is_comparable_buffer(cmpop op, std::string& err)
{
switch(op)
{
Expand All @@ -299,7 +299,7 @@ static bool flt_is_comparable_buffer(cmpop op, std::string& err)
}
}

static bool flt_is_comparable_ip_or_net(cmpop op, std::string& err)
static inline bool flt_is_comparable_ip_or_net(cmpop op, std::string& err)
{
switch(op)
{
Expand All @@ -318,13 +318,48 @@ static bool flt_is_comparable_ip_or_net(cmpop op, std::string& err)
}
}

bool flt_is_comparable(cmpop op, ppm_param_type t, std::string& err)
static inline bool flt_is_comparable_any_list(cmpop op, std::string& err)
{
switch(op)
{
case CO_IN:
case CO_EXISTS:
case CO_INTERSECTS:
return true;
default:
ASSERT(false);
std::string opname;
cmpop_to_str(op, opname);
err = "'" + opname + "' operator not supported list filters";
return false;
}
}

bool flt_is_comparable(cmpop op, ppm_param_type t, bool is_list, std::string& err)
{
if(op == CO_EXISTS)
{
return true;
}

if (is_list)
{
switch (t)
{
case PT_CHARBUF:
case PT_UINT64:
case PT_RELTIME:
case PT_ABSTIME:
case PT_BOOL:
case PT_IPADDR:
case PT_IPNET:
return flt_is_comparable_any_list(op, err);
default:
err = "list filters are not supported for type '" + std::string(param_type_to_string(t)) + "'";
return false;
}
}

switch(t)
{
case PT_INT8:
Expand Down Expand Up @@ -368,6 +403,10 @@ bool flt_is_comparable(cmpop op, ppm_param_type t, std::string& err)
case PT_BYTEBUF:
return flt_is_comparable_buffer(op, err);
default:
ASSERT(false);
std::string opname;
cmpop_to_str(op, opname);
err = "'" + opname + "' operator not supported for type '" + std::string(param_type_to_string(t)) + "'";
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/filter_compare.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace std
std::string to_string(cmpop);
}

bool flt_is_comparable(cmpop op, ppm_param_type t, std::string& err);
bool flt_is_comparable(cmpop op, ppm_param_type t, bool is_list, std::string& err);
bool flt_compare(cmpop op, ppm_param_type type, const void* operand1, const void* operand2, uint32_t op1_len = 0, uint32_t op2_len = 0);
bool flt_compare_avg(cmpop op, ppm_param_type type, const void* operand1, const void* operand2, uint32_t op1_len, uint32_t op2_len, uint32_t cnt1, uint32_t cnt2);
bool flt_compare_ipv4net(cmpop op, uint64_t operand1, const ipv4net* operand2);
Expand Down

0 comments on commit d0a8d67

Please sign in to comment.