Skip to content

Commit

Permalink
Prevent rule_result from leaking on error.
Browse files Browse the repository at this point in the history
Change falco_engine::process_event to return a unique_ptr that wraps the
rule result, so it won't be leaked if this method throws an exception.

This means that callers don't need to create their own.
  • Loading branch information
mstemm committed Dec 1, 2016
1 parent ded3ee5 commit b3c691e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
8 changes: 4 additions & 4 deletions userspace/engine/falco_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ void falco_engine::enable_rule(string &pattern, bool enabled)
m_evttype_filter.enable(pattern, enabled);
}

falco_engine::rule_result *falco_engine::process_event(sinsp_evt *ev)
unique_ptr<falco_engine::rule_result> falco_engine::process_event(sinsp_evt *ev)
{

if(should_drop_evt())
{
return NULL;
return unique_ptr<struct rule_result>();
}

if(!m_evttype_filter.run(ev))
{
return NULL;
return unique_ptr<struct rule_result>();
}

struct rule_result *res = new rule_result();
unique_ptr<struct rule_result> res(new rule_result());

lua_getglobal(m_ls, lua_on_event.c_str());

Expand Down
2 changes: 1 addition & 1 deletion userspace/engine/falco_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class falco_engine : public falco_common
// the rule that matched. If no rule matched, returns NULL.
//
// the reutrned rule_result is allocated and must be delete()d.
rule_result *process_event(sinsp_evt *ev);
std::unique_ptr<rule_result> process_event(sinsp_evt *ev);

//
// Print details on the given rule. If rule is NULL, print
Expand Down
3 changes: 1 addition & 2 deletions userspace/falco/falco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,10 @@ uint64_t do_inspect(falco_engine *engine,
// engine, which will match the event against the set
// of rules. If a match is found, pass the event to
// the outputs.
falco_engine::rule_result *res = engine->process_event(ev);
unique_ptr<falco_engine::rule_result> res = engine->process_event(ev);
if(res)
{
outputs->handle_event(res->evt, res->rule, res->priority, res->format);
delete(res);
}

num_evts++;
Expand Down

0 comments on commit b3c691e

Please sign in to comment.