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<char> 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<char[]>.
This commit is contained in:
Nathan Froyd 2016-02-18 11:14:02 -05:00
parent 648addac4e
commit cd3c15f774
5 changed files with 15 additions and 12 deletions

View File

@ -206,9 +206,9 @@ nsXBLDocumentInfo::ReadPrototypeBindings(nsIURI* aURI, nsXBLDocumentInfo** aDocI
return rv;
nsCOMPtr<nsIObjectInputStream> stream;
rv = NewObjectInputStreamFromBuffer(buf, len, getter_AddRefs(stream));
rv = NewObjectInputStreamFromBuffer(UniquePtr<char[]>(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

View File

@ -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<char[]>(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<char[]>(buf.forget()),
len, getter_AddRefs(objectInput));
if (NS_SUCCEEDED(rv)) {
buf.forget();
rv = objectInput->ReadCString(fileLocale);
tmp = objectInput->ReadCString(fileChromePath);
if (NS_FAILED(tmp)) {

View File

@ -19,7 +19,7 @@ namespace mozilla {
namespace scache {
NS_EXPORT nsresult
NewObjectInputStreamFromBuffer(char* buffer, uint32_t len,
NewObjectInputStreamFromBuffer(UniquePtr<char[]> buffer, uint32_t len,
nsIObjectInputStream** stream)
{
nsCOMPtr<nsIStringInputStream> stringStream
@ -27,7 +27,7 @@ NewObjectInputStreamFromBuffer(char* buffer, uint32_t len,
nsCOMPtr<nsIObjectInputStream> objectInput
= do_CreateInstance("@mozilla.org/binaryinputstream;1");
stringStream->AdoptData(buffer, len);
stringStream->AdoptData(buffer.release(), len);
objectInput->SetInputStream(stringStream);
objectInput.forget(stream);

View File

@ -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<char[]> buffer, uint32_t len,
nsIObjectInputStream** stream);
// We can't retrieve the wrapped stream from the objectOutputStream later,

View File

@ -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<char[]> 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<char[]>(buf2.forget()), len2,
getter_AddRefs(objectInput));
if (NS_FAILED(rv)) {
fail("failed to created input stream");
return rv;
}
buf2.forget();
nsCOMPtr<nsISupports> deserialized;
rv = objectInput->ReadObject(true, getter_AddRefs(deserialized));