Skip to content

Commit

Permalink
Fix format memory leak (draios#694)
Browse files Browse the repository at this point in the history
* Whitespace diffs.

Committing separate from other changes.

* Fix leak when fmt string ends with non-filtercheck

Make sure that any final rawstring_check added to the list of tokens is
also added to m_chks_to_free, so it is properly freed.

This fixes draios#693.
  • Loading branch information
mstemm authored and Damian Myerscough committed Mar 3, 2017
1 parent 061169b commit 82f2ca2
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions userspace/libsinsp/eventformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ void sinsp_evt_formatter::set_format(const string& fmt)
}
}

sinsp_filter_check* chk = g_filterlist.new_filter_check_from_fldname(string(cfmt + j + 1),
m_inspector,
sinsp_filter_check* chk = g_filterlist.new_filter_check_from_fldname(string(cfmt + j + 1),
m_inspector,
false);

if(chk == NULL)
Expand All @@ -151,7 +151,9 @@ void sinsp_evt_formatter::set_format(const string& fmt)

if(last_nontoken_str_start != j)
{
m_tokens.push_back(new rawstring_check(lfmt.substr(last_nontoken_str_start, j - last_nontoken_str_start)));
sinsp_filter_check * chk = new rawstring_check(lfmt.substr(last_nontoken_str_start, j - last_nontoken_str_start));
m_tokens.push_back(chk);
m_chks_to_free.push_back(chk);
m_tokenlens.push_back(0);
}
}
Expand Down Expand Up @@ -206,12 +208,12 @@ bool sinsp_evt_formatter::tostring(sinsp_evt* evt, OUT string* res)

fi = m_tokens[j]->get_field_info();

if(fi)
if(fi)
{
m_root[fi->m_name] = m_tokens[j]->tojson(evt);
}
}
else
}
}
else
{
char* str = m_tokens[j]->tostring(evt);

Expand All @@ -220,14 +222,14 @@ bool sinsp_evt_formatter::tostring(sinsp_evt* evt, OUT string* res)
continue;
}

if(str == NULL)
if(str == NULL)
{
if(m_require_all_values)
{
retval = false;
continue;
}
else
else
{
str = (char*)"<NA>";
}
Expand All @@ -254,13 +256,13 @@ bool sinsp_evt_formatter::tostring(sinsp_evt* evt, OUT string* res)
|| m_inspector->get_buffer_format() == sinsp_evt::PF_JSONHEXASCII
|| m_inspector->get_buffer_format() == sinsp_evt::PF_JSONBASE64)
{
if(m_first)
if(m_first)
{
// Give it the opening stanza of a JSON array
(*res) = '[';
m_first = false;
}
else
}
else
{
// Otherwise say this is another object in an
// existing JSON array
Expand Down

0 comments on commit 82f2ca2

Please sign in to comment.