[Scudo] Improve ScopedString constructor

Avoid referencing elements beyond internal vector size.

Reviewed By: cryptoad

Differential Revision: https://reviews.llvm.org/D103718
This commit is contained in:
Vitaly Buka 2021-06-04 13:48:54 -07:00
parent b850798f11
commit df87aeb826
2 changed files with 26 additions and 3 deletions

View File

@ -18,12 +18,12 @@ namespace scudo {
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);

View File

@ -12,6 +12,12 @@
#include <limits.h>
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<int>(-1),
@ -28,6 +34,23 @@ TEST(ScudoStringsTest, Basic) {
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");