llvm-undname: Attempt to fix leak-on-invalid found by oss-fuzz
authorNico Weber <nicolasweber@gmx.de>
Fri, 19 Apr 2019 14:13:11 +0000 (14:13 +0000)
committerNico Weber <nicolasweber@gmx.de>
Fri, 19 Apr 2019 14:13:11 +0000 (14:13 +0000)
llvm-svn: 358760

llvm/lib/Demangle/MicrosoftDemangle.cpp

index 3fb8e0e..ebe2ef5 100644 (file)
@@ -1230,6 +1230,11 @@ Demangler::demangleStringLiteral(StringView &MangledName) {
 
   EncodedStringLiteralNode *Result = Arena.alloc<EncodedStringLiteralNode>();
 
+  // Must happen before the first `goto StringLiteralError`.
+  if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
+    // FIXME: Propagate out-of-memory as an error?
+    std::terminate();
+
   // Prefix indicating the beginning of a string literal
   if (!MangledName.consumeFront("@_"))
     goto StringLiteralError;
@@ -1261,9 +1266,6 @@ Demangler::demangleStringLiteral(StringView &MangledName) {
   if (MangledName.empty())
     goto StringLiteralError;
 
-  if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
-    // FIXME: Propagate out-of-memory as an error?
-    std::terminate();
   if (IsWcharT) {
     Result->Char = CharKind::Wchar;
     if (StringByteSize > 64)
@@ -1328,6 +1330,7 @@ Demangler::demangleStringLiteral(StringView &MangledName) {
 
 StringLiteralError:
   Error = true;
+  std::free(OS.getBuffer());
   return nullptr;
 }