Merge "String8: fix infinite loop and segmentation fault in removeAll()" into main am: 4a33c22c77 am: 339fecf742 am: e9f38cfe0f

Original change: https://android-review.googlesource.com/c/platform/system/core/+/2659075

Change-Id: I4e01a5b5738ce5e5af1d8e75d3b77695ce031bcc
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Eric Miao 2023-07-19 04:20:10 +00:00 committed by Automerger Merge Worker
commit 7239722199
2 changed files with 23 additions and 0 deletions

View file

@ -393,6 +393,11 @@ ssize_t String8::find(const char* other, size_t start) const
}
bool String8::removeAll(const char* other) {
ALOG_ASSERT(other, "String8::removeAll() requires a non-NULL string");
if (*other == '\0')
return true;
ssize_t index = find(other);
if (index < 0) return false;

View file

@ -114,3 +114,21 @@ TEST_F(String8Test, append) {
EXPECT_EQ(NO_MEMORY, s.append("baz", SIZE_MAX));
EXPECT_STREQ("foobar", s);
}
TEST_F(String8Test, removeAll) {
String8 s("Hello, world!");
// NULL input should cause an assertion failure and error message in logcat
EXPECT_DEATH(s.removeAll(NULL), "");
// expect to return true and string content should remain unchanged
EXPECT_TRUE(s.removeAll(""));
EXPECT_STREQ("Hello, world!", s);
// expect to return false
EXPECT_FALSE(s.removeAll("x"));
EXPECT_STREQ("Hello, world!", s);
EXPECT_TRUE(s.removeAll("o"));
EXPECT_STREQ("Hell, wrld!", s);
}