src: Don't allow const char * in istarts_with and istarts_with_l

This commit is contained in:
Tatsuhiro Tsujikawa 2016-03-25 23:18:31 +09:00
parent 841b3c87db
commit 2bca6360b6
3 changed files with 27 additions and 31 deletions

View File

@ -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<std::string> parse_uris(std::vector<std::string>::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':

View File

@ -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 <typename S, typename T> 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 <typename CharT, size_t N>
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 <typename CharT, size_t N>
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 <typename T, typename CharT, size_t N>
bool istarts_with_l(const T &a, const CharT(&b)[N]) {
return istarts_with(a.begin(), a.end(), b, b + N - 1);
}
template <typename InputIterator1, typename InputIterator2>

View File

@ -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) {