Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(userspace/libsinsp)!: reduce usage of raw pointers #1702

Merged
merged 10 commits into from
Feb 22, 2024
Merged
2 changes: 1 addition & 1 deletion test/libsinsp_e2e/sys_call_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ TEST_F(sys_call_test32, execve_ia32_emulation)
{
sinsp_filter_compiler compiler(inspector,
"evt.type=execve and proc.apid=" + std::to_string(getpid()));
is_subprocess_execve.reset(compiler.compile());
is_subprocess_execve = compiler.compile();
};

event_filter_t filter = [&](sinsp_evt* evt) { return is_subprocess_execve->run(evt); };
Expand Down
2 changes: 1 addition & 1 deletion userspace/chisel/chisel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void chiselinfo::set_filter(string filterstr)

if(filterstr != "")
{
m_filter = compiler.compile();
m_filter = compiler.compile().release();
}
}

Expand Down
15 changes: 5 additions & 10 deletions userspace/chisel/chisel_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ int lua_cbacks::request_field(lua_State *ls)
throw sinsp_exception("chisel error");
}

sinsp_filter_check* chk = s_filterlist.new_filter_check_from_fldname(fld,
auto chk = s_filterlist.new_filter_check_from_fldname(fld,
inspector,
false);
false).release();

if(chk == NULL)
{
Expand Down Expand Up @@ -710,8 +710,8 @@ int lua_cbacks::get_machine_info(lua_State *ls)
int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool barebone)
{
uint32_t j;
sinsp_filter_compiler* compiler = NULL;
sinsp_filter* filter = NULL;
std::unique_ptr<sinsp_filter_compiler> compiler;
std::unique_ptr<sinsp_filter> filter;
sinsp_evt tevt;
scap_evt tscapevt;

Expand Down Expand Up @@ -739,7 +739,7 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
{
try
{
compiler = new sinsp_filter_compiler(ch->m_inspector, filterstr);
compiler = std::make_unique<sinsp_filter_compiler>(ch->m_inspector, filterstr);
filter = compiler->compile();
}
catch(const sinsp_exception& e)
Expand Down Expand Up @@ -1079,11 +1079,6 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
return true;
});

if(filter)
{
delete filter;
}

return 1;
}

