From a922b6c0703026021126c8833cd4d42a27126e11 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 5 Feb 2017 21:21:49 +0300 Subject: Fix incorrect buffer access in chatutils and in stringutils unit tests. --- src/utils/chatutils.cpp | 16 ++++++++++++---- src/utils/stringutils_unittest.cc | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/utils/chatutils.cpp b/src/utils/chatutils.cpp index 3ad6c8a1f..d80e8d761 100644 --- a/src/utils/chatutils.cpp +++ b/src/utils/chatutils.cpp @@ -146,12 +146,16 @@ void replaceVars(std::string &str) if (*it != localPlayer->getName()) newStr.append(*it).append(","); } - if (newStr[newStr.size() - 1] == ',') - newStr = newStr.substr(0, newStr.size() - 1); if (!newStr.empty()) + { + if (newStr[newStr.size() - 1] == ',') + newStr = newStr.substr(0, newStr.size() - 1); replaceAll(str, "", newStr); + } else + { replaceAll(str, "", ""); + } } if (str.find("") != std::string::npos) { @@ -167,12 +171,16 @@ void replaceVars(std::string &str) if (*it != localPlayer->getName()) newStr.append(*it).append(","); } - if (newStr[newStr.size() - 1] == ',') - newStr = newStr.substr(0, newStr.size() - 1); if (!newStr.empty()) + { + if (newStr[newStr.size() - 1] == ',') + newStr = newStr.substr(0, newStr.size() - 1); replaceAll(str, "", newStr); + } else + { replaceAll(str, "", ""); + } } else { diff --git a/src/utils/stringutils_unittest.cc b/src/utils/stringutils_unittest.cc index cc1b2da69..2af7b7939 100644 --- a/src/utils/stringutils_unittest.cc +++ b/src/utils/stringutils_unittest.cc @@ -781,7 +781,7 @@ TEST_CASE("stringuntils splitToIntVector 1") splitToIntVector(tokens, "10,,30", ','); REQUIRE(tokens.size() == 2); REQUIRE(tokens[0] == 10); - REQUIRE(tokens[2] == 30); + REQUIRE(tokens[1] == 30); tokens.clear(); splitToIntVector(tokens, "10,2;30", ','); -- cgit v1.2.3-70-g09d2