diff --git a/common/util.cc b/common/util.cc index 8af9f5884c..b6097bf28f 100644 --- a/common/util.cc +++ b/common/util.cc @@ -256,8 +256,9 @@ bool starts_with(const std::string &s1, const std::string &s2) { return strncmp(s1.c_str(), s2.c_str(), s2.size()) == 0; } -bool ends_with(const std::string &s1, const std::string &s2) { - return strcmp(s1.c_str() + (s1.size() - s2.size()), s2.c_str()) == 0; +bool ends_with(const std::string& s, const std::string& suffix) { + return s.size() >= suffix.size() && + strcmp(s.c_str() + (s.size() - suffix.size()), suffix.c_str()) == 0; } std::string check_output(const std::string& command) { diff --git a/common/util.h b/common/util.h index 0cbad5bd31..3bf5a690a6 100644 --- a/common/util.h +++ b/common/util.h @@ -77,7 +77,7 @@ float getenv(const char* key, float default_val); std::string hexdump(const uint8_t* in, const size_t size); std::string dir_name(std::string const& path); bool starts_with(const std::string &s1, const std::string &s2); -bool ends_with(const std::string &s1, const std::string &s2); +bool ends_with(const std::string &s, const std::string &suffix); // ***** random helpers ***** int random_int(int min, int max);