Commit 12915eb6 authored by Siddharth Sharma's avatar Siddharth Sharma Committed by Commit Bot

Fixed SplitStringUsingSubstr to handle empty delimiters

Issue:
The base::SplitStringUsingSubstr does not handle the case when the
delimiter is empty. The code goes into an infinite loop in that case
as begin_index and end_index always stay 0 and we keep on adding empty
string pieces to the result. This causes a memory overflow.

Fix:
This change adds a check for the delimiter size and returns the
original input string if the size is 0.

Bug: 1062612
Change-Id: I29c184bf18df1bcc50fdb6205a7ff5fa6920148b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2108213
Commit-Queue: Siddharth Sharma <siddhash@microsoft.com>
Reviewed-by: default avatardanakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751476}
parent 0ea6c7ff
...@@ -104,6 +104,11 @@ std::vector<OutputStringType> SplitStringUsingSubstrT( ...@@ -104,6 +104,11 @@ std::vector<OutputStringType> SplitStringUsingSubstrT(
using size_type = typename Piece::size_type; using size_type = typename Piece::size_type;
std::vector<OutputStringType> result; std::vector<OutputStringType> result;
if (delimiter.size() == 0) {
result.emplace_back(input);
return result;
}
for (size_type begin_index = 0, end_index = 0; end_index != Piece::npos; for (size_type begin_index = 0, end_index = 0; end_index != Piece::npos;
begin_index = end_index + delimiter.size()) { begin_index = end_index + delimiter.size()) {
end_index = input.find(delimiter, begin_index); end_index = input.find(delimiter, begin_index);
......
...@@ -218,6 +218,13 @@ TEST(SplitStringUsingSubstrTest, EmptyString) { ...@@ -218,6 +218,13 @@ TEST(SplitStringUsingSubstrTest, EmptyString) {
EXPECT_THAT(results, ElementsAre("")); EXPECT_THAT(results, ElementsAre(""));
} }
TEST(SplitStringUsingSubstrTest, EmptyDelimiter) {
std::vector<std::string> results = SplitStringUsingSubstr(
"TEST", std::string(), TRIM_WHITESPACE, SPLIT_WANT_ALL);
ASSERT_EQ(1u, results.size());
EXPECT_THAT(results, ElementsAre("TEST"));
}
TEST(StringUtilTest, SplitString_Basics) { TEST(StringUtilTest, SplitString_Basics) {
std::vector<std::string> r; std::vector<std::string> r;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment