Bug 541828 - Do not crash on small invalid jar files r=alfredkayser
authorTaras Glek <tglek@mozilla.com>
Fri, 26 Mar 2010 10:19:53 -0700
changeset 40152 ebe3a63d03d3a50ec43221efe68ae52e24f75e88
parent 40151 47f0b03385025499de074428aaacc973d51ed785
child 40153 cec7b12eb5e30a59bc9c621662fbe192000411f9
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersalfredkayser
bugs541828
milestone1.9.3a4pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
Bug 541828 - Do not crash on small invalid jar files r=alfredkayser
modules/libjar/nsZipArchive.cpp
modules/libjar/test/unit/data/test_corrupt2.zip
modules/libjar/test/unit/test_corrupt_541828.js
--- a/modules/libjar/nsZipArchive.cpp
+++ b/modules/libjar/nsZipArchive.cpp
@@ -521,25 +521,27 @@ nsZipItem* nsZipArchive::CreateZipItem()
 //---------------------------------------------
 nsresult nsZipArchive::BuildFileList()
 {
   // Get archive size using end pos
   PRUint8* buf;
   PRUint8* startp = mFd->mFileData;
   PRUint8* endp = startp + mFd->mLen;
 
-  for (buf = endp - ZIPEND_SIZE; xtolong(buf) != ENDSIG; buf--)
+  PRUint32 centralOffset = 0;
+  for (buf = endp - ZIPEND_SIZE; buf > startp; buf--)
   {
-    if (buf == startp) {
-      // We're at the beginning of the file, and still no sign
-      // of the end signature.  File must be corrupted!
-      return NS_ERROR_FILE_CORRUPTED;
+    if (xtolong(buf) == ENDSIG) {
+      centralOffset = xtolong(((ZipEnd *)buf)->offset_central_dir);
+      break;
     }
   }
-  PRUint32 centralOffset = xtolong(((ZipEnd *)buf)->offset_central_dir);
+
+  if (!centralOffset)
+    return NS_ERROR_FILE_CORRUPTED;
 
   //-- Read the central directory headers
   buf = startp + centralOffset;
   if (endp - buf < PRInt32(sizeof(PRUint32)))
       return NS_ERROR_FILE_CORRUPTED;
   PRUint32 sig = xtolong(buf);
   while (sig == CENTRALSIG) {
     // Make sure there is enough data available.
new file mode 100644
--- /dev/null
+++ b/modules/libjar/test/unit/data/test_corrupt2.zip
@@ -0,0 +1,1 @@
+2
new file mode 100644
--- /dev/null
+++ b/modules/libjar/test/unit/test_corrupt_541828.js
@@ -0,0 +1,57 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org code.
+ *
+ * The Initial Developer of the Original Code is
+ * Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ * 
+ * Contributor(s):
+ *  Taras Glek <tglek@mozilla.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+// Check that files can be read from after closing zipreader
+function run_test() {
+  const Cc = Components.classes;
+  const Ci = Components.interfaces;
+
+  // the build script have created the zip we can test on in the current dir.
+  var file = do_get_file("data/test_corrupt2.zip");
+
+  var zipreader = Cc["@mozilla.org/libjar/zip-reader;1"].
+                  createInstance(Ci.nsIZipReader);
+  var failed = false;
+  try {
+    zipreader.open(file);
+    // corrupt files should trigger an exception
+  } catch (ex) {
+    failed = true;
+  }
+  do_check_true(failed);
+}
+