From 991d04869421828cf6a08ff40af54e3cfbcc45c4 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 14 Sep 2019 11:28:15 -0400 Subject: [PATCH] CStringExtras: Prevent undefined behavior within IndexOfSubstring Unlikely to occur, but does completely prevent the case of undefined behavior if a non-ascii character ends up within the given string. --- Runtime/CStringExtras.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Runtime/CStringExtras.hpp b/Runtime/CStringExtras.hpp index 399030d82..bd022ce09 100644 --- a/Runtime/CStringExtras.hpp +++ b/Runtime/CStringExtras.hpp @@ -29,10 +29,12 @@ public: static int IndexOfSubstring(std::string_view haystack, std::string_view needle) { std::string str(haystack); - std::transform(str.begin(), str.end(), str.begin(), tolower); - std::string::size_type s = str.find(needle); - if (s == std::string::npos) + std::transform(str.begin(), str.end(), str.begin(), + [](char c) { return std::tolower(static_cast(c)); }); + const std::string::size_type s = str.find(needle); + if (s == std::string::npos) { return -1; + } return s; } };