From fe04c2050c4fff8ba4c61c9b59e9e0954cd6a5e1 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Tue, 17 Oct 2017 20:38:05 +0200 Subject: [PATCH] Bug 1409329 - NS_NewBufferedOutputStream should take the ownership of the outputStream, r=smaug --- dom/webbrowserpersist/nsWebBrowserPersist.cpp | 5 ++-- dom/workers/ServiceWorkerEvents.cpp | 4 +-- .../spellcheck/src/mozPersonalDictionary.cpp | 2 +- modules/libjar/zipwriter/nsZipWriter.cpp | 4 +-- modules/libpref/Preferences.cpp | 2 +- netwerk/base/BackgroundFileSaver.cpp | 7 ++--- netwerk/base/nsNetUtil.cpp | 28 +++++-------------- netwerk/base/nsNetUtil.h | 20 ++----------- netwerk/cache/nsDiskCacheDeviceSQL.cpp | 3 +- rdf/base/nsRDFXMLDataSource.cpp | 3 +- .../manager/ssl/nsCertOverrideService.cpp | 3 +- .../VariableLengthPrefixSet.cpp | 6 ++-- .../nsUrlClassifierPrefixSet.cpp | 6 ++-- 13 files changed, 36 insertions(+), 57 deletions(-) diff --git a/dom/webbrowserpersist/nsWebBrowserPersist.cpp b/dom/webbrowserpersist/nsWebBrowserPersist.cpp index a5971216d8b4..9c03e17c0d7c 100644 --- a/dom/webbrowserpersist/nsWebBrowserPersist.cpp +++ b/dom/webbrowserpersist/nsWebBrowserPersist.cpp @@ -2321,8 +2321,9 @@ nsWebBrowserPersist::MakeOutputStreamFromFile( rv = fileOutputStream->Init(aFile, ioFlags, -1, 0); NS_ENSURE_SUCCESS(rv, rv); - *aOutputStream = NS_BufferOutputStream(fileOutputStream, - BUFFERED_OUTPUT_SIZE).take(); + rv = NS_NewBufferedOutputStream(aOutputStream, fileOutputStream.forget(), + BUFFERED_OUTPUT_SIZE); + NS_ENSURE_SUCCESS(rv, rv); if (mPersistFlags & PERSIST_FLAGS_CLEANUP_ON_FAILURE) { diff --git a/dom/workers/ServiceWorkerEvents.cpp b/dom/workers/ServiceWorkerEvents.cpp index 054dbede07c6..bcf3c01d75e3 100644 --- a/dom/workers/ServiceWorkerEvents.cpp +++ b/dom/workers/ServiceWorkerEvents.cpp @@ -664,8 +664,8 @@ RespondWithHandler::ResolvedCallback(JSContext* aCx, JS::Handle aValu // objects which may or many not play well with NS_InputStreamIsBuffered(). if (!NS_OutputStreamIsBuffered(responseBody)) { nsCOMPtr buffered; - rv = NS_NewBufferedOutputStream(getter_AddRefs(buffered), responseBody, - kCopySegmentSize); + rv = NS_NewBufferedOutputStream(getter_AddRefs(buffered), + responseBody.forget(), kCopySegmentSize); if (NS_WARN_IF(NS_FAILED(rv))) { return; } diff --git a/extensions/spellcheck/src/mozPersonalDictionary.cpp b/extensions/spellcheck/src/mozPersonalDictionary.cpp index 9b1c8c325533..d0f601e0df8a 100644 --- a/extensions/spellcheck/src/mozPersonalDictionary.cpp +++ b/extensions/spellcheck/src/mozPersonalDictionary.cpp @@ -105,7 +105,7 @@ public: // Get a buffered output stream 4096 bytes big, to optimize writes. nsCOMPtr bufferedOutputStream; res = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOutputStream), - outStream, 4096); + outStream.forget(), 4096); if (NS_FAILED(res)) { return res; } diff --git a/modules/libjar/zipwriter/nsZipWriter.cpp b/modules/libjar/zipwriter/nsZipWriter.cpp index 894189dd1b18..3f9cbc0b7641 100644 --- a/modules/libjar/zipwriter/nsZipWriter.cpp +++ b/modules/libjar/zipwriter/nsZipWriter.cpp @@ -270,9 +270,9 @@ NS_IMETHODIMP nsZipWriter::Open(nsIFile *aFile, int32_t aIoFlags) return rv; } - rv = NS_NewBufferedOutputStream(getter_AddRefs(mStream), stream, 64 * 1024); + rv = NS_NewBufferedOutputStream(getter_AddRefs(mStream), stream.forget(), + 64 * 1024); if (NS_FAILED(rv)) { - stream->Close(); mHeaders.Clear(); mEntryHash.Clear(); return rv; diff --git a/modules/libpref/Preferences.cpp b/modules/libpref/Preferences.cpp index 4a16180bc9bb..2ac90ed624aa 100644 --- a/modules/libpref/Preferences.cpp +++ b/modules/libpref/Preferences.cpp @@ -3534,7 +3534,7 @@ public: } rv = NS_NewBufferedOutputStream( - getter_AddRefs(outStream), outStreamSink, 4096); + getter_AddRefs(outStream), outStreamSink.forget(), 4096); if (NS_FAILED(rv)) { return rv; } diff --git a/netwerk/base/BackgroundFileSaver.cpp b/netwerk/base/BackgroundFileSaver.cpp index bf7d31fc1259..6f167a4bbcc2 100755 --- a/netwerk/base/BackgroundFileSaver.cpp +++ b/netwerk/base/BackgroundFileSaver.cpp @@ -620,10 +620,9 @@ BackgroundFileSaver::ProcessStateChange() PR_WRONLY | creationIoFlags, 0600); NS_ENSURE_SUCCESS(rv, rv); - outputStream = NS_BufferOutputStream(outputStream, BUFFERED_IO_SIZE); - if (!outputStream) { - return NS_ERROR_FAILURE; - } + rv = NS_NewBufferedOutputStream(getter_AddRefs(outputStream), + outputStream.forget(), BUFFERED_IO_SIZE); + NS_ENSURE_SUCCESS(rv, rv); // Wrap the output stream so that it feeds the digest context if needed. if (mDigestContext) { diff --git a/netwerk/base/nsNetUtil.cpp b/netwerk/base/nsNetUtil.cpp index 78b43c48a310..7181d9116e15 100644 --- a/netwerk/base/nsNetUtil.cpp +++ b/netwerk/base/nsNetUtil.cpp @@ -1337,38 +1337,24 @@ NS_NewLocalFileStream(nsIFileStream **result, } nsresult -NS_NewBufferedOutputStream(nsIOutputStream **result, - nsIOutputStream *str, - uint32_t bufferSize) +NS_NewBufferedOutputStream(nsIOutputStream** aResult, + already_AddRefed aOutputStream, + uint32_t aBufferSize) { + nsCOMPtr outputStream = Move(aOutputStream); + nsresult rv; nsCOMPtr out = do_CreateInstance(NS_BUFFEREDOUTPUTSTREAM_CONTRACTID, &rv); if (NS_SUCCEEDED(rv)) { - rv = out->Init(str, bufferSize); + rv = out->Init(outputStream, aBufferSize); if (NS_SUCCEEDED(rv)) { - out.forget(result); + out.forget(aResult); } } return rv; } -already_AddRefed -NS_BufferOutputStream(nsIOutputStream *aOutputStream, - uint32_t aBufferSize) -{ - NS_ASSERTION(aOutputStream, "No output stream given!"); - - nsCOMPtr bos; - nsresult rv = NS_NewBufferedOutputStream(getter_AddRefs(bos), aOutputStream, - aBufferSize); - if (NS_SUCCEEDED(rv)) - return bos.forget(); - - bos = aOutputStream; - return bos.forget(); -} - MOZ_MUST_USE nsresult NS_NewBufferedInputStream(nsIInputStream **result, nsIInputStream *str, diff --git a/netwerk/base/nsNetUtil.h b/netwerk/base/nsNetUtil.h index 10d35d8aca0f..82b9683a32b3 100644 --- a/netwerk/base/nsNetUtil.h +++ b/netwerk/base/nsNetUtil.h @@ -517,24 +517,10 @@ NS_NewBufferedInputStream(nsIInputStream **result, // note: the resulting stream can be QI'ed to nsISafeOutputStream iff the // provided stream supports it. -nsresult NS_NewBufferedOutputStream(nsIOutputStream **result, - nsIOutputStream *str, - uint32_t bufferSize); +nsresult NS_NewBufferedOutputStream(nsIOutputStream** aResult, + already_AddRefed aOutputStream, + uint32_t aBufferSize); -/** - * Attempts to buffer a given stream. If this fails, it returns the - * passed-in stream. - * - * @param aOutputStream - * The output stream we want to buffer. This cannot be null. - * @param aBufferSize - * The size of the buffer for the buffered output stream. - * @returns an nsIOutputStream that is buffered with the specified buffer size, - * or is aOutputStream if creating the new buffered stream failed. - */ -already_AddRefed -NS_BufferOutputStream(nsIOutputStream *aOutputStream, - uint32_t aBufferSize); already_AddRefed NS_BufferInputStream(nsIInputStream *aInputStream, uint32_t aBufferSize); diff --git a/netwerk/cache/nsDiskCacheDeviceSQL.cpp b/netwerk/cache/nsDiskCacheDeviceSQL.cpp index 521493edfbd4..a1b6113f722a 100644 --- a/netwerk/cache/nsDiskCacheDeviceSQL.cpp +++ b/netwerk/cache/nsDiskCacheDeviceSQL.cpp @@ -1815,7 +1815,8 @@ nsOfflineCacheDevice::OpenOutputStreamForEntry(nsCacheEntry *entry, nsCOMPtr bufferedOut; nsresult rv = - NS_NewBufferedOutputStream(getter_AddRefs(bufferedOut), out, 16 * 1024); + NS_NewBufferedOutputStream(getter_AddRefs(bufferedOut), out.forget(), + 16 * 1024); NS_ENSURE_SUCCESS(rv, rv); bufferedOut.swap(*result); diff --git a/rdf/base/nsRDFXMLDataSource.cpp b/rdf/base/nsRDFXMLDataSource.cpp index 70c668adb829..6c342bbb0559 100644 --- a/rdf/base/nsRDFXMLDataSource.cpp +++ b/rdf/base/nsRDFXMLDataSource.cpp @@ -766,7 +766,8 @@ RDFXMLDataSourceImpl::rdfXMLFlush(nsIURI *aURI) if (NS_FAILED(rv)) return rv; nsCOMPtr bufferedOut; - rv = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOut), out, 4096); + rv = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOut), + out.forget(), 4096); if (NS_FAILED(rv)) return rv; rv = Serialize(bufferedOut); diff --git a/security/manager/ssl/nsCertOverrideService.cpp b/security/manager/ssl/nsCertOverrideService.cpp index a9f5f3e1711f..d98748a4d5d2 100644 --- a/security/manager/ssl/nsCertOverrideService.cpp +++ b/security/manager/ssl/nsCertOverrideService.cpp @@ -281,7 +281,8 @@ nsCertOverrideService::Write(const MutexAutoLock& aProofOfLock) // get a buffered output stream 4096 bytes big, to optimize writes nsCOMPtr bufferedOutputStream; - rv = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOutputStream), fileOutputStream, 4096); + rv = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOutputStream), + fileOutputStream.forget(), 4096); if (NS_FAILED(rv)) { return rv; } diff --git a/toolkit/components/url-classifier/VariableLengthPrefixSet.cpp b/toolkit/components/url-classifier/VariableLengthPrefixSet.cpp index 7fcf006622c6..a82fd2111304 100644 --- a/toolkit/components/url-classifier/VariableLengthPrefixSet.cpp +++ b/toolkit/components/url-classifier/VariableLengthPrefixSet.cpp @@ -275,8 +275,10 @@ VariableLengthPrefixSet::StoreToFile(nsIFile* aFile) } // Convert to buffered stream - nsCOMPtr out = - NS_BufferOutputStream(localOutFile, std::min(fileSize, MAX_BUFFER_SIZE)); + nsCOMPtr out; + rv = NS_NewBufferedOutputStream(getter_AddRefs(out), localOutFile.forget(), + std::min(fileSize, MAX_BUFFER_SIZE)); + NS_ENSURE_SUCCESS(rv, rv); rv = mFixedPrefixSet->WritePrefixes(out); NS_ENSURE_SUCCESS(rv, rv); diff --git a/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.cpp b/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.cpp index 5dc0bda86d4e..b2401f3c5777 100644 --- a/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.cpp +++ b/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.cpp @@ -385,8 +385,10 @@ nsUrlClassifierPrefixSet::StoreToFile(nsIFile* aFile) } // Convert to buffered stream - nsCOMPtr out = - NS_BufferOutputStream(localOutFile, std::min(fileSize, MAX_BUFFER_SIZE)); + nsCOMPtr out; + rv = NS_NewBufferedOutputStream(getter_AddRefs(out), localOutFile.forget(), + std::min(fileSize, MAX_BUFFER_SIZE)); + NS_ENSURE_SUCCESS(rv, rv); rv = WritePrefixes(out); NS_ENSURE_SUCCESS(rv, rv);