From 6326aec0894463999075c622d0dc7f666e24ca30 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 8 Apr 2014 22:44:30 +0900 Subject: [PATCH] nghttpx: Return std::unique_ptr from parse_config_str_list --- src/shrpx.cc | 4 ++-- src/shrpx_config.cc | 8 ++++---- src/shrpx_config.h | 17 +++++++++-------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/shrpx.cc b/src/shrpx.cc index a6eec5ce..9daa88eb 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -1168,11 +1168,11 @@ int main(int argc, char **argv) if(!get_config()->npn_list) { mod_config()->npn_list = parse_config_str_list(&mod_config()->npn_list_len, - DEFAULT_NPN_LIST); + DEFAULT_NPN_LIST).release(); } if(!get_config()->tls_proto_list) { mod_config()->tls_proto_list = parse_config_str_list - (&mod_config()->tls_proto_list_len, DEFAULT_TLS_PROTO_LIST); + (&mod_config()->tls_proto_list_len, DEFAULT_TLS_PROTO_LIST).release(); } if(!get_config()->subcerts.empty()) { diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index b9305782..8fd4c15a 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -229,12 +229,12 @@ void set_config_str(char **destp, const char *val) *destp = strdup(val); } -char** parse_config_str_list(size_t *outlen, const char *s) +std::unique_ptr parse_config_str_list(size_t *outlen, const char *s) { size_t len = 1; for(const char *first = s, *p = nullptr; (p = strchr(first, ',')); ++len, first = p + 1); - auto list = new char*[len]; + auto list = util::make_unique(len); auto first = strdup(s); len = 0; for(;;) { @@ -468,11 +468,11 @@ int parse_config(const char *opt, const char *optarg) } else if(util::strieq(opt, SHRPX_OPT_NPN_LIST)) { delete [] mod_config()->npn_list; mod_config()->npn_list = parse_config_str_list(&mod_config()->npn_list_len, - optarg); + optarg).release(); } else if(util::strieq(opt, SHRPX_OPT_TLS_PROTO_LIST)) { delete [] mod_config()->tls_proto_list; mod_config()->tls_proto_list = parse_config_str_list - (&mod_config()->tls_proto_list_len, optarg); + (&mod_config()->tls_proto_list_len, optarg).release(); } else if(util::strieq(opt, SHRPX_OPT_VERIFY_CLIENT)) { mod_config()->verify_client = util::strieq(optarg, "yes"); } else if(util::strieq(opt, SHRPX_OPT_VERIFY_CLIENT_CACERT)) { diff --git a/src/shrpx_config.h b/src/shrpx_config.h index 31e6ef10..df80e2d9 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -274,15 +275,15 @@ int load_config(const char *filename); std::string read_passwd_from_file(const char *filename); // Parses comma delimited strings in |s| and returns the array of -// pointers, each element points to the each substring in |s|. The -// number of elements are stored in |*outlen|. The |s| must be comma -// delimited list of strings. The strings must be delimited by a +// pointers, each element points to the each substring in |s|. The +// number of elements are stored in |*outlen|. The |s| must be comma +// delimited list of strings. The strings must be delimited by a // single comma and any white spaces around it are treated as a part -// of protocol strings. This function may modify |s| and the caller -// must leave it as is after this call. This function allocates memory -// to store the parsed strings and it is caller's responsibility to -// deallocate the memory. -char** parse_config_str_list(size_t *outlen, const char *s); +// of protocol strings. This function may modify |s| and the caller +// must leave it as is after this call. This function copies |s| and +// first element in the return value points to it. It is caller's +// responsibility to deallocate its memory. +std::unique_ptr parse_config_str_list(size_t *outlen, const char *s); // Copies NULL-terminated string |val| to |*destp|. If |*destp| is not // NULL, it is freed before copying.