From 841b3c87dbe015924112de7017b032b8a3e73a1b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 25 Mar 2016 23:04:44 +0900 Subject: [PATCH] src: Don't allow const char * in ends_with and ends_with_l --- src/util.cc | 2 +- src/util.h | 10 +++++----- src/util_test.cc | 11 +++++++---- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/util.cc b/src/util.cc index f8463abe..9139872d 100644 --- a/src/util.cc +++ b/src/util.cc @@ -735,7 +735,7 @@ bool check_path(const std::string &path) { path.find('\\') == std::string::npos && path.find("/../") == std::string::npos && path.find("/./") == std::string::npos && - !util::ends_with(path, "/..") && !util::ends_with(path, "/."); + !util::ends_with_l(path, "/..") && !util::ends_with_l(path, "/."); } int64_t to_time64(const timeval &tv) { diff --git a/src/util.h b/src/util.h index cb61fe54..45aacbad 100644 --- a/src/util.h +++ b/src/util.h @@ -242,13 +242,13 @@ bool ends_with(InputIterator1 first1, InputIterator1 last1, return std::equal(first2, last2, last1 - (last2 - first2)); } -inline bool ends_with(const std::string &a, const std::string &b) { - return ends_with(std::begin(a), std::end(a), std::begin(b), std::end(b)); +template bool ends_with(const T &a, const S &b) { + return ends_with(a.begin(), a.end(), b.begin(), b.end()); } -template -bool ends_with_l(const StringRef &a, const CharT(&b)[N]) { - return ends_with(std::begin(a), std::end(a), b, b + N - 1); +template +bool ends_with_l(const T &a, const CharT(&b)[N]) { + return ends_with(a.begin(), a.end(), b, b + N - 1); } template diff --git a/src/util_test.cc b/src/util_test.cc index 37604512..019a790f 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -394,10 +394,13 @@ void test_util_starts_with(void) { } void test_util_ends_with(void) { - CU_ASSERT(util::ends_with("foo", "foo")); - CU_ASSERT(util::ends_with("foo", "")); - CU_ASSERT(util::ends_with("ofoo", "foo")); - CU_ASSERT(!util::ends_with("ofoo", "fo")); + CU_ASSERT( + util::ends_with(StringRef::from_lit("foo"), StringRef::from_lit("foo"))); + CU_ASSERT(util::ends_with(StringRef::from_lit("foo"), StringRef{})); + CU_ASSERT( + util::ends_with(StringRef::from_lit("ofoo"), StringRef::from_lit("foo"))); + CU_ASSERT( + !util::ends_with(StringRef::from_lit("ofoo"), StringRef::from_lit("fo"))); CU_ASSERT( util::iends_with(StringRef::from_lit("fOo"), StringRef::from_lit("Foo")));