Bug 1414068 - SlicedInputStream should be marked as closed when the underyling stream returns NS_BASE_STREAM_CLOSED, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Mon, 06 Nov 2017 14:22:47 +0100
changeset 390382 81effafd00ce
parent 390381 7c79d729d27e
child 390383 f66d8bc5ad30
push id32828
push userccoroiu@mozilla.com
push dateMon, 06 Nov 2017 23:06:05 +0000
treeherdermozilla-central@c2fe4b3b1b93 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1414068
milestone58.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 1414068 - SlicedInputStream should be marked as closed when the underyling stream returns NS_BASE_STREAM_CLOSED, r=smaug
xpcom/io/SlicedInputStream.cpp
--- a/xpcom/io/SlicedInputStream.cpp
+++ b/xpcom/io/SlicedInputStream.cpp
@@ -111,16 +111,21 @@ SlicedInputStream::Available(uint64_t* a
 {
   NS_ENSURE_STATE(mInputStream);
 
   if (mClosed) {
     return NS_BASE_STREAM_CLOSED;
   }
 
   nsresult rv = mInputStream->Available(aLength);
+  if (rv == NS_BASE_STREAM_CLOSED) {
+    mClosed = true;
+    return rv;
+  }
+
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   // Let's remove extra length from the end.
   if (*aLength + mCurPos > mStart + mLength) {
     *aLength -= XPCOM_MIN(*aLength, (*aLength + mCurPos) - (mStart + mLength));
   }
@@ -154,17 +159,22 @@ SlicedInputStream::Read(char* aBuffer, u
 
       mCurPos = mStart;
     } else {
       char buf[4096];
       while (mCurPos < mStart) {
         uint32_t bytesRead;
         uint64_t bufCount = XPCOM_MIN(mStart - mCurPos, (uint64_t)sizeof(buf));
         nsresult rv = mInputStream->Read(buf, bufCount, &bytesRead);
-        if (NS_WARN_IF(NS_FAILED(rv)) || bytesRead == 0) {
+        if (NS_SUCCEEDED(rv) && bytesRead == 0) {
+          mClosed = true;
+          return rv;
+        }
+
+        if (NS_WARN_IF(NS_FAILED(rv))) {
           return rv;
         }
 
          mCurPos += bytesRead;
       }
     }
   }
 
@@ -174,17 +184,22 @@ SlicedInputStream::Read(char* aBuffer, u
   }
 
   // Nothing else to read.
   if (!aCount) {
     return NS_OK;
   }
 
   nsresult rv = mInputStream->Read(aBuffer, aCount, aReadCount);
-  if (NS_WARN_IF(NS_FAILED(rv)) || *aReadCount == 0) {
+  if (NS_SUCCEEDED(rv) && *aReadCount == 0) {
+    mClosed = true;
+    return rv;
+  }
+
+  if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   mCurPos += *aReadCount;
   return NS_OK;
 }
 
 NS_IMETHODIMP
@@ -304,22 +319,27 @@ SlicedInputStream::OnInputStreamReady(ns
   }
 
   if (mCurPos < mStart) {
     char buf[4096];
     while (mCurPos < mStart) {
       uint32_t bytesRead;
       uint64_t bufCount = XPCOM_MIN(mStart - mCurPos, (uint64_t)sizeof(buf));
       nsresult rv = mInputStream->Read(buf, bufCount, &bytesRead);
+      if (NS_SUCCEEDED(rv) && bytesRead == 0) {
+        mClosed = true;
+        return RunAsyncWaitCallback();
+      }
+
       if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
         return mWeakAsyncInputStream->AsyncWait(this, 0, mStart - mCurPos,
                                                 mAsyncWaitEventTarget);
       }
 
-      if (NS_WARN_IF(NS_FAILED(rv)) || bytesRead == 0) {
+      if (NS_WARN_IF(NS_FAILED(rv))) {
         return RunAsyncWaitCallback();
       }
 
       mCurPos += bytesRead;
     }
 
     // Now we are ready to do the 'real' asyncWait.
     return mWeakAsyncInputStream->AsyncWait(this, mAsyncWaitFlags,
@@ -422,16 +442,21 @@ SlicedInputStream::Seek(int32_t aWhence,
       break;
     case NS_SEEK_CUR:
       // mCurPos could be lower than mStart if the reading has not started yet.
       offset = XPCOM_MAX(mStart, mCurPos) + aOffset;
       break;
     case NS_SEEK_END: {
       uint64_t available;
       rv = mInputStream->Available(&available);
+      if (rv == NS_BASE_STREAM_CLOSED) {
+        mClosed = true;
+        return rv;
+      }
+
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
 
       offset = XPCOM_MIN(mStart + mLength, available) + aOffset;
       break;
     }
     default: