Bug 965362 - Part 1: Make sure that nsScriptableInputStream::Read reads all of the allowed available bytes from the input stream; r=bsmedberg
authorEhsan Akhgari <ehsan@mozilla.com>
Thu, 27 Mar 2014 11:20:08 -0400
changeset 175644 03513b312aa5a8ea3aec1dc7deb1da7aa861bc55
parent 175643 8715ca8dd2317b02e90ab3699cb5acc34f0f9ab2
child 175645 852fe5a5434dd6a3598902c1115e63dd32b8566c
push id26496
push userkwierso@gmail.com
push dateFri, 28 Mar 2014 02:28:34 +0000
treeherdermozilla-central@3c09159e01da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs965362
milestone31.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 965362 - Part 1: Make sure that nsScriptableInputStream::Read reads all of the allowed available bytes from the input stream; r=bsmedberg
xpcom/io/nsScriptableInputStream.cpp
xpcom/io/nsScriptableInputStream.h
--- a/xpcom/io/nsScriptableInputStream.cpp
+++ b/xpcom/io/nsScriptableInputStream.cpp
@@ -40,58 +40,66 @@ nsScriptableInputStream::Read(uint32_t a
     rv = mInputStream->Available(&count64);
     if (NS_FAILED(rv)) return rv;
 
     // bug716556 - Ensure count+1 doesn't overflow
     uint32_t count = XPCOM_MIN((uint32_t)XPCOM_MIN<uint64_t>(count64, aCount), UINT32_MAX - 1);
     buffer = (char*)moz_malloc(count+1); // make room for '\0'
     if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
 
-    uint32_t amtRead = 0;
-    rv = mInputStream->Read(buffer, count, &amtRead);
+    rv = ReadHelper(buffer, count);
     if (NS_FAILED(rv)) {
         nsMemory::Free(buffer);
         return rv;
     }
 
-    buffer[amtRead] = '\0';
+    buffer[count] = '\0';
     *_retval = buffer;
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsScriptableInputStream::ReadBytes(uint32_t aCount, nsACString &_retval) {
     if (!mInputStream) {
       return NS_ERROR_NOT_INITIALIZED;
     }
 
     _retval.SetLength(aCount);
     if (_retval.Length() != aCount) {
       return NS_ERROR_OUT_OF_MEMORY;
     }
 
     char *ptr = _retval.BeginWriting();
+    nsresult rv = ReadHelper(ptr, aCount);
+    if (NS_FAILED(rv)) {
+      _retval.Truncate();
+    }
+    return rv;
+}
+
+nsresult
+nsScriptableInputStream::ReadHelper(char* aBuffer, uint32_t aCount)
+{
     uint32_t totalBytesRead = 0;
     while (1) {
       uint32_t bytesRead;
-      nsresult rv = mInputStream->Read(ptr + totalBytesRead,
+      nsresult rv = mInputStream->Read(aBuffer + totalBytesRead,
                                        aCount - totalBytesRead,
                                        &bytesRead);
       if (NS_FAILED(rv)) {
         return rv;
       }
 
       totalBytesRead += bytesRead;
       if (totalBytesRead == aCount) {
         break;
       }
 
       // If we have read zero bytes, we have hit EOF.
       if (bytesRead == 0) {
-        _retval.Truncate();
         return NS_ERROR_FAILURE;
       }
 
     }
     return NS_OK;
 }
 
 nsresult
--- a/xpcom/io/nsScriptableInputStream.h
+++ b/xpcom/io/nsScriptableInputStream.h
@@ -28,12 +28,14 @@ public:
     nsScriptableInputStream() {}
 
     static nsresult
     Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
 
 private:
     ~nsScriptableInputStream() {}
 
+    nsresult ReadHelper(char* aBuffer, uint32_t aCount);
+
     nsCOMPtr<nsIInputStream> mInputStream;
 };
 
 #endif // ___nsscriptableinputstream___h_