aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Demangle/MicrosoftDemangle.cpp
diff options
context:
space:
mode:
authorNathan Sidwell <nathan@acm.org>2022-02-28 10:13:44 -0800
committerNathan Sidwell <nathan@acm.org>2022-10-17 04:23:16 -0700
commitd3b10150b683142a7481893ddffe9206e1d29f95 (patch)
treee35600e8395894be0a9070b631b9691219d10390 /llvm/lib/Demangle/MicrosoftDemangle.cpp
parent335f94bfef0e9a53a44de2f0a2a62b29e2c593e9 (diff)
downloadllvm-d3b10150b683142a7481893ddffe9206e1d29f95.zip
llvm-d3b10150b683142a7481893ddffe9206e1d29f95.tar.gz
llvm-d3b10150b683142a7481893ddffe9206e1d29f95.tar.bz2
[demangler] Simplify OutputBuffer initialization
Every non-testcase use of OutputBuffer contains code to allocate an initial buffer (using either 128 or 1024 as initial guesses). There's now no need to do that, given recent changes to the buffer extension heuristics -- it allocates a 1k(ish) buffer on first need. Just pass in a buffer (if any) to the constructor. Thus the OutputBuffer's ownership of the buffer starts at its own lifetime start. We can reduce the lifetime of this object in several cases. That new constructor takes a 'size_t *' for the size argument, as all uses with a non-null buffer are passing through a malloc'd buffer from their own caller in this manner. The buffer reset member function is never used, and is deleted. Some adjustment to a couple of uses is needed, due to the lazy buffer creation of this patch. a) the Microsoft demangler can demangle empty strings to nothing, which it then memoizes. We need to avoid the UB of passing nullptr to memcpy. b) a unit test checks insertion of no characters into an empty buffer. We need to avoid UB when converting that to std::string. The original buffer initialization code would return a failure code if that first malloc failed. Existing code either ignored that, called std::terminate with a FIXME, or returned an error code. But that's not foolproof anyway, as a subsequent buffer extension failure ends up calling std::terminate. I am working on addressing that unfortunate failure mode in a manner more consistent with the C++ ABI design. Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D122604
Diffstat (limited to 'llvm/lib/Demangle/MicrosoftDemangle.cpp')
-rw-r--r--llvm/lib/Demangle/MicrosoftDemangle.cpp22
1 files changed, 5 insertions, 17 deletions
diff --git a/llvm/lib/Demangle/MicrosoftDemangle.cpp b/llvm/lib/Demangle/MicrosoftDemangle.cpp
index b4e98a2..c21b0a3 100644
--- a/llvm/lib/Demangle/MicrosoftDemangle.cpp
+++ b/llvm/lib/Demangle/MicrosoftDemangle.cpp
@@ -246,7 +246,10 @@ demanglePointerCVQualifiers(StringView &MangledName) {
StringView Demangler::copyString(StringView Borrowed) {
char *Stable = Arena.allocUnalignedBuffer(Borrowed.size());
- std::memcpy(Stable, Borrowed.begin(), Borrowed.size());
+ // This is not a micro-optimization, it avoids UB, should Borrowed be an null
+ // buffer.
+ if (Borrowed.size())
+ std::memcpy(Stable, Borrowed.begin(), Borrowed.size());
return {Stable, Borrowed.size()};
}
@@ -970,9 +973,6 @@ void Demangler::memorizeIdentifier(IdentifierNode *Identifier) {
// Render this class template name into a string buffer so that we can
// memorize it for the purpose of back-referencing.
OutputBuffer OB;
- if (!initializeOutputBuffer(nullptr, nullptr, OB, 1024))
- // FIXME: Propagate out-of-memory as an error?
- std::terminate();
Identifier->output(OB, OF_Default);
StringView Owned = copyString(OB);
memorizeString(Owned);
@@ -1283,11 +1283,6 @@ Demangler::demangleStringLiteral(StringView &MangledName) {
EncodedStringLiteralNode *Result = Arena.alloc<EncodedStringLiteralNode>();
- // Must happen before the first `goto StringLiteralError`.
- if (!initializeOutputBuffer(nullptr, nullptr, OB, 1024))
- // FIXME: Propagate out-of-memory as an error?
- std::terminate();
-
// Prefix indicating the beginning of a string literal
if (!MangledName.consumeFront("@_"))
goto StringLiteralError;
@@ -1446,9 +1441,6 @@ Demangler::demangleLocallyScopedNamePiece(StringView &MangledName) {
// Render the parent symbol's name into a buffer.
OutputBuffer OB;
- if (!initializeOutputBuffer(nullptr, nullptr, OB, 1024))
- // FIXME: Propagate out-of-memory as an error?
- std::terminate();
OB << '`';
Scope->output(OB, OF_Default);
OB << '\'';
@@ -2311,8 +2303,6 @@ void Demangler::dumpBackReferences() {
// Create an output stream so we can render each type.
OutputBuffer OB;
- if (!initializeOutputBuffer(nullptr, nullptr, OB, 1024))
- std::terminate();
for (size_t I = 0; I < Backrefs.FunctionParamCount; ++I) {
OB.setCurrentPosition(0);
@@ -2339,7 +2329,6 @@ char *llvm::microsoftDemangle(const char *MangledName, size_t *NMangled,
char *Buf, size_t *N,
int *Status, MSDemangleFlags Flags) {
Demangler D;
- OutputBuffer OB;
StringView Name{MangledName};
SymbolNode *AST = D.parse(Name);
@@ -2364,9 +2353,8 @@ char *llvm::microsoftDemangle(const char *MangledName, size_t *NMangled,
int InternalStatus = demangle_success;
if (D.Error)
InternalStatus = demangle_invalid_mangled_name;
- else if (!initializeOutputBuffer(Buf, N, OB, 1024))
- InternalStatus = demangle_memory_alloc_failure;
else {
+ OutputBuffer OB(Buf, N);
AST->output(OB, OF);
OB += '\0';
if (N != nullptr)