From d5703cb7ab27e97cb3d450b8b6131e2f83dcc63d Mon Sep 17 00:00:00 2001 From: Nathan Froyd Date: Thu, 18 Feb 2016 11:14:02 -0500 Subject: [PATCH] Bug 1249389 - part 1 - change NewObjectInputStreamFromBuffer to take a UniquePtr argument; r=erahm Because NewObjectInputStreamFromBuffer takes a raw pointer as input, the typical coding pattern to use it is: nsAutoArrayPtr buf; // assign something to buf nsresult rv = NewObjectInputStreamFromBuffer(buf, ...); if (NS_FAILED(rv)) { ... return rv; } buf.forget(); Which is clumsy, error-prone, and obscures the ownership transfer of the pointer into the stream returned by NewObjectInputStreamFromBuffer. Let's address all of these concerns by changing the argument to a UniquePtr. --- dom/xbl/nsXBLDocumentInfo.cpp | 4 ++-- dom/xul/nsXULPrototypeCache.cpp | 8 ++++---- startupcache/StartupCacheUtils.cpp | 4 ++-- startupcache/StartupCacheUtils.h | 3 ++- startupcache/test/TestStartupCache.cpp | 8 +++++--- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/dom/xbl/nsXBLDocumentInfo.cpp b/dom/xbl/nsXBLDocumentInfo.cpp index 4a8095140ca..5be464559dc 100644 --- a/dom/xbl/nsXBLDocumentInfo.cpp +++ b/dom/xbl/nsXBLDocumentInfo.cpp @@ -206,9 +206,9 @@ nsXBLDocumentInfo::ReadPrototypeBindings(nsIURI* aURI, nsXBLDocumentInfo** aDocI return rv; nsCOMPtr stream; - rv = NewObjectInputStreamFromBuffer(buf, len, getter_AddRefs(stream)); + rv = NewObjectInputStreamFromBuffer(UniquePtr(buf.forget()), + len, getter_AddRefs(stream)); NS_ENSURE_SUCCESS(rv, rv); - buf.forget(); // The file compatibility.ini stores the build id. This is checked in // nsAppRunner.cpp and will delete the cache if a different build is diff --git a/dom/xul/nsXULPrototypeCache.cpp b/dom/xul/nsXULPrototypeCache.cpp index 64a717a1ea0..5bb9ac0a530 100644 --- a/dom/xul/nsXULPrototypeCache.cpp +++ b/dom/xul/nsXULPrototypeCache.cpp @@ -345,9 +345,9 @@ nsXULPrototypeCache::GetInputStream(nsIURI* uri, nsIObjectInputStream** stream) if (NS_FAILED(rv)) return NS_ERROR_NOT_AVAILABLE; - rv = NewObjectInputStreamFromBuffer(buf, len, getter_AddRefs(ois)); + rv = NewObjectInputStreamFromBuffer(UniquePtr(buf.forget()), + len, getter_AddRefs(ois)); NS_ENSURE_SUCCESS(rv, rv); - buf.forget(); mInputStreamTable.Put(uri, ois); @@ -500,10 +500,10 @@ nsXULPrototypeCache::BeginCaching(nsIURI* aURI) rv = startupCache->GetBuffer(kXULCacheInfoKey, getter_Transfers(buf), &len); if (NS_SUCCEEDED(rv)) - rv = NewObjectInputStreamFromBuffer(buf, len, getter_AddRefs(objectInput)); + rv = NewObjectInputStreamFromBuffer(UniquePtr(buf.forget()), + len, getter_AddRefs(objectInput)); if (NS_SUCCEEDED(rv)) { - buf.forget(); rv = objectInput->ReadCString(fileLocale); tmp = objectInput->ReadCString(fileChromePath); if (NS_FAILED(tmp)) { diff --git a/startupcache/StartupCacheUtils.cpp b/startupcache/StartupCacheUtils.cpp index aac8d8a36b9..7273bd970d2 100644 --- a/startupcache/StartupCacheUtils.cpp +++ b/startupcache/StartupCacheUtils.cpp @@ -19,7 +19,7 @@ namespace mozilla { namespace scache { NS_EXPORT nsresult -NewObjectInputStreamFromBuffer(char* buffer, uint32_t len, +NewObjectInputStreamFromBuffer(UniquePtr buffer, uint32_t len, nsIObjectInputStream** stream) { nsCOMPtr stringStream @@ -27,7 +27,7 @@ NewObjectInputStreamFromBuffer(char* buffer, uint32_t len, nsCOMPtr objectInput = do_CreateInstance("@mozilla.org/binaryinputstream;1"); - stringStream->AdoptData(buffer, len); + stringStream->AdoptData(buffer.release(), len); objectInput->SetInputStream(stringStream); objectInput.forget(stream); diff --git a/startupcache/StartupCacheUtils.h b/startupcache/StartupCacheUtils.h index 1226d1668c2..f9454575ab0 100644 --- a/startupcache/StartupCacheUtils.h +++ b/startupcache/StartupCacheUtils.h @@ -8,12 +8,13 @@ #include "nsIStorageStream.h" #include "nsIObjectInputStream.h" #include "nsIObjectOutputStream.h" +#include "mozilla/UniquePtr.h" namespace mozilla { namespace scache { NS_EXPORT nsresult -NewObjectInputStreamFromBuffer(char* buffer, uint32_t len, +NewObjectInputStreamFromBuffer(UniquePtr buffer, uint32_t len, nsIObjectInputStream** stream); // We can't retrieve the wrapped stream from the objectOutputStream later, diff --git a/startupcache/test/TestStartupCache.cpp b/startupcache/test/TestStartupCache.cpp index e7102c5c38d..0597d15bcae 100644 --- a/startupcache/test/TestStartupCache.cpp +++ b/startupcache/test/TestStartupCache.cpp @@ -23,6 +23,7 @@ #include "nsIXPConnect.h" #include "prio.h" #include "mozilla/Maybe.h" +#include "mozilla/UniquePtr.h" using namespace JS; @@ -30,7 +31,7 @@ namespace mozilla { namespace scache { NS_IMPORT nsresult -NewObjectInputStreamFromBuffer(char* buffer, uint32_t len, +NewObjectInputStreamFromBuffer(UniquePtr buffer, uint32_t len, nsIObjectInputStream** stream); // We can't retrieve the wrapped stream from the objectOutputStream later, @@ -46,6 +47,7 @@ NewBufferFromStorageStream(nsIStorageStream *storageStream, } // namespace mozilla using namespace mozilla::scache; +using mozilla::UniquePtr; #define NS_ENSURE_STR_MATCH(str1, str2, testname) \ PR_BEGIN_MACRO \ @@ -216,12 +218,12 @@ TestWriteObject() { return rv; } - rv = NewObjectInputStreamFromBuffer(buf2, len2, getter_AddRefs(objectInput)); + rv = NewObjectInputStreamFromBuffer(UniquePtr(buf2.forget()), len2, + getter_AddRefs(objectInput)); if (NS_FAILED(rv)) { fail("failed to created input stream"); return rv; } - buf2.forget(); nsCOMPtr deserialized; rv = objectInput->ReadObject(true, getter_AddRefs(deserialized));