From 2bca6360b61fd6795588a66814b8bf23d545b723 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 25 Mar 2016 23:18:31 +0900 Subject: [PATCH] src: Don't allow const char * in istarts_with and istarts_with_l --- src/h2load.cc | 24 +++++++++++++----------- src/util.h | 19 +++++-------------- src/util_test.cc | 15 +++++++++------ 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/h2load.cc b/src/h2load.cc index 10d2f88f..c2424d74 100644 --- a/src/h2load.cc +++ b/src/h2load.cc @@ -1429,7 +1429,7 @@ constexpr char UNIX_PATH_PREFIX[] = "unix:"; } // namespace namespace { -bool parse_base_uri(std::string base_uri) { +bool parse_base_uri(const StringRef &base_uri) { http_parser_url u{}; if (http_parser_parse_url(base_uri.c_str(), base_uri.size(), 0, &u) != 0 || !util::has_uri_field(u, UF_SCHEMA) || !util::has_uri_field(u, UF_HOST)) { @@ -1462,7 +1462,7 @@ std::vector parse_uris(std::vector::iterator first, if (!config.has_base_uri()) { - if (!parse_base_uri(*first)) { + if (!parse_base_uri(StringRef{*first})) { std::cerr << "invalid URI: " << *first << std::endl; exit(EXIT_FAILURE); } @@ -1899,18 +1899,19 @@ int main(int argc, char **argv) { } break; case 'B': { + auto arg = StringRef{optarg}; config.base_uri = ""; config.base_uri_unix = false; - if (util::istarts_with_l(optarg, UNIX_PATH_PREFIX)) { + if (util::istarts_with_l(arg, UNIX_PATH_PREFIX)) { // UNIX domain socket path sockaddr_un un; - auto path = optarg + str_size(UNIX_PATH_PREFIX); - auto pathlen = strlen(optarg) - str_size(UNIX_PATH_PREFIX); + auto path = StringRef{std::begin(arg) + str_size(UNIX_PATH_PREFIX), + std::end(arg)}; - if (pathlen == 0 || pathlen + 1 > sizeof(un.sun_path)) { - std::cerr << "--base-uri: invalid UNIX domain socket path: " << optarg + if (path.size() == 0 || path.size() + 1 > sizeof(un.sun_path)) { + std::cerr << "--base-uri: invalid UNIX domain socket path: " << arg << std::endl; exit(EXIT_FAILURE); } @@ -1918,18 +1919,19 @@ int main(int argc, char **argv) { config.base_uri_unix = true; auto &unix_addr = config.unix_addr; - std::copy_n(path, pathlen + 1, unix_addr.sun_path); + std::copy(std::begin(path), std::end(path), unix_addr.sun_path); + unix_addr.sun_path[path.size()] = '\0'; unix_addr.sun_family = AF_UNIX; break; } - if (!parse_base_uri(optarg)) { - std::cerr << "--base-uri: invalid base URI: " << optarg << std::endl; + if (!parse_base_uri(arg)) { + std::cerr << "--base-uri: invalid base URI: " << arg << std::endl; exit(EXIT_FAILURE); } - config.base_uri = optarg; + config.base_uri = arg.str(); break; } case 'v': diff --git a/src/util.h b/src/util.h index 45aacbad..7f1c4714 100644 --- a/src/util.h +++ b/src/util.h @@ -215,22 +215,13 @@ bool istarts_with(InputIterator1 first1, InputIterator1 last1, return std::equal(first2, last2, first1, CaseCmp()); } -inline bool istarts_with(const std::string &a, const std::string &b) { - return istarts_with(std::begin(a), std::end(a), std::begin(b), std::end(b)); +template bool istarts_with(const S &a, const T &b) { + return istarts_with(a.begin(), a.end(), b.begin(), b.end()); } -inline bool istarts_with(const StringRef &a, const StringRef &b) { - return istarts_with(std::begin(a), std::end(a), std::begin(b), std::end(b)); -} - -template -bool istarts_with_l(const std::string &a, const CharT(&b)[N]) { - return istarts_with(std::begin(a), std::end(a), b, b + N - 1); -} - -template -bool istarts_with_l(const StringRef &a, const CharT(&b)[N]) { - return istarts_with(std::begin(a), std::end(a), b, b + N - 1); +template +bool istarts_with_l(const T &a, const CharT(&b)[N]) { + return istarts_with(a.begin(), a.end(), b, b + N - 1); } template diff --git a/src/util_test.cc b/src/util_test.cc index 019a790f..f6493ce9 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -384,13 +384,16 @@ void test_util_starts_with(void) { CU_ASSERT(util::starts_with("ofoo", "")); CU_ASSERT(!util::starts_with("ofoo", "foo")); - CU_ASSERT(util::istarts_with("FOO", "fOO")); - CU_ASSERT(util::starts_with("ofoo", "")); - CU_ASSERT(util::istarts_with("fOOo", "Foo")); - CU_ASSERT(!util::istarts_with("ofoo", "foo")); + CU_ASSERT(util::istarts_with(StringRef::from_lit("FOO"), + StringRef::from_lit("fOO"))); + CU_ASSERT(util::istarts_with(StringRef::from_lit("ofoo"), StringRef{})); + CU_ASSERT(util::istarts_with(StringRef::from_lit("fOOo"), + StringRef::from_lit("Foo"))); + CU_ASSERT(!util::istarts_with(StringRef::from_lit("ofoo"), + StringRef::from_lit("foo"))); - CU_ASSERT(util::istarts_with_l("fOOo", "Foo")); - CU_ASSERT(!util::istarts_with_l("ofoo", "foo")); + CU_ASSERT(util::istarts_with_l(StringRef::from_lit("fOOo"), "Foo")); + CU_ASSERT(!util::istarts_with_l(StringRef::from_lit("ofoo"), "foo")); } void test_util_ends_with(void) {