This is an automated email from the ASF dual-hosted git repository.
aboda pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git
The following commit(s) were added to refs/heads/master by this push:
new 189b1bd MINIFICPP-1280 - StringUtils::replaceAll hangs in case
from_string is empty
189b1bd is described below
commit 189b1bd451fa399ae74138fbf5ae6a6adbb4ea4e
Author: Arpad Boda <[email protected]>
AuthorDate: Mon Jul 6 15:21:45 2020 +0200
MINIFICPP-1280 - StringUtils::replaceAll hangs in case from_string is empty
Signed-off-by: Arpad Boda <[email protected]>
Approved by szaszm on GH
This closes #831
---
extensions/windows-event-log/ConsumeWindowsEventLog.cpp | 3 +++
libminifi/src/utils/StringUtils.cpp | 3 +++
libminifi/test/unit/StringUtilsTests.cpp | 3 +++
3 files changed, 9 insertions(+)
diff --git a/extensions/windows-event-log/ConsumeWindowsEventLog.cpp
b/extensions/windows-event-log/ConsumeWindowsEventLog.cpp
index 252c1c7..e2e1d0f 100644
--- a/extensions/windows-event-log/ConsumeWindowsEventLog.cpp
+++ b/extensions/windows-event-log/ConsumeWindowsEventLog.cpp
@@ -572,6 +572,9 @@ bool ConsumeWindowsEventLog::createEventRender(EVT_HANDLE
hEvent, EventRender& e
for (const auto &mapEntry : walker.getIdentifiers()) {
// replace the identifiers with their translated strings.
+ if (mapEntry.first.empty() || mapEntry.second.empty()) {
+ continue; // This is most probably a result of a failed ID
resolution
+ }
utils::StringUtils::replaceAll(message, mapEntry.first,
mapEntry.second);
}
wel::WindowsEventLogHeader log_header(header_names_);
diff --git a/libminifi/src/utils/StringUtils.cpp
b/libminifi/src/utils/StringUtils.cpp
index 6533422..881f721 100644
--- a/libminifi/src/utils/StringUtils.cpp
+++ b/libminifi/src/utils/StringUtils.cpp
@@ -136,6 +136,9 @@ std::string& StringUtils::replaceAll(std::string&
source_string, const std::stri
while ((lastFound = source_string.find(from_string, loc)) !=
std::string::npos) {
source_string.replace(lastFound, from_string.size(), to_string);
loc = lastFound + to_string.size();
+ if (from_string.empty()) {
+ loc++;
+ }
}
return source_string;
}
diff --git a/libminifi/test/unit/StringUtilsTests.cpp
b/libminifi/test/unit/StringUtilsTests.cpp
index f85abc1..a94f85e 100644
--- a/libminifi/test/unit/StringUtilsTests.cpp
+++ b/libminifi/test/unit/StringUtilsTests.cpp
@@ -349,6 +349,7 @@ TEST_CASE("StringUtils::replaceOne works correctly",
"[replaceOne]") {
REQUIRE(utils::StringUtils::replaceOne("banana", "an", "***") == "b***ana");
REQUIRE(utils::StringUtils::replaceOne("banana", "banana", "kiwi") ==
"kiwi");
REQUIRE(utils::StringUtils::replaceOne("banana", "banana", "grapefruit") ==
"grapefruit");
+ REQUIRE(utils::StringUtils::replaceOne("fruit", "", "grape") ==
"grapefruit");
}
TEST_CASE("StringUtils::replaceAll works correctly", "[replaceAll]") {
@@ -364,4 +365,6 @@ TEST_CASE("StringUtils::replaceAll works correctly",
"[replaceAll]") {
REQUIRE(replaceAll("banana", "an", "***") == "b******a");
REQUIRE(replaceAll("banana", "banana", "kiwi") == "kiwi");
REQUIRE(replaceAll("banana", "banana", "grapefruit") == "grapefruit");
+ REQUIRE(replaceAll("abc", "", "d") == "dadbdcd");
+ REQUIRE(replaceAll("banana", "", "") == "banana");
}