llvm-dwp: More error handling around invalid compressed sections

llvm-svn: 270466
diff --git a/llvm/test/tools/llvm-dwp/Inputs/empty_compressed_section.dwo b/llvm/test/tools/llvm-dwp/Inputs/empty_compressed_section.dwo
new file mode 100644
index 0000000..bc9b4ff
--- /dev/null
+++ b/llvm/test/tools/llvm-dwp/Inputs/empty_compressed_section.dwo
Binary files differ
diff --git a/llvm/test/tools/llvm-dwp/Inputs/invalid_compressed.dwo b/llvm/test/tools/llvm-dwp/Inputs/invalid_compressed.dwo
new file mode 100644
index 0000000..0cbfd14
--- /dev/null
+++ b/llvm/test/tools/llvm-dwp/Inputs/invalid_compressed.dwo
Binary files differ
diff --git a/llvm/test/tools/llvm-dwp/X86/compressfail.test b/llvm/test/tools/llvm-dwp/X86/compressfail.test
index d9f50cb..78b6255 100644
--- a/llvm/test/tools/llvm-dwp/X86/compressfail.test
+++ b/llvm/test/tools/llvm-dwp/X86/compressfail.test
@@ -1,5 +1,7 @@
 RUN: not llvm-dwp %p/../Inputs/compressfail/a.dwo -o %t 2>&1 | FileCheck %s
+RUN: not llvm-dwp %p/../Inputs/empty_compressed_section.dwo -o %t 2>&1 | FileCheck %s
+RUN: not llvm-dwp %p/../Inputs/invalid_compressed.dwo -o %t 2>&1 | FileCheck %s
 
 REQUIRES: zlib
 
-CHECK: error: failure while decompressing compressed section: 'zdebug_info.dwo'
+CHECK: error: failure while decompressing compressed section: 'zdebug_{{.*}}.dwo'
diff --git a/llvm/tools/llvm-dwp/llvm-dwp.cpp b/llvm/tools/llvm-dwp/llvm-dwp.cpp
index b88f0ef..1ce6a14 100644
--- a/llvm/tools/llvm-dwp/llvm-dwp.cpp
+++ b/llvm/tools/llvm-dwp/llvm-dwp.cpp
@@ -432,20 +432,17 @@
         return errorCodeToError(Err);
 
       if (Name.startswith("zdebug_")) {
+        UncompressedSections.emplace_back();
         uint64_t OriginalSize;
         if (!zlib::isAvailable())
           return make_error<DWPError>("zlib not available");
-        if (!consumeCompressedDebugSectionHeader(Contents, OriginalSize))
+        if (!consumeCompressedDebugSectionHeader(Contents, OriginalSize) ||
+            zlib::uncompress(Contents, UncompressedSections.back(),
+                             OriginalSize) != zlib::StatusOK)
           return make_error<DWPError>(
               ("failure while decompressing compressed section: '" + Name +
                "\'")
                   .str());
-        UncompressedSections.resize(UncompressedSections.size() + 1);
-        if (zlib::uncompress(Contents, UncompressedSections.back(),
-                             OriginalSize) != zlib::StatusOK) {
-          UncompressedSections.pop_back();
-          continue;
-        }
         Name = Name.substr(1);
         Contents = UncompressedSections.back();
       }