From: Joel Rosdahl Date: Wed, 9 Nov 2022 20:06:05 +0000 (+0100) Subject: refactor: Avoid changing loop variable in body of for loop X-Git-Tag: v4.7.4~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e031c2170ba9ebeaadf240c22b8fe9f8be13010b;p=thirdparty%2Fccache.git refactor: Avoid changing loop variable in body of for loop This is to please CodeQL's "For loop variable changed in body" check. --- diff --git a/src/Util.cpp b/src/Util.cpp index bdcc9117d..eaac8b953 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -438,7 +438,8 @@ expand_environment_variables(const std::string& str) { std::string result; const char* left = str.c_str(); - for (const char* right = left; *right; ++right) { + const char* right = left; + while (*right) { if (*right == '$') { result.append(left, right - left); @@ -471,6 +472,7 @@ expand_environment_variables(const std::string& str) left = right + 1; } } + ++right; } result += left; return result; diff --git a/src/util/string.cpp b/src/util/string.cpp index 7f1784159..3a03a0bec 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -134,7 +134,8 @@ percent_decode(std::string_view string) std::string result; result.reserve(string.size()); - for (size_t i = 0; i < string.size(); ++i) { + size_t i = 0; + while (i < string.size()) { if (string[i] != '%') { result += string[i]; } else if (i + 2 >= string.size() || !std::isxdigit(string[i + 1]) @@ -147,6 +148,7 @@ percent_decode(std::string_view string) result += ch; i += 2; } + ++i; } return result;