CloseArchive() to free memory when OpenArchive fails.

Bug: 26962895
Change-Id: I42418eee320ddae857b42572690316c53f638e85
diff --git a/linker/linker.cpp b/linker/linker.cpp
index c07b21d..724b6a1 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -379,13 +379,21 @@
         }
 
         ZipArchiveHandle handle = nullptr;
+        void* cookie = nullptr;
+        auto zip_guard = make_scope_guard([&]() {
+          if (cookie != nullptr) {
+            EndIteration(cookie);
+          }
+          if (handle != nullptr) {
+            CloseArchive(handle);
+          }
+        });
         if (OpenArchive(resolved_path, &handle) != 0) {
           DL_WARN("Warning: unable to open zip archive: %s", resolved_path);
           continue;
         }
 
         // Check if zip-file has a dir with entry_path name
-        void* cookie = nullptr;
         std::string prefix_str = entry_path + "/";
         ZipString prefix(prefix_str.c_str());
 
@@ -406,13 +414,6 @@
           continue;
         }
 
-        auto zip_guard = make_scope_guard([&]() {
-          if (cookie != nullptr) {
-            EndIteration(cookie);
-          }
-          CloseArchive(handle);
-        });
-
         resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path);
       }
     }
@@ -1382,6 +1383,7 @@
 
   if (OpenArchiveFd(fd, "", handle) != 0) {
     // invalid zip-file (?)
+    CloseArchive(handle);
     close(fd);
     return false;
   }
diff --git a/tests/libs/bionic_tests_zipalign.cpp b/tests/libs/bionic_tests_zipalign.cpp
index dbbfd7c..a72820f 100644
--- a/tests/libs/bionic_tests_zipalign.cpp
+++ b/tests/libs/bionic_tests_zipalign.cpp
@@ -138,6 +138,7 @@
 
   int32_t return_value = OpenArchive(argv[2], &handle);
   if (return_value != 0) {
+    CloseArchive(handle);
     fprintf(stderr, "Unable to open '%s': %s\n", argv[2], ErrorCodeString(return_value));
     return false;
   }