etr/libhttpserver

[BUG] Segfault at runtime when passing nullptr on resource register

LeSpocky opened this issue · 0 comments

Prerequisites

Description

When passing a nullptr as second argument (http_resource* hrm) to webserver::register_resource() the application will segfault at runtime.

Steps to Reproduce

  1. declare a std::unique_ptr to a class derived from httpserver::http_resource
  2. forget to reset/initialize that ptr
  3. pass it to httpserver::webserver::register_resource() the usual way
  4. send a request to the webserver on that resource

Expected behavior: not sure, maybe some kind of parameter check by httpserver::webserver::register_resource()

Actual behavior: the webserver crashes with segfault at runtime

Reproduces how often: always

Versions

  • OS version: Debian GNU/Linux 10 (buster)
  • libhttpserver version: master
  • libmicrohttpd version: unknown

Additional Information

class file_response_resource : public httpserver::http_resource {
public:
	file_response_resource( std::string filename ) : filename(filename) {
		printf( "file_response resource created with filename: %s",
					 filename.c_str() );
	};

	const std::shared_ptr<httpserver::http_response> render_GET(
			const httpserver::http_request& );
private:
	std::string filename;
};

const std::shared_ptr<httpserver::http_response> file_response_resource::render_GET(
	const httpserver::http_request& )
{
	return std::shared_ptr<httpserver::file_response>(
			new httpserver::file_response(filename, 200, "text/plain") );
}

int main( int argc, char *argv[] )
{
	httpserver::webserver ws = httpserver::create_webserver(8080);
	std::unique_ptr<file_response_resource> frr;

	// forget to init frr here
	// frr.reset(new file_response_resource(argv[1]));

	// accidentally pass a nullptr here
	ws.register_resource("/file", frr.get(), true);
	ws.start(true);

	return 0;
}