[llvm] r311625 - Fix bug 34051 by handling empty .res files gracefully.

Eric Beckmann via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 23 19:36:50 PDT 2017


Author: ecbeckmann
Date: Wed Aug 23 19:36:50 2017
New Revision: 311625

URL: http://llvm.org/viewvc/llvm-project?rev=311625&view=rev
Log:
Fix bug 34051 by handling empty .res files gracefully.

Summary:
Previously, llvm-cvtres crashes on .res files which are empty except for
the null header.  This allows the library to simply pass over them.

Subscribers: llvm-commits, hiraditya

Differential Revision: https://reviews.llvm.org/D37044

Modified:
    llvm/trunk/include/llvm/Object/WindowsResource.h
    llvm/trunk/lib/Object/WindowsResource.cpp

Modified: llvm/trunk/include/llvm/Object/WindowsResource.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/WindowsResource.h?rev=311625&r1=311624&r2=311625&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/WindowsResource.h (original)
+++ llvm/trunk/include/llvm/Object/WindowsResource.h Wed Aug 23 19:36:50 2017
@@ -85,6 +85,12 @@ struct WinResHeaderSuffix {
   support::ulittle32_t Characteristics;
 };
 
+class EmptyResError : public GenericBinaryError {
+public:
+  EmptyResError(Twine Msg, object_error ECOverride)
+      : GenericBinaryError(Msg, ECOverride) {}
+};
+
 class ResourceEntryRef {
 public:
   Error moveNext(bool &End);
@@ -103,11 +109,12 @@ public:
 private:
   friend class WindowsResource;
 
-  ResourceEntryRef(BinaryStreamRef Ref, const WindowsResource *Owner,
-                   Error &Err);
-
+  ResourceEntryRef(BinaryStreamRef Ref, const WindowsResource *Owner);
   Error loadNext();
 
+  static Expected<ResourceEntryRef> create(BinaryStreamRef Ref,
+                                           const WindowsResource *Owner);
+
   BinaryStreamReader Reader;
   bool IsStringType;
   ArrayRef<UTF16> Type;

Modified: llvm/trunk/lib/Object/WindowsResource.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/WindowsResource.cpp?rev=311625&r1=311624&r2=311625&view=diff
==============================================================================
--- llvm/trunk/lib/Object/WindowsResource.cpp (original)
+++ llvm/trunk/lib/Object/WindowsResource.cpp Wed Aug 23 19:36:50 2017
@@ -57,19 +57,22 @@ WindowsResource::createWindowsResource(M
 }
 
 Expected<ResourceEntryRef> WindowsResource::getHeadEntry() {
-  Error Err = Error::success();
-  auto Ref = ResourceEntryRef(BinaryStreamRef(BBS), this, Err);
-  if (Err)
-    return std::move(Err);
-  return Ref;
+  if (BBS.getLength() < sizeof(WinResHeaderPrefix) + sizeof(WinResHeaderSuffix))
+    return make_error<EmptyResError>(".res contains no entries",
+                                     object_error::unexpected_eof);
+  return ResourceEntryRef::create(BinaryStreamRef(BBS), this);
 }
 
 ResourceEntryRef::ResourceEntryRef(BinaryStreamRef Ref,
-                                   const WindowsResource *Owner, Error &Err)
-    : Reader(Ref), OwningRes(Owner) {
-  if (loadNext())
-    Err = make_error<GenericBinaryError>("Could not read first entry.\n",
-                                         object_error::unexpected_eof);
+                                   const WindowsResource *Owner)
+    : Reader(Ref), OwningRes(Owner) {}
+
+Expected<ResourceEntryRef>
+ResourceEntryRef::create(BinaryStreamRef BSR, const WindowsResource *Owner) {
+  auto Ref = ResourceEntryRef(BSR, Owner);
+  if (auto E = Ref.loadNext())
+    return std::move(E);
+  return Ref;
 }
 
 Error ResourceEntryRef::moveNext(bool &End) {
@@ -127,8 +130,20 @@ WindowsResourceParser::WindowsResourcePa
 
 Error WindowsResourceParser::parse(WindowsResource *WR) {
   auto EntryOrErr = WR->getHeadEntry();
-  if (!EntryOrErr)
-    return EntryOrErr.takeError();
+  if (!EntryOrErr) {
+    auto E = EntryOrErr.takeError();
+    if (E.isA<EmptyResError>()) {
+      // Check if the .res file contains no entries.  In this case we don't have
+      // to throw an error but can rather just return without parsing anything.
+      // This applies for files which have a valid PE header magic and the
+      // mandatory empty null resource entry.  Files which do not fit this
+      // criteria would have already been filtered out by
+      // WindowsResource::createWindowsResource().
+      consumeError(std::move(E));
+      return Error::success();
+    }
+    return E;
+  }
 
   ResourceEntryRef Entry = EntryOrErr.get();
   bool End = false;




More information about the llvm-commits mailing list