[compiler-rt] df87aeb - [Scudo] Improve ScopedString constructor
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 4 18:28:41 PDT 2021
Author: Vitaly Buka
Date: 2021-06-04T18:28:29-07:00
New Revision: df87aeb8268240a1f9042aff1450d84900c52ae5
URL: https://github.com/llvm/llvm-project/commit/df87aeb8268240a1f9042aff1450d84900c52ae5
DIFF: https://github.com/llvm/llvm-project/commit/df87aeb8268240a1f9042aff1450d84900c52ae5.diff
LOG: [Scudo] Improve ScopedString constructor
Avoid referencing elements beyond internal vector size.
Reviewed By: cryptoad
Differential Revision: https://reviews.llvm.org/D103718
Added:
Modified:
compiler-rt/lib/scudo/standalone/string_utils.h
compiler-rt/lib/scudo/standalone/tests/strings_test.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/scudo/standalone/string_utils.h b/compiler-rt/lib/scudo/standalone/string_utils.h
index 7c1cd5753068..7d9c539c2711 100644
--- a/compiler-rt/lib/scudo/standalone/string_utils.h
+++ b/compiler-rt/lib/scudo/standalone/string_utils.h
@@ -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);
diff --git a/compiler-rt/lib/scudo/standalone/tests/strings_test.cpp b/compiler-rt/lib/scudo/standalone/tests/strings_test.cpp
index 332bac2f796e..298977a14c80 100644
--- 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 <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");
More information about the llvm-commits
mailing list