Bug 710968 - Updater incorrectly checks fread() retval. r=bbondy
authorJared Wein <jwein@mozilla.com>
Thu, 12 Jan 2012 16:38:20 -0800
changeset 84394 ed19305f89e68a735422c82dd81ff7c10ec3687d
parent 84393 1798c955b58a34cd143e7dcbfca1fed7eb5b1786
child 84395 9b5f1ccdb02163d837e0141de5ccb6e12a07ddf8
push id21843
push usertim.taubert@gmx.de
push dateFri, 13 Jan 2012 10:45:53 +0000
treeherdermozilla-central@9b5f1ccdb021 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbbondy
bugs710968
milestone12.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 710968 - Updater incorrectly checks fread() retval. r=bbondy
toolkit/mozapps/update/updater/bspatch.cpp
toolkit/mozapps/update/updater/updater.cpp
--- a/toolkit/mozapps/update/updater/bspatch.cpp
+++ b/toolkit/mozapps/update/updater/bspatch.cpp
@@ -98,28 +98,25 @@ MBS_ApplyPatch(const MBSPatchHeader *hea
   if (!buf)
     return MEM_ERROR;
 
   int rv = OK;
 
   size_t r = header->cblen + header->difflen + header->extralen;
   unsigned char *wb = buf;
   while (r) {
-    size_t c = fread(wb, 1, (r > SSIZE_MAX) ? SSIZE_MAX : r, patchFile);
-    if (c < 0) {
+    const size_t count = (r > SSIZE_MAX) ? SSIZE_MAX : r;
+    size_t c = fread(wb, 1, count, patchFile);
+    if (c != count) {
       rv = READ_ERROR;
       goto end;
     }
 
     r -= c;
-
-    if (c == 0 && r) {
-      rv = UNEXPECTED_ERROR;
-      goto end;
-    }
+    wb += c;
   }
 
   {
     MBSPatchTriple *ctrlsrc = (MBSPatchTriple*) buf;
     unsigned char *diffsrc = buf + header->cblen;
     unsigned char *extrasrc = diffsrc + header->difflen;
 
     MBSPatchTriple *ctrlend = (MBSPatchTriple*) diffsrc;
--- a/toolkit/mozapps/update/updater/updater.cpp
+++ b/toolkit/mozapps/update/updater/updater.cpp
@@ -1033,30 +1033,26 @@ PatchFile::LoadSourceFile(FILE* ofile)
 
   buf = (unsigned char *) malloc(header.slen);
   if (!buf)
     return MEM_ERROR;
 
   size_t r = header.slen;
   unsigned char *rb = buf;
   while (r) {
-    size_t c = fread(rb, 1, r, ofile);
-    if (c < 0) {
+    const size_t count = mmin(SSIZE_MAX, r);
+    size_t c = fread(rb, 1, count, ofile);
+    if (c != count) {
       LOG(("LoadSourceFile: error reading destination file: " LOG_S "\n",
            mFile));
       return READ_ERROR;
     }
 
     r -= c;
     rb += c;
-
-    if (c == 0 && r) {
-      LOG(("LoadSourceFile: expected %d more bytes in destination file\n", r));
-      return UNEXPECTED_ERROR;
-    }
   }
 
   // Verify that the contents of the source file correspond to what we expect.
 
   unsigned int crc = crc32(buf, header.slen);
 
   if (crc != header.scrc32) {
     LOG(("LoadSourceFile: destination file crc %d does not match expected " \
@@ -2400,27 +2396,25 @@ GetManifestContents(const NS_tchar *mani
 
   char *mbuf = (char *) malloc(ms.st_size + 1);
   if (!mbuf)
     return NULL;
 
   size_t r = ms.st_size;
   char *rb = mbuf;
   while (r) {
-    size_t c = fread(rb, 1, mmin(SSIZE_MAX, r), mfile);
-    if (c < 0) {
+    const size_t count = mmin(SSIZE_MAX, r);
+    size_t c = fread(rb, 1, count, mfile);
+    if (c != count) {
       LOG(("GetManifestContents: error reading manifest file: " LOG_S "\n", manifest));
       return NULL;
     }
 
     r -= c;
     rb += c;
-
-    if (c == 0 && r)
-      return NULL;
   }
   mbuf[ms.st_size] = '\0';
   rb = mbuf;
 
 #ifndef XP_WIN
   return rb;
 #else
   NS_tchar *wrb = (NS_tchar *) malloc((ms.st_size + 1) * sizeof(NS_tchar));