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 @@ -18,7 +18,11 @@ class ScopedString { public: - explicit ScopedString() { String.push_back('\0'); } + explicit ScopedString() { init(); } + explicit ScopedString(uptr Size) { + String.reserve(Size + 1); + init(); + } uptr length() { return String.size() - 1; } const char *data() { return String.data(); } void clear() { @@ -30,6 +34,7 @@ void output() const { outputRaw(String.data()); } private: + void init() { String.push_back('\0'); } 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,10 @@ } TEST(ScudoStringsTest, ClearLarge) { - scudo::ScopedString Str; + const char appendString[] = "123"; + scudo::ScopedString Str(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()); @@ -75,7 +76,7 @@ // Use a ScopedString that spans a page, and attempt to write past the end // of it with variations of append. The expectation is for nothing to crash. const scudo::uptr PageSize = scudo::getPageSizeCached(); - scudo::ScopedString Str; + scudo::ScopedString Str(2 * PageSize); Str.clear(); fillString(Str, 2 * PageSize); Str.clear();