refactor(userspace): reduce usage of raw pointers

Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
This commit is contained in:
Jason Dellaluce 2024-02-20 09:34:02 +00:00 committed by poiana
parent b515f0a079
commit 0ec2a6c708
6 changed files with 18 additions and 47 deletions

View File

@ -20,23 +20,6 @@ limitations under the License.
using namespace libsinsp::filter; using namespace libsinsp::filter;
bool filter_macro_resolver::run(libsinsp::filter::ast::expr*& filter)
{
m_unknown_macros.clear();
m_resolved_macros.clear();
m_errors.clear();
visitor v(m_errors, m_unknown_macros, m_resolved_macros, m_macros);
v.m_node_substitute = nullptr;
filter->accept(&v);
if (v.m_node_substitute)
{
delete filter;
filter = v.m_node_substitute.release();
}
return !m_resolved_macros.empty();
}
bool filter_macro_resolver::run(std::shared_ptr<libsinsp::filter::ast::expr>& filter) bool filter_macro_resolver::run(std::shared_ptr<libsinsp::filter::ast::expr>& filter)
{ {
m_unknown_macros.clear(); m_unknown_macros.clear();

View File

@ -40,11 +40,6 @@ class filter_macro_resolver
class and is deleted automatically. class and is deleted automatically.
\return true if at least one of the defined macros is resolved \return true if at least one of the defined macros is resolved
*/ */
bool run(libsinsp::filter::ast::expr*& filter);
/*!
\brief Version of run() that works with shared pointers
*/
bool run(std::shared_ptr<libsinsp::filter::ast::expr>& filter); bool run(std::shared_ptr<libsinsp::filter::ast::expr>& filter);
/*! /*!

View File

@ -77,45 +77,41 @@ falco_outputs::~falco_outputs()
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
this->stop_worker(); this->stop_worker();
#endif #endif
for(auto o : m_outputs)
{
delete o;
}
} }
// This function is called only at initialization-time by the constructor // This function is called only at initialization-time by the constructor
void falco_outputs::add_output(const falco::outputs::config &oc) void falco_outputs::add_output(const falco::outputs::config &oc)
{ {
falco::outputs::abstract_output *oo; std::unique_ptr<falco::outputs::abstract_output> oo;
if(oc.name == "file") if(oc.name == "file")
{ {
oo = new falco::outputs::output_file(); oo = std::make_unique<falco::outputs::output_file>();
} }
#ifndef _WIN32 #ifndef _WIN32
else if(oc.name == "program") else if(oc.name == "program")
{ {
oo = new falco::outputs::output_program(); oo = std::make_unique<falco::outputs::output_program>();
} }
#endif #endif
else if(oc.name == "stdout") else if(oc.name == "stdout")
{ {
oo = new falco::outputs::output_stdout(); oo = std::make_unique<falco::outputs::output_stdout>();
} }
#ifndef _WIN32 #ifndef _WIN32
else if(oc.name == "syslog") else if(oc.name == "syslog")
{ {
oo = new falco::outputs::output_syslog(); oo = std::make_unique<falco::outputs::output_syslog>();
} }
#endif #endif
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__) && !defined(MINIMAL_BUILD) #if !defined(_WIN32) && !defined(__EMSCRIPTEN__) && !defined(MINIMAL_BUILD)
else if(oc.name == "http") else if(oc.name == "http")
{ {
oo = new falco::outputs::output_http(); oo = std::make_unique<falco::outputs::output_http>();
} }
else if(oc.name == "grpc") else if(oc.name == "grpc")
{ {
oo = new falco::outputs::output_grpc(); oo = std::make_unique<falco::outputs::output_grpc>();
} }
#endif #endif
else else
@ -126,12 +122,11 @@ void falco_outputs::add_output(const falco::outputs::config &oc)
std::string init_err; std::string init_err;
if (oo->init(oc, m_buffered, m_hostname, m_json_output, init_err)) if (oo->init(oc, m_buffered, m_hostname, m_json_output, init_err))
{ {
m_outputs.push_back(oo); m_outputs.push_back(std::move(oo));
} }
else else
{ {
falco_logger::log(falco_logger::level::ERR, "Failed to init output: " + init_err); falco_logger::log(falco_logger::level::ERR, "Failed to init output: " + init_err);
delete(oo);
} }
} }
@ -323,12 +318,12 @@ void falco_outputs::worker() noexcept
m_queue.pop(cmsg); m_queue.pop(cmsg);
#endif #endif
for(auto o : m_outputs) for(const auto& o : m_outputs)
{ {
wd.set_timeout(timeout, o->get_name()); wd.set_timeout(timeout, o->get_name());
try try
{ {
process_msg(o, cmsg); process_msg(o.get(), cmsg);
} }
catch(const std::exception &e) catch(const std::exception &e)
{ {

View File

@ -93,7 +93,7 @@ public:
private: private:
std::unique_ptr<falco_formats> m_formats; std::unique_ptr<falco_formats> m_formats;
std::vector<falco::outputs::abstract_output *> m_outputs; std::vector<std::unique_ptr<falco::outputs::abstract_output>> m_outputs;
bool m_buffered; bool m_buffered;
bool m_json_output; bool m_json_output;

View File

@ -43,13 +43,13 @@ void falco_webserver::start(
// allocate and configure server // allocate and configure server
if (ssl_enabled) if (ssl_enabled)
{ {
m_server = new httplib::SSLServer( m_server = std::make_unique<httplib::SSLServer>(
ssl_certificate.c_str(), ssl_certificate.c_str(),
ssl_certificate.c_str()); ssl_certificate.c_str());
} }
else else
{ {
m_server = new httplib::Server(); m_server = std::make_unique<httplib::Server>();
} }
// configure server // configure server
@ -71,8 +71,7 @@ void falco_webserver::start(
// run server in a separate thread // run server in a separate thread
if (!m_server->is_valid()) if (!m_server->is_valid())
{ {
delete m_server; m_server = nullptr;
m_server = NULL;
throw falco_exception("invalid webserver configuration"); throw falco_exception("invalid webserver configuration");
} }
@ -111,7 +110,7 @@ void falco_webserver::stop()
{ {
if (m_running) if (m_running)
{ {
if (m_server != NULL) if (m_server != nullptr)
{ {
m_server->stop(); m_server->stop();
} }
@ -119,10 +118,9 @@ void falco_webserver::stop()
{ {
m_server_thread.join(); m_server_thread.join();
} }
if (m_server != NULL) if (m_server != nullptr)
{ {
delete m_server; m_server = nullptr;
m_server = NULL;
} }
m_running = false; m_running = false;
} }

View File

@ -45,6 +45,6 @@ public:
private: private:
bool m_running = false; bool m_running = false;
httplib::Server* m_server = NULL; std::unique_ptr<httplib::Server> m_server = nullptr;
std::thread m_server_thread; std::thread m_server_thread;
}; };