From 4063d4bc13c47fba25753f913e32d9231c7824c5 Mon Sep 17 00:00:00 2001 From: Sebastiano Merlino Date: Tue, 27 Jan 2026 20:22:11 -0800 Subject: [PATCH] Fix segfault when passing nullptr to register_resource (issue #265) Add validation to reject null http_resource pointers in webserver::register_resource(), throwing std::invalid_argument instead of allowing a segfault at runtime when the resource is accessed. --- src/webserver.cpp | 4 ++++ test/integ/ws_start_stop.cpp | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/webserver.cpp b/src/webserver.cpp index d963c290..38e5719d 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -191,6 +191,10 @@ void webserver::request_completed(void *cls, struct MHD_Connection *connection, } bool webserver::register_resource(const std::string& resource, http_resource* hrm, bool family) { + if (hrm == nullptr) { + throw std::invalid_argument("The http_resource pointer cannot be null"); + } + if (single_resource && ((resource != "" && resource != "/") || !family)) { throw std::invalid_argument("The resource should be '' or '/' and be marked as family when using a single_resource server"); } diff --git a/test/integ/ws_start_stop.cpp b/test/integ/ws_start_stop.cpp index 6e717196..8a6d999c 100644 --- a/test/integ/ws_start_stop.cpp +++ b/test/integ/ws_start_stop.cpp @@ -372,6 +372,11 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource_not_default_resource) ws.stop(); LT_END_AUTO_TEST(single_resource_not_default_resource) +LT_BEGIN_AUTO_TEST(ws_start_stop_suite, register_resource_nullptr_throws) + httpserver::webserver ws = httpserver::create_webserver(PORT); + LT_CHECK_THROW(ws.register_resource("/test", nullptr)); +LT_END_AUTO_TEST(register_resource_nullptr_throws) + LT_BEGIN_AUTO_TEST(ws_start_stop_suite, thread_per_connection_fails_with_max_threads) { // NOLINT (internal scope opening - not method start) httpserver::webserver ws = httpserver::create_webserver(PORT)