From 7c51d8a83afaba8ad5c728e90cde57b915f5baf4 Mon Sep 17 00:00:00 2001 From: Alexander Dahl Date: Thu, 18 Nov 2021 22:11:18 +0100 Subject: [PATCH] Use keyword nullptr where possible Replace every usage of 0x0 or NULL with nullptr, which is available since C++11. NULL, or 0x0, or nullptr, or even 0 was used over the entiry code base before for expressing the concept of a null pointer. Use the C++ special word nullptr instead to unify the code base and minimize friction with expectations of modern C++ developers. References: #241 Signed-off-by: Alexander Dahl --- examples/service.cpp | 2 +- src/deferred_response.cpp | 2 +- src/file_response.cpp | 2 +- src/http_request.cpp | 26 ++++++++-------- src/http_response.cpp | 2 +- src/http_utils.cpp | 10 +++---- src/httpserver/create_webserver.hpp | 16 +++++----- src/httpserver/details/modded_request.hpp | 12 ++++---- src/httpserver/http_request.hpp | 4 +-- src/webserver.cpp | 36 +++++++++++------------ test/integ/basic.cpp | 14 ++++----- test/integ/ws_start_stop.cpp | 4 +-- test/unit/http_utils_test.cpp | 4 +-- 13 files changed, 67 insertions(+), 67 deletions(-) diff --git a/examples/service.cpp b/examples/service.cpp index 8880d96a..3f8ccb03 100644 --- a/examples/service.cpp +++ b/examples/service.cpp @@ -158,7 +158,7 @@ int main(int argc, char **argv) { while ((c = getopt(argc, argv, "p:k:c:sv?")) != EOF) { switch (c) { case 'p': - port = strtoul(optarg, NULL, 10); + port = strtoul(optarg, nullptr, 10); break; case 'k': key = optarg; diff --git a/src/deferred_response.cpp b/src/deferred_response.cpp index fa42134f..f2764810 100644 --- a/src/deferred_response.cpp +++ b/src/deferred_response.cpp @@ -29,7 +29,7 @@ namespace httpserver { namespace details { MHD_Response* get_raw_response_helper(void* cls, ssize_t (*cb)(void*, uint64_t, char*, size_t)) { - return MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 1024, cb, cls, NULL); + return MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 1024, cb, cls, nullptr); } } // namespace details diff --git a/src/file_response.cpp b/src/file_response.cpp index 623c7036..3bfdcec3 100644 --- a/src/file_response.cpp +++ b/src/file_response.cpp @@ -35,7 +35,7 @@ MHD_Response* file_response::get_raw_response() { if (size) { return MHD_create_response_from_fd(size, fd); } else { - return MHD_create_response_from_buffer(0, 0x0, MHD_RESPMEM_PERSISTENT); + return MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT); } } diff --git a/src/http_request.cpp b/src/http_request.cpp index 5f29e0cc..4c3f0c82 100644 --- a/src/http_request.cpp +++ b/src/http_request.cpp @@ -58,7 +58,7 @@ bool http_request::check_digest_auth(const std::string& realm, const std::string const std::string http_request::get_connection_value(const std::string& key, enum MHD_ValueKind kind) const { const char* header_c = MHD_lookup_connection_value(underlying_connection, kind, key.c_str()); - if (header_c == NULL) return EMPTY; + if (header_c == nullptr) return EMPTY; return header_c; } @@ -140,7 +140,7 @@ MHD_Result http_request::build_request_args(void *cls, enum MHD_ValueKind kind, std::ignore = kind; arguments_accumulator* aa = static_cast(cls); - std::string value = ((arg_value == NULL) ? "" : arg_value); + std::string value = ((arg_value == nullptr) ? "" : arg_value); http::base_unescaper(&value, aa->unescaper); (*aa->arguments)[key] = value; @@ -152,7 +152,7 @@ MHD_Result http_request::build_request_querystring(void *cls, enum MHD_ValueKind std::ignore = kind; std::string* querystring = static_cast(cls); - std::string value = ((arg_value == NULL) ? "" : arg_value); + std::string value = ((arg_value == nullptr) ? "" : arg_value); int buffer_size = std::string(key).size() + value.size() + 3; char* buf = new char[buffer_size]; @@ -170,14 +170,14 @@ MHD_Result http_request::build_request_querystring(void *cls, enum MHD_ValueKind } const std::string http_request::get_user() const { - char* username = 0x0; - char* password = 0x0; + char* username = nullptr; + char* password = nullptr; username = MHD_basic_auth_get_username_password(underlying_connection, &password); - if (password != 0x0) free(password); + if (password != nullptr) free(password); std::string user; - if (username != 0x0) user = username; + if (username != nullptr) user = username; free(username); @@ -185,14 +185,14 @@ const std::string http_request::get_user() const { } const std::string http_request::get_pass() const { - char* username = 0x0; - char* password = 0x0; + char* username = nullptr; + char* password = nullptr; username = MHD_basic_auth_get_username_password(underlying_connection, &password); - if (username != 0x0) free(username); + if (username != nullptr) free(username); std::string pass; - if (password != 0x0) pass = password; + if (password != nullptr) pass = password; free(password); @@ -200,11 +200,11 @@ const std::string http_request::get_pass() const { } const std::string http_request::get_digested_user() const { - char* digested_user_c = 0x0; + char* digested_user_c = nullptr; digested_user_c = MHD_digest_auth_get_username(underlying_connection); std::string digested_user = EMPTY; - if (digested_user_c != 0x0) { + if (digested_user_c != nullptr) { digested_user = digested_user_c; free(digested_user_c); } diff --git a/src/http_response.cpp b/src/http_response.cpp index 052e58f8..c801fa66 100644 --- a/src/http_response.cpp +++ b/src/http_response.cpp @@ -27,7 +27,7 @@ namespace httpserver { MHD_Response* http_response::get_raw_response() { - return MHD_create_response_from_buffer(0, 0x0, MHD_RESPMEM_PERSISTENT); + return MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT); } void http_response::decorate_response(MHD_Response* response) { diff --git a/src/http_utils.cpp b/src/http_utils.cpp index a79f8f91..e275f128 100644 --- a/src/http_utils.cpp +++ b/src/http_utils.cpp @@ -345,8 +345,8 @@ ip_representation::ip_representation(const std::string& ip) { } if (parts[i].size() == 4) { - pieces[y] = strtol((parts[i].substr(0, 2)).c_str(), NULL, 16); - pieces[y+1] = strtol((parts[i].substr(2, 2)).c_str(), NULL, 16); + pieces[y] = strtol((parts[i].substr(0, 2)).c_str(), nullptr, 16); + pieces[y+1] = strtol((parts[i].substr(2, 2)).c_str(), nullptr, 16); y += 2; } else { @@ -371,7 +371,7 @@ ip_representation::ip_representation(const std::string& ip) { for (unsigned int ii = 0; ii < subparts.size(); ii++) { if (subparts[ii] != "*") { - pieces[y+ii] = strtol(subparts[ii].c_str(), NULL, 10); + pieces[y+ii] = strtol(subparts[ii].c_str(), nullptr, 10); if (pieces[y+ii] > 255) throw std::invalid_argument("IP is badly formatted. 255 is max value for ip part."); } else { CLEAR_BIT(mask, y+ii); @@ -396,7 +396,7 @@ ip_representation::ip_representation(const std::string& ip) { if (parts.size() == 4) { for (unsigned int i = 0; i < parts.size(); i++) { if (parts[i] != "*") { - pieces[12+i] = strtol(parts[i].c_str(), NULL, 10); + pieces[12+i] = strtol(parts[i].c_str(), nullptr, 10); if (pieces[12+i] > 255) throw std::invalid_argument("IP is badly formatted. 255 is max value for ip part."); } else { CLEAR_BIT(mask, 12+i); @@ -481,7 +481,7 @@ void dump_arg_map(std::ostream &os, const std::string &prefix, const std::mapsize(); } diff --git a/src/httpserver/create_webserver.hpp b/src/httpserver/create_webserver.hpp index 74509264..d5e2b07e 100644 --- a/src/httpserver/create_webserver.hpp +++ b/src/httpserver/create_webserver.hpp @@ -335,11 +335,11 @@ class create_webserver { size_t _content_size_limit = static_cast(-1); int _connection_timeout = DEFAULT_WS_TIMEOUT; int _per_IP_connection_limit = 0; - log_access_ptr _log_access = 0x0; - log_error_ptr _log_error = 0x0; - validator_ptr _validator = 0x0; - unescaper_ptr _unescaper = 0x0; - const struct sockaddr* _bind_address = 0x0; + log_access_ptr _log_access = nullptr; + log_error_ptr _log_error = nullptr; + validator_ptr _validator = nullptr; + unescaper_ptr _unescaper = nullptr; + const struct sockaddr* _bind_address = nullptr; int _bind_socket = 0; int _max_thread_stack_size = 0; bool _use_ssl = false; @@ -363,9 +363,9 @@ class create_webserver { bool _deferred_enabled = false; bool _single_resource = false; bool _tcp_nodelay = false; - render_ptr _not_found_resource = 0x0; - render_ptr _method_not_allowed_resource = 0x0; - render_ptr _internal_error_resource = 0x0; + render_ptr _not_found_resource = nullptr; + render_ptr _method_not_allowed_resource = nullptr; + render_ptr _internal_error_resource = nullptr; friend class webserver; }; diff --git a/src/httpserver/details/modded_request.hpp b/src/httpserver/details/modded_request.hpp index a0c0086c..c4f18638 100644 --- a/src/httpserver/details/modded_request.hpp +++ b/src/httpserver/details/modded_request.hpp @@ -35,14 +35,14 @@ namespace httpserver { namespace details { struct modded_request { - struct MHD_PostProcessor *pp = 0x0; - std::string* complete_uri = 0x0; - std::string* standardized_url = 0x0; - webserver* ws = 0x0; + struct MHD_PostProcessor *pp = nullptr; + std::string* complete_uri = nullptr; + std::string* standardized_url = nullptr; + webserver* ws = nullptr; const std::shared_ptr (httpserver::http_resource::*callback)(const httpserver::http_request&); - http_request* dhr = 0x0; + http_request* dhr = nullptr; std::shared_ptr dhrs; bool second = false; bool has_body = false; @@ -56,7 +56,7 @@ struct modded_request { modded_request& operator=(modded_request&& b) = default; ~modded_request() { - if (NULL != pp) { + if (nullptr != pp) { MHD_destroy_post_processor(pp); } if (second) { diff --git a/src/httpserver/http_request.hpp b/src/httpserver/http_request.hpp index 2c426c05..00bc2d45 100644 --- a/src/httpserver/http_request.hpp +++ b/src/httpserver/http_request.hpp @@ -244,9 +244,9 @@ class http_request { size_t content_size_limit = static_cast(-1); std::string version; - struct MHD_Connection* underlying_connection = 0x0; + struct MHD_Connection* underlying_connection = nullptr; - unescaper_ptr unescaper = 0x0; + unescaper_ptr unescaper = nullptr; static MHD_Result build_request_header(void *cls, enum MHD_ValueKind kind, const char *key, const char *value); diff --git a/src/webserver.cpp b/src/webserver.cpp index 7a86c9ec..a1392bdc 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -159,8 +159,8 @@ webserver::webserver(const create_webserver& params): method_not_allowed_resource(params._method_not_allowed_resource), internal_error_resource(params._internal_error_resource) { ignore_sigpipe(); - pthread_mutex_init(&mutexwait, NULL); - pthread_cond_init(&mutexcond, NULL); + pthread_mutex_init(&mutexwait, nullptr); + pthread_cond_init(&mutexcond, nullptr); } webserver::~webserver() { @@ -180,10 +180,10 @@ void webserver::request_completed(void *cls, struct MHD_Connection *connection, std::ignore = toe; details::modded_request* mr = static_cast(*con_cls); - if (mr == 0x0) return; + if (mr == nullptr) return; delete mr; - mr = 0x0; + mr = nullptr; } bool webserver::register_resource(const std::string& resource, http_resource* hrm, bool family) { @@ -204,14 +204,14 @@ bool webserver::register_resource(const std::string& resource, http_resource* hr bool webserver::start(bool blocking) { struct { - MHD_OptionItem operator ()(enum MHD_OPTION opt, intptr_t val, void *ptr = 0) { + MHD_OptionItem operator ()(enum MHD_OPTION opt, intptr_t val, void *ptr = nullptr) { MHD_OptionItem x = {opt, val, ptr}; return x; } } gen; vector iov; - iov.push_back(gen(MHD_OPTION_NOTIFY_COMPLETED, (intptr_t) &request_completed, NULL)); + iov.push_back(gen(MHD_OPTION_NOTIFY_COMPLETED, (intptr_t) &request_completed, nullptr)); iov.push_back(gen(MHD_OPTION_URI_LOG_CALLBACK, (intptr_t) &uri_log, this)); iov.push_back(gen(MHD_OPTION_EXTERNAL_LOGGER, (intptr_t) &error_log, this)); iov.push_back(gen(MHD_OPTION_UNESCAPE_CALLBACK, (intptr_t) &unescaper_func, this)); @@ -279,7 +279,7 @@ bool webserver::start(bool blocking) { } #endif // HAVE_GNUTLS - iov.push_back(gen(MHD_OPTION_END, 0, NULL)); + iov.push_back(gen(MHD_OPTION_END, 0, nullptr)); int start_conf = start_method; @@ -310,8 +310,8 @@ bool webserver::start(bool blocking) { start_conf |= MHD_USE_TCP_FASTOPEN; #endif - daemon = NULL; - if (bind_address == 0x0) { + daemon = nullptr; + if (bind_address == nullptr) { daemon = MHD_start_daemon(start_conf, port, &policy_callback, this, &answer_to_connection, this, MHD_OPTION_ARRAY, &iov[0], MHD_OPTION_END); @@ -321,7 +321,7 @@ bool webserver::start(bool blocking) { &iov[0], MHD_OPTION_SOCK_ADDR, bind_address, MHD_OPTION_END); } - if (daemon == NULL) { + if (daemon == nullptr) { throw std::invalid_argument("Unable to connect daemon to port: " + std::to_string(port)); } @@ -430,11 +430,11 @@ void error_log(void* cls, const char* fmt, va_list ap) { std::ignore = ap; webserver* dws = static_cast(cls); - if (dws->log_error != 0x0) dws->log_error(fmt); + if (dws->log_error != nullptr) dws->log_error(fmt); } void access_log(webserver* dws, string uri) { - if (dws->log_access != 0x0) dws->log_access(uri); + if (dws->log_access != nullptr) dws->log_access(uri); } size_t unescaper_func(void * cls, struct MHD_Connection *c, char *s) { @@ -472,7 +472,7 @@ void webserver::upgrade_handler(void *cls, struct MHD_Connection* connection, vo } const std::shared_ptr webserver::not_found_page(details::modded_request* mr) const { - if (not_found_resource != 0x0) { + if (not_found_resource != nullptr) { return not_found_resource(*mr->dhr); } else { return std::shared_ptr(new string_response(NOT_FOUND_ERROR, http_utils::http_not_found)); @@ -480,7 +480,7 @@ const std::shared_ptr webserver::not_found_page(details::modded_r } const std::shared_ptr webserver::method_not_allowed_page(details::modded_request* mr) const { - if (method_not_allowed_resource != 0x0) { + if (method_not_allowed_resource != nullptr) { return method_not_allowed_resource(*mr->dhr); } else { return std::shared_ptr(new string_response(METHOD_ERROR, http_utils::http_method_not_allowed)); @@ -488,7 +488,7 @@ const std::shared_ptr webserver::method_not_allowed_page(details: } const std::shared_ptr webserver::internal_error_page(details::modded_request* mr, bool force_our) const { - if (internal_error_resource != 0x0 && !force_our) { + if (internal_error_resource != nullptr && !force_our) { return internal_error_resource(*mr->dhr); } else { return std::shared_ptr(new string_response(GENERIC_ERROR, http_utils::http_internal_server_error, "text/plain")); @@ -507,13 +507,13 @@ MHD_Result webserver::requests_answer_first_step(MHD_Connection* connection, str const char *encoding = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, http_utils::http_header_content_type); if (post_process_enabled && - (0x0 != encoding && + (nullptr != encoding && ((0 == strncasecmp(http_utils::http_post_encoding_form_urlencoded, encoding, strlen(http_utils::http_post_encoding_form_urlencoded))) || (0 == strncasecmp(http_utils::http_post_encoding_multipart_formdata, encoding, strlen(http_utils::http_post_encoding_multipart_formdata)))))) { const size_t post_memory_limit(32 * 1024); // Same as #MHD_POOL_SIZE_DEFAULT mr->pp = MHD_create_post_processor(connection, post_memory_limit, &post_iterator, mr); } else { - mr->pp = NULL; + mr->pp = nullptr; } return MHD_YES; } @@ -529,7 +529,7 @@ MHD_Result webserver::requests_answer_second_step(MHD_Connection* connection, co #endif // DEBUG mr->dhr->grow_content(upload_data, *upload_data_size); - if (mr->pp != NULL) MHD_post_process(mr->pp, upload_data, *upload_data_size); + if (mr->pp != nullptr) MHD_post_process(mr->pp, upload_data, *upload_data_size); } *upload_data_size = 0; diff --git a/test/integ/basic.cpp b/test/integ/basic.cpp index e6a0d31f..7c8669c2 100644 --- a/test/integ/basic.cpp +++ b/test/integ/basic.cpp @@ -416,7 +416,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, request_with_header) curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base"); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); - struct curl_slist *list = NULL; + struct curl_slist *list = nullptr; list = curl_slist_append(list, "MyHeader: MyValue"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); @@ -502,7 +502,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, complete) CURL* curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base"); curl_easy_setopt(curl, CURLOPT_POST, 1L); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, nullptr); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0); CURLcode res = curl_easy_perform(curl); LT_ASSERT_EQ(res, 0); @@ -579,7 +579,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, only_render) curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base"); curl_easy_setopt(curl, CURLOPT_POST, 1L); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, nullptr); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -931,8 +931,8 @@ LT_BEGIN_AUTO_TEST(basic_suite, request_is_printable) curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); - struct curl_slist *list = NULL; - list = curl_slist_append(NULL, "MyHeader: MyValue"); + struct curl_slist *list = nullptr; + list = curl_slist_append(nullptr, "MyHeader: MyValue"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); res = curl_easy_perform(curl); @@ -966,8 +966,8 @@ LT_BEGIN_AUTO_TEST(basic_suite, response_is_printable) curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); - struct curl_slist *list = NULL; - list = curl_slist_append(NULL, "MyHeader: MyValue"); + struct curl_slist *list = nullptr; + list = curl_slist_append(nullptr, "MyHeader: MyValue"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); res = curl_easy_perform(curl); diff --git a/test/integ/ws_start_stop.cpp b/test/integ/ws_start_stop.cpp index 89506afc..0d9b77cc 100644 --- a/test/integ/ws_start_stop.cpp +++ b/test/integ/ws_start_stop.cpp @@ -494,14 +494,14 @@ void* start_ws_blocking(void* par) { ws->register_resource("base", &ok); ws->start(true); - return 0x0; + return nullptr; } LT_BEGIN_AUTO_TEST(ws_start_stop_suite, blocking_server) httpserver::webserver ws = httpserver::create_webserver(8080); pthread_t tid; - pthread_create(&tid, NULL, start_ws_blocking, reinterpret_cast(&ws)); + pthread_create(&tid, nullptr, start_ws_blocking, reinterpret_cast(&ws)); sleep(1); diff --git a/test/unit/http_utils_test.cpp b/test/unit/http_utils_test.cpp index 5a31bfb9..0bf80024 100644 --- a/test/unit/http_utils_test.cpp +++ b/test/unit/http_utils_test.cpp @@ -193,7 +193,7 @@ LT_BEGIN_AUTO_TEST(http_utils_suite, ip_to_str_invalid_family) LT_END_AUTO_TEST(ip_to_str_invalid_family) LT_BEGIN_AUTO_TEST(http_utils_suite, ip_to_str_null) - LT_CHECK_THROW(httpserver::http::get_ip_str((struct sockaddr*) 0x0)); + LT_CHECK_THROW(httpserver::http::get_ip_str((struct sockaddr*) nullptr)); LT_END_AUTO_TEST(ip_to_str_null) LT_BEGIN_AUTO_TEST(http_utils_suite, get_port_invalid_family) @@ -207,7 +207,7 @@ LT_BEGIN_AUTO_TEST(http_utils_suite, get_port_invalid_family) LT_END_AUTO_TEST(get_port_invalid_family) LT_BEGIN_AUTO_TEST(http_utils_suite, get_port_null) - LT_CHECK_THROW(httpserver::http::get_port((struct sockaddr*) 0x0)); + LT_CHECK_THROW(httpserver::http::get_port((struct sockaddr*) nullptr)); LT_END_AUTO_TEST(get_port_null) LT_BEGIN_AUTO_TEST(http_utils_suite, ip_representation4_str)