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,12 +18,12 @@ class ScopedString { public: - explicit ScopedString() : String() {} + explicit ScopedString() { String.push_back('\0'); } uptr length() { return Length; } const char *data() { return String.data(); } void clear() { - if (!String.empty()) - String[0] = '\0'; + String.clear(); + String.push_back('\0'); Length = 0; } void append(const char *Format, va_list Args); 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 @@ -12,6 +12,12 @@ #include +TEST(ScudoStringsTest, Constructor) { + scudo::ScopedString Str; + EXPECT_EQ(0, Str.length()); + EXPECT_EQ('\0', *Str.data()); +} + TEST(ScudoStringsTest, Basic) { scudo::ScopedString Str; Str.append("a%db%zdc%ue%zuf%xh%zxq%pe%sr", static_cast(-1), @@ -28,6 +34,23 @@ EXPECT_STREQ(expectedString.c_str(), Str.data()); } +TEST(ScudoStringsTest, Clear) { + scudo::ScopedString Str; + Str.append("123"); + Str.clear(); + EXPECT_EQ(0, Str.length()); + EXPECT_EQ('\0', *Str.data()); +} + +TEST(ScudoStringsTest, ClearLarge) { + scudo::ScopedString Str; + for (int i = 0; i < 10000; ++i) + Str.append("123"); + Str.clear(); + EXPECT_EQ(0, Str.length()); + EXPECT_EQ('\0', *Str.data()); +} + TEST(ScudoStringsTest, Precision) { scudo::ScopedString Str; Str.append("%.*s", 3, "12345");