Skip to content

Commit cbf6bfd

Browse files
authored
Fix segfault when passing nullptr to register_resource (issue #265) (#353)
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.
1 parent 54c800e commit cbf6bfd

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/webserver.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ void webserver::request_completed(void *cls, struct MHD_Connection *connection,
191191
}
192192

193193
bool webserver::register_resource(const std::string& resource, http_resource* hrm, bool family) {
194+
if (hrm == nullptr) {
195+
throw std::invalid_argument("The http_resource pointer cannot be null");
196+
}
197+
194198
if (single_resource && ((resource != "" && resource != "/") || !family)) {
195199
throw std::invalid_argument("The resource should be '' or '/' and be marked as family when using a single_resource server");
196200
}

test/integ/ws_start_stop.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource_not_default_resource)
372372
ws.stop();
373373
LT_END_AUTO_TEST(single_resource_not_default_resource)
374374

375+
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, register_resource_nullptr_throws)
376+
httpserver::webserver ws = httpserver::create_webserver(PORT);
377+
LT_CHECK_THROW(ws.register_resource("/test", nullptr));
378+
LT_END_AUTO_TEST(register_resource_nullptr_throws)
379+
375380
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, thread_per_connection_fails_with_max_threads)
376381
{ // NOLINT (internal scope opening - not method start)
377382
httpserver::webserver ws = httpserver::create_webserver(PORT)

0 commit comments

Comments
 (0)