Expand Down
10 changes: 5 additions & 5 deletions userspace/chisel/chisel_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void chisel_table::configure(vector<chisel_view_column_info>* entries, const str
if(filter != "")
{
sinsp_filter_compiler compiler(m_inspector, filter);
m_filter = compiler.compile();
m_filter = compiler.compile().release();
}

//////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -167,9 +167,9 @@ void chisel_table::configure(vector<chisel_view_column_info>* entries, const str

for(auto vit : *entries)
{
sinsp_filter_check* chk = s_filterlist.new_filter_check_from_fldname(vit.get_field(m_view_depth),
auto chk = s_filterlist.new_filter_check_from_fldname(vit.get_field(m_view_depth),
m_inspector,
false);
false).release();

if(chk == NULL)
{
Expand Down Expand Up @@ -209,9 +209,9 @@ void chisel_table::configure(vector<chisel_view_column_info>* entries, const str
}
else
{
sinsp_filter_check* chk = s_filterlist.new_filter_check_from_fldname("util.cnt",
auto chk = s_filterlist.new_filter_check_from_fldname("util.cnt",
m_inspector,
false);
false).release();

if(chk == NULL)
{
Expand Down
2 changes: 2 additions & 0 deletions userspace/libsinsp/container_engine/container_engine_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.

#pragma once

#include <memory>

#include <libsinsp/container_engine/container_cache_interface.h>

class sinsp_threadinfo;
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/container_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by

std::shared_ptr<sinsp_threadinfo> sinsp_container_info::get_tinfo(sinsp* inspector) const
{
std::shared_ptr<sinsp_threadinfo> tinfo(inspector->build_threadinfo());
std::shared_ptr<sinsp_threadinfo> tinfo(inspector->build_threadinfo().release());
Molter73 marked this conversation as resolved.
Show resolved Hide resolved
tinfo->m_tid = -1;
tinfo->m_pid = -1;
tinfo->m_vtid = -2;
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/dns_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ limitations under the License.
#include <chrono>
#include <future>
#include <mutex>
#include <memory>
#if !defined(__EMSCRIPTEN__)
#include "tbb/concurrent_unordered_map.h"
#endif
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include <optional>
#include <unordered_map>
#include <string_view>
#include <memory>

#include <json/json.h>

Expand Down
32 changes: 11 additions & 21 deletions userspace/libsinsp/eventformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,6 @@ sinsp_evt_formatter::sinsp_evt_formatter(sinsp* inspector,
set_format(of, fmt);
}

sinsp_evt_formatter::~sinsp_evt_formatter()
{
uint32_t j;

for(j = 0; j < m_chks_to_free.size(); j++)
{
delete m_chks_to_free[j];
}
}

void sinsp_evt_formatter::set_format(output_format of, const std::string& fmt)
{
uint32_t j;
Expand Down Expand Up @@ -106,10 +96,10 @@ void sinsp_evt_formatter::set_format(output_format of, const std::string& fmt)

if(last_nontoken_str_start != j)
{
rawstring_check* newtkn = new rawstring_check(lfmt.substr(last_nontoken_str_start, j - last_nontoken_str_start));
m_tokens.emplace_back(std::make_pair("", newtkn));
auto newtkn = std::make_unique<rawstring_check>(lfmt.substr(last_nontoken_str_start, j - last_nontoken_str_start));
m_tokens.emplace_back(std::make_pair("", newtkn.get()));
m_tokenlens.push_back(0);
m_chks_to_free.push_back(newtkn);
m_checks.push_back(std::move(newtkn));
}

if(j == lfmtlen - 1)
Expand Down Expand Up @@ -148,35 +138,35 @@ void sinsp_evt_formatter::set_format(output_format of, const std::string& fmt)
}
}

sinsp_filter_check* chk = m_available_checks.new_filter_check_from_fldname(std::string(cfmt + j + 1),
auto chk = m_available_checks.new_filter_check_from_fldname(std::string(cfmt + j + 1),
m_inspector,
false);

if(chk == NULL)
if(chk == nullptr)
{
throw sinsp_exception("invalid formatting token " + std::string(cfmt + j + 1));
}

m_chks_to_free.push_back(chk);

const char * fstart = cfmt + j + 1;
uint32_t fsize = chk->parse_field_name(fstart, true, false);

j += fsize;
ASSERT(j <= lfmt.length());

m_tokens.emplace_back(std::make_pair(std::string(fstart, fsize), chk));
m_tokens.emplace_back(std::make_pair(std::string(fstart, fsize), chk.get()));
m_tokenlens.push_back(toklen);

m_checks.push_back(std::move(chk));

last_nontoken_str_start = j + 1;
}
}

if(last_nontoken_str_start != j)
{
sinsp_filter_check * chk = new rawstring_check(lfmt.substr(last_nontoken_str_start, j - last_nontoken_str_start));
m_tokens.emplace_back(std::make_pair("", chk));
m_chks_to_free.push_back(chk);
auto chk = std::make_unique<rawstring_check>(lfmt.substr(last_nontoken_str_start, j - last_nontoken_str_start));
m_tokens.emplace_back(std::make_pair("", chk.get()));
m_checks.emplace_back(std::move(chk));
m_tokenlens.push_back(0);
}
}
Expand Down
5 changes: 3 additions & 2 deletions userspace/libsinsp/eventformatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#include <map>
#include <utility>
#include <string>
#include <memory>
#include <json/json.h>

#include <libsinsp/filter_check_list.h>
Expand Down Expand Up @@ -55,7 +56,7 @@ class SINSP_PUBLIC sinsp_evt_formatter

sinsp_evt_formatter(sinsp* inspector, const std::string& fmt, filter_check_list &available_checks);

virtual ~sinsp_evt_formatter();
virtual ~sinsp_evt_formatter() = default;

/*!
\brief Resolve all the formatted tokens and return them in a key/value
Expand Down Expand Up @@ -119,7 +120,7 @@ class SINSP_PUBLIC sinsp_evt_formatter
sinsp* m_inspector;
filter_check_list &m_available_checks;
bool m_require_all_values;
std::vector<sinsp_filter_check*> m_chks_to_free;
std::vector<std::unique_ptr<sinsp_filter_check>> m_checks;

Json::Value m_root;
Json::FastWriter m_writer;
Expand Down
3 changes: 3 additions & 0 deletions userspace/libsinsp/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
# limitations under the License.
#

include(jsoncpp)

add_executable(sinsp-example
util.cpp
test.cpp
)

target_link_libraries(sinsp-example
sinsp
"${JSONCPP_LIB}"
)

if (EMSCRIPTEN)
Expand Down
Loading
Loading