diff --git a/compiler-rt/lib/scudo/standalone/string_utils.h b/compiler-rt/lib/scudo/standalone/string_utils.h --- a/compiler-rt/lib/scudo/standalone/string_utils.h +++ b/compiler-rt/lib/scudo/standalone/string_utils.h @@ -19,12 +19,11 @@ class ScopedString { public: explicit ScopedString() { String.push_back('\0'); } - uptr length() { return Length; } + uptr length() { return String.size() - 1; } const char *data() { return String.data(); } void clear() { String.clear(); String.push_back('\0'); - Length = 0; } void append(const char *Format, va_list Args); void append(const char *Format, ...); @@ -32,7 +31,6 @@ private: Vector String; - uptr Length = 0; }; int formatString(char *Buffer, uptr BufferLength, const char *Format, ...); diff --git a/compiler-rt/lib/scudo/standalone/string_utils.cpp b/compiler-rt/lib/scudo/standalone/string_utils.cpp --- a/compiler-rt/lib/scudo/standalone/string_utils.cpp +++ b/compiler-rt/lib/scudo/standalone/string_utils.cpp @@ -219,7 +219,6 @@ } void ScopedString::append(const char *Format, va_list Args) { - RAW_CHECK(Length <= String.size()); va_list ArgsCopy; va_copy(ArgsCopy, Args); // formatString doesn't currently support a null buffer or zero buffer length, @@ -228,11 +227,13 @@ char C[1]; const uptr AdditionalLength = static_cast(formatString(C, sizeof(C), Format, Args)) + 1; - String.resize(Length + AdditionalLength); - formatString(String.data() + Length, AdditionalLength, Format, ArgsCopy); + const uptr Length = length(); + String.resize(length() + AdditionalLength); + const uptr AddedLength = static_cast(formatString( + String.data() + Length, String.size() - Length, Format, ArgsCopy)); + RAW_CHECK(data()[length()] == '\0'); + RAW_CHECK(AddedLength == AdditionalLength); va_end(ArgsCopy); - Length = strlen(String.data()); - RAW_CHECK(Length < String.size()); } FORMAT(2, 3)