From 91ff474c44ed52ef2abce36fc08dcb7a32a4739e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 14 Aug 2019 23:20:25 -0400 Subject: [PATCH] hecl/hecl: Handle bounded strings within CaseInsensitiveCompare std::string_view instances aren't guaranteed to be null-terminated, so we shouldn't be treating them as if they are in these functions, and should instead use a bounded comparison based off their sizes. This way we prevent an edge-case from ever becoming a problem and also remove an ifdef, making the code uniform across all implementations. --- hecl/include/hecl/hecl.hpp | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/hecl/include/hecl/hecl.hpp b/hecl/include/hecl/hecl.hpp index 57d51c93a..d8e49c0e3 100644 --- a/hecl/include/hecl/hecl.hpp +++ b/hecl/include/hecl/hecl.hpp @@ -22,20 +22,22 @@ extern "C" int rep_closefrom(int lower); #endif #include #include +#include #include #include "winsupport.hpp" #endif +#include #include -#include #include #include +#include #include -#include -#include -#include #include #include +#include +#include + #include "logvisor/logvisor.hpp" #include "athena/Global.hpp" #include "../extern/boo/xxhash/xxhash.h" @@ -469,20 +471,16 @@ public: */ struct CaseInsensitiveCompare { bool operator()(std::string_view lhs, std::string_view rhs) const { -#if _WIN32 - if (_stricmp(lhs.data(), rhs.data()) < 0) -#else - if (strcasecmp(lhs.data(), rhs.data()) < 0) -#endif - return true; - return false; + return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](char lhs, char rhs) { + return std::tolower(static_cast(lhs)) < std::tolower(static_cast(rhs)); + }); } #if _WIN32 bool operator()(std::wstring_view lhs, std::wstring_view rhs) const { - if (_wcsicmp(lhs.data(), rhs.data()) < 0) - return true; - return false; + return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](wchar_t lhs, wchar_t rhs) { + return std::towlower(lhs) < std::towlower(rhs); + }); } #endif };