Skip to content

Commit

Permalink
[NFC][sanitizer] Add InternalScopedString::Append (llvm#66559)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalybuka authored and ZijunZhaoCCK committed Sep 19, 2023
1 parent ddb026e commit d6cb40e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ class InternalScopedString {
buffer_.resize(1);
buffer_[0] = '\0';
}
void Append(const char *str);
void AppendF(const char *format, ...) FORMAT(2, 3);
const char *data() const { return buffer_.data(); }
char *data() { return buffer_.data(); }
Expand Down
9 changes: 9 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,15 @@ int internal_snprintf(char *buffer, uptr length, const char *format, ...) {
return needed_length;
}

void InternalScopedString::Append(const char *str) {
if (!str) // For consistency with AppendF("%s", str) which accepts nullptr.
return;
uptr prev_len = length();
uptr str_len = internal_strlen(str);
buffer_.resize(prev_len + str_len + 1);
internal_memcpy(buffer_.data() + prev_len, str, str_len + 1);
}

void InternalScopedString::AppendF(const char *format, ...) {
uptr prev_len = length();

Expand Down
28 changes: 27 additions & 1 deletion compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,33 @@ TEST(SanitizerCommon, RemoveANSIEscapeSequencesFromString) {
}
}

TEST(SanitizerCommon, InternalScopedString) {
TEST(SanitizerCommon, InternalScopedStringAppend) {
InternalScopedString str;
EXPECT_EQ(0U, str.length());
EXPECT_STREQ("", str.data());

str.Append(nullptr);
EXPECT_EQ(0U, str.length());
EXPECT_STREQ("", str.data());

str.Append("");
EXPECT_EQ(0U, str.length());
EXPECT_STREQ("", str.data());

str.Append("foo");
EXPECT_EQ(3U, str.length());
EXPECT_STREQ("foo", str.data());

str.Append("");
EXPECT_EQ(3U, str.length());
EXPECT_STREQ("foo", str.data());

str.Append("123\000456");
EXPECT_EQ(6U, str.length());
EXPECT_STREQ("foo123", str.data());
}

TEST(SanitizerCommon, InternalScopedStringAppendF) {
InternalScopedString str;
EXPECT_EQ(0U, str.length());
EXPECT_STREQ("", str.data());
Expand Down

0 comments on commit d6cb40e

Please sign in to comment.