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

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 15 17:43:31 PDT 2023


https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/66559

>From 47dda676019e53efdb1964342304669f9c23c840 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Fri, 15 Sep 2023 16:53:03 -0700
Subject: [PATCH 1/2] [NFC][sanitizer] Add InternalScopedString::Append

---
 .../lib/sanitizer_common/sanitizer_common.h   |  1 +
 .../lib/sanitizer_common/sanitizer_printf.cpp |  7 +++++++
 .../tests/sanitizer_common_test.cpp           | 20 ++++++++++++++++++-
 3 files changed, 27 insertions(+), 1 deletion(-)

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..62c1cf4abe4256b 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp
@@ -337,6 +337,13 @@ int internal_snprintf(char *buffer, uptr length, const char *format, ...) {
   return needed_length;
 }
 
+void InternalScopedString::Append(const char *str) {
+  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..18921e85fda73d4 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,25 @@ TEST(SanitizerCommon, RemoveANSIEscapeSequencesFromString) {
   }
 }
 
-TEST(SanitizerCommon, InternalScopedString) {
+TEST(SanitizerCommon, InternalScopedStringAppend) {
+  InternalScopedString str;
+  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());

>From 33033ffc0ac67331c7bdb765a3a33b6ac87d892f Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at gmail.com>
Date: Fri, 15 Sep 2023 17:43:24 -0700
Subject: [PATCH 2/2] Update sanitizer_common_test.cpp

---
 .../lib/sanitizer_common/tests/sanitizer_common_test.cpp      | 4 ++++
 1 file changed, 4 insertions(+)

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 18921e85fda73d4..918d824f8bc76b0 100644
--- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp
+++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp
@@ -381,6 +381,10 @@ TEST(SanitizerCommon, InternalScopedStringAppend) {
   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());



More information about the llvm-commits mailing list