[compiler-rt] 591266c - [NFC][sanitizer] Add InternalScopedString::Append (#66559)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 15 22:40:11 PDT 2023


Author: Vitaly Buka
Date: 2023-09-15T22:40:08-07:00
New Revision: 591266c56c0cfd85c99bcf6ccbbd62481adb95dd

URL: https://github.com/llvm/llvm-project/commit/591266c56c0cfd85c99bcf6ccbbd62481adb95dd
DIFF: https://github.com/llvm/llvm-project/commit/591266c56c0cfd85c99bcf6ccbbd62481adb95dd.diff

LOG: [NFC][sanitizer] Add InternalScopedString::Append (#66559)

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp
    compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
index 26393d65c8e935e..6b327a4aa16f0b7 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
@@ -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(); }

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp
index 33a226358f62eb6..3d9d66c3f976139 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp
@@ -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();
 

diff  --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp
index 6acfdd437196d35..fc12c07bb914702 100644
--- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp
+++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp
@@ -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());


        


More information about the llvm-commits mailing list