diff --git a/compiler-rt/lib/scudo/standalone/string_utils.h b/compiler-rt/lib/scudo/standalone/string_utils.h --- a/compiler-rt/lib/scudo/standalone/string_utils.h +++ b/compiler-rt/lib/scudo/standalone/string_utils.h @@ -28,6 +28,7 @@ void append(const char *Format, va_list Args); void append(const char *Format, ...) FORMAT(2, 3); void output() const { outputRaw(String.data()); } + void reserve(size_t Size) { String.reserve(Size + 1); } private: Vector String; diff --git a/compiler-rt/lib/scudo/standalone/tests/strings_test.cpp b/compiler-rt/lib/scudo/standalone/tests/strings_test.cpp --- a/compiler-rt/lib/scudo/standalone/tests/strings_test.cpp +++ b/compiler-rt/lib/scudo/standalone/tests/strings_test.cpp @@ -43,9 +43,11 @@ } TEST(ScudoStringsTest, ClearLarge) { + constexpr char appendString[] = "123"; scudo::ScopedString Str; + Str.reserve(sizeof(appendString) * 10000); for (int i = 0; i < 10000; ++i) - Str.append("123"); + Str.append(appendString); Str.clear(); EXPECT_EQ(0ul, Str.length()); EXPECT_EQ('\0', *Str.data()); @@ -76,6 +78,7 @@ // of it with variations of append. The expectation is for nothing to crash. const scudo::uptr PageSize = scudo::getPageSizeCached(); scudo::ScopedString Str; + Str.reserve(2 * PageSize); Str.clear(); fillString(Str, 2 * PageSize); Str.clear();