From fabc18a680fd331a25604e9a085a0fcc14b379b3 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Mon, 21 Feb 2022 17:14:51 +0100 Subject: [PATCH] Updated the StringUtil::replace function to avoid potential endless loops. --- es-core/src/utils/StringUtil.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/es-core/src/utils/StringUtil.cpp b/es-core/src/utils/StringUtil.cpp index 5eb54e49f..0a86e65c4 100644 --- a/es-core/src/utils/StringUtil.cpp +++ b/es-core/src/utils/StringUtil.cpp @@ -583,17 +583,20 @@ namespace Utils } std::string replace(const std::string& stringArg, - const std::string& replace, - const std::string& with) + const std::string& from, + const std::string& to) { - std::string stringReplace = stringArg; - size_t pos; + std::string replaced; + size_t lastPos {0}; + size_t findPos {0}; - while ((pos = stringReplace.find(replace)) != std::string::npos) - stringReplace = - stringReplace.replace(pos, replace.length(), with.c_str(), with.length()); + while ((findPos = stringArg.find(from, lastPos)) != std::string::npos) { + replaced.append(stringArg, lastPos, findPos - lastPos).append(to); + lastPos = findPos + from.length(); + } - return stringReplace; + replaced.append(stringArg.substr(lastPos)); + return replaced; } std::wstring stringToWideString(const std::string& stringArg)