From 86d66ddf825ecac0fb95118d22efeac7952ee8c9 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 13 Oct 2024 15:33:38 +1000 Subject: [PATCH] StringUtil: Don't call data() on empty string_view --- src/common/string_util.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/common/string_util.h b/src/common/string_util.h index 0274f7212..f95374252 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -64,20 +64,21 @@ static inline int Strncasecmp(const char* s1, const char* s2, std::size_t n) // Case-insensitive equality of string views. static inline bool EqualNoCase(std::string_view s1, std::string_view s2) { - if (s1.length() != s2.length()) + const size_t s1_len = s1.length(); + const size_t s2_len = s2.length(); + if (s1_len != s2_len) return false; + else if (s1_len == 0) + return true; - return (Strncasecmp(s1.data(), s2.data(), s1.length()) == 0); + return (Strncasecmp(s1.data(), s2.data(), s1_len) == 0); } static inline int CompareNoCase(std::string_view s1, std::string_view s2) { const size_t s1_len = s1.length(); const size_t s2_len = s2.length(); - if (s1_len != s2_len) - return false; - const size_t compare_len = std::min(s1_len, s2_len); - const int compare_res = Strncasecmp(s1.data(), s2.data(), compare_len); + const int compare_res = (compare_len > 0) ? Strncasecmp(s1.data(), s2.data(), compare_len) : 0; return (compare_len != 0) ? compare_res : ((s1_len < s2_len) ? -1 : ((s1_len > s2_len) ? 1 : 0)); }