Bug 857830: Part 1 - Ensure that ReadAheadFile's aOutFd is always set when provided. r=glandium
authorAaron Klotz <aklotz@mozilla.com>
Tue, 07 May 2013 00:06:14 -0600
changeset 131099 e1f30dbb81086b17a8a3ff4d97d80b8c0f9b7636
parent 131098 7d3a05e286a6c5e04df7155813a0630b93fd3e09
child 131100 abaf789d8030afc528c0e7f636b8689fba0aa981
push id24649
push userryanvm@gmail.com
push dateWed, 08 May 2013 02:10:32 +0000
treeherdermozilla-central@b980d32c366f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs857830
milestone23.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 857830: Part 1 - Ensure that ReadAheadFile's aOutFd is always set when provided. r=glandium
xpcom/glue/FileUtils.cpp
--- a/xpcom/glue/FileUtils.cpp
+++ b/xpcom/glue/FileUtils.cpp
@@ -476,47 +476,61 @@ mozilla::ReadAheadLib(mozilla::pathstr_t
   }
 #endif
 }
 
 void
 mozilla::ReadAheadFile(mozilla::pathstr_t aFilePath, const size_t aOffset,
                        const size_t aCount, mozilla::filedesc_t* aOutFd)
 {
+#if defined(XP_WIN)
   if (!aFilePath) {
+    if (aOutFd) {
+      *aOutFd = INVALID_HANDLE_VALUE;
+    }
     return;
   }
-#if defined(XP_WIN)
   HANDLE fd = CreateFileW(aFilePath, GENERIC_READ, FILE_SHARE_READ,
                           NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
+  if (aOutFd) {
+    *aOutFd = fd;
+  }
   if (fd == INVALID_HANDLE_VALUE) {
     return;
   }
   ReadAhead(fd, aOffset, aCount);
-  if (aOutFd) {
-    *aOutFd = fd;
-  } else {
+  if (!aOutFd) {
     CloseHandle(fd);
   }
 #elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX)
+  if (!aFilePath) {
+    if (aOutFd) {
+      *aOutFd = -1;
+    }
+    return;
+  }
   int fd = open(aFilePath, O_RDONLY);
+  if (aOutFd) {
+    *aOutFd = fd;
+  }
   if (fd < 0) {
     return;
   }
   size_t count;
   if (aCount == SIZE_MAX) {
     struct stat st;
     if (fstat(fd, &st) < 0) {
+      if (!aOutFd) {
+        close(fd);
+      }
       return;
     }
     count = st.st_size;
   } else {
     count = aCount;
   }
   ReadAhead(fd, aOffset, count);
-  if (aOutFd) {
-    *aOutFd = fd;
-  } else {
+  if (!aOutFd) {
     close(fd);
   }
 #endif
 }