[BUG] Segfault at runtime when passing nullptr on resource register
LeSpocky opened this issue · 0 comments
LeSpocky commented
Prerequisites
- Put an X between the brackets on this line if you have checked that your issue isn't already filed: https://github.com/search?l=&q=repo%3Aetr%2Flibhttpserver&type=Issues
Description
When passing a nullptr as second argument (http_resource* hrm) to webserver::register_resource() the application will segfault at runtime.
Steps to Reproduce
- declare a
std::unique_ptrto a class derived fromhttpserver::http_resource - forget to reset/initialize that ptr
- pass it to
httpserver::webserver::register_resource()the usual way - 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;
}