Index: libcxxabi/src/demangle/Utility.h =================================================================== --- libcxxabi/src/demangle/Utility.h +++ libcxxabi/src/demangle/Utility.h @@ -75,7 +75,10 @@ OutputBuffer(const OutputBuffer &) = delete; OutputBuffer &operator=(const OutputBuffer &) = delete; - operator StringView() const { return StringView(Buffer, CurrentPosition); } + operator StringView() const { + assert(Buffer && "Buffer cannot by nullptr"); + return StringView(Buffer, CurrentPosition); + } void reset(char *Buffer_, size_t BufferCapacity_) { CurrentPosition = 0; @@ -174,6 +177,8 @@ return CurrentPosition ? Buffer[CurrentPosition - 1] : '\0'; } + // Some uses require a buffer even when they write nothing. + void allocateBuffer() { grow(1); } bool empty() const { return CurrentPosition == 0; } char *getBuffer() { return Buffer; } Index: llvm/include/llvm/Demangle/Utility.h =================================================================== --- llvm/include/llvm/Demangle/Utility.h +++ llvm/include/llvm/Demangle/Utility.h @@ -75,7 +75,10 @@ OutputBuffer(const OutputBuffer &) = delete; OutputBuffer &operator=(const OutputBuffer &) = delete; - operator StringView() const { return StringView(Buffer, CurrentPosition); } + operator StringView() const { + assert(Buffer && "Buffer cannot by nullptr"); + return StringView(Buffer, CurrentPosition); + } void reset(char *Buffer_, size_t BufferCapacity_) { CurrentPosition = 0; @@ -174,6 +177,8 @@ return CurrentPosition ? Buffer[CurrentPosition - 1] : '\0'; } + // Some uses require a buffer even when they write nothing. + void allocateBuffer() { grow(1); } bool empty() const { return CurrentPosition == 0; } char *getBuffer() { return Buffer; } Index: llvm/lib/Demangle/MicrosoftDemangle.cpp =================================================================== --- llvm/lib/Demangle/MicrosoftDemangle.cpp +++ llvm/lib/Demangle/MicrosoftDemangle.cpp @@ -1301,6 +1301,10 @@ goto StringLiteralError; } + // We can demangle to an empty string, and need to avoid a null buffer in that + // case. + OB.allocateBuffer(); + // Encoded Length std::tie(StringByteSize, IsNegative) = demangleNumber(MangledName); if (Error || IsNegative || StringByteSize < (IsWcharT ? 2 : 1)) Index: llvm/unittests/Demangle/OutputBufferTest.cpp =================================================================== --- llvm/unittests/Demangle/OutputBufferTest.cpp +++ llvm/unittests/Demangle/OutputBufferTest.cpp @@ -44,6 +44,8 @@ TEST(OutputBufferTest, Insert) { OutputBuffer OB; + OB.allocateBuffer(); + OB.insert(0, "", 0); EXPECT_EQ("", toString(OB));