From a957fb669d62e7bf6f9186c803abc7f3ce6cb876 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 4 Feb 2024 14:06:58 +1000 Subject: [PATCH] StringUtil: Fix inequal-length EqualNoCase --- src/common/string_util.h | 6 +++--- src/core/settings.cpp | 4 ++-- src/core/settings.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/common/string_util.h b/src/common/string_util.h index 9300c069c..a440a96e4 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -63,10 +63,10 @@ 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.empty() || s2.empty()) - return (s1.empty() == s2.empty()); + if (s1.length() != s2.length()) + return false; - return (Strncasecmp(s1.data(), s2.data(), std::min(s1.length(), s2.length())) == 0); + return (Strncasecmp(s1.data(), s2.data(), s1.length()) == 0); } /// Wrapper around std::from_chars diff --git a/src/core/settings.cpp b/src/core/settings.cpp index 2a4577ad1..60a087cd2 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -1358,12 +1358,12 @@ static constexpr const std::array s_controller_display_names = { TRANSLATE_NOOP("ControllerType", "PlayStation Mouse"), TRANSLATE_NOOP("ControllerType", "NeGcon")}; -std::optional Settings::ParseControllerTypeName(const char* str) +std::optional Settings::ParseControllerTypeName(std::string_view str) { int index = 0; for (const char* name : s_controller_type_names) { - if (StringUtil::Strcasecmp(name, str) == 0) + if (StringUtil::EqualNoCase(str, name)) return static_cast(index); index++; diff --git a/src/core/settings.h b/src/core/settings.h index 0b1246142..726b188d8 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -409,7 +409,7 @@ struct Settings static const char* GetAudioBackendName(AudioBackend backend); static const char* GetAudioBackendDisplayName(AudioBackend backend); - static std::optional ParseControllerTypeName(const char* str); + static std::optional ParseControllerTypeName(std::string_view str); static const char* GetControllerTypeName(ControllerType type); static const char* GetControllerTypeDisplayName(ControllerType type);