Skip to content

Commit

Permalink
fix(falco): prevent use-after-return in webserver
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Guerra <[email protected]>
  • Loading branch information
LucaGuerra authored and poiana committed Dec 19, 2024
1 parent 129087a commit 1239566
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
11 changes: 5 additions & 6 deletions userspace/falco/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,25 @@ void falco_webserver::start(const falco::app::state &state,
throw falco_exception("invalid webserver configuration");
}

std::atomic<bool> failed;
failed.store(false, std::memory_order_release);
m_server_thread = std::thread([this, webserver_config, &failed] {
m_failed.store(false, std::memory_order_release);
m_server_thread = std::thread([this, webserver_config] {
try {
this->m_server->listen(webserver_config.m_listen_address,
webserver_config.m_listen_port);
} catch(std::exception &e) {
falco_logger::log(falco_logger::level::ERR,
"falco_webserver: " + std::string(e.what()) + "\n");
}
failed.store(true, std::memory_order_release);
this->m_failed.store(true, std::memory_order_release);
});

// wait for the server to actually start up
// note: is_running() is atomic
while(!m_server->is_running() && !failed.load(std::memory_order_acquire)) {
while(!m_server->is_running() && !m_failed.load(std::memory_order_acquire)) {
std::this_thread::yield();
}
m_running = true;
if(failed.load(std::memory_order_acquire)) {
if(m_failed.load(std::memory_order_acquire)) {
stop();
throw falco_exception("an error occurred while starting webserver");
}
Expand Down
1 change: 1 addition & 0 deletions userspace/falco/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ class falco_webserver {
bool m_running = false;
std::unique_ptr<httplib::Server> m_server = nullptr;
std::thread m_server_thread;
std::atomic<bool> m_failed;
};

0 comments on commit 1239566

Please sign in to comment.