Bug 1132078 - Remove useless null checks after allocating memory with |new| from xpcom/io. r=nfroyd
authorThomas Baquet <thomas@bkfox.net>
Mon, 02 Mar 2015 11:59:36 +0100
changeset 232767 2a019a588c26b3fdbdf7bb35e05d0cd1f70bfb69
parent 232766 074a9461cafd2784ad9c9d8ec21085ad37515768
child 232768 133524b43f9c7f79abd60624dd3dd1335b6308f5
push id28392
push userkwierso@gmail.com
push dateTue, 10 Mar 2015 22:33:53 +0000
treeherdermozilla-central@440b80b56ba6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnfroyd
bugs1132078
milestone39.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 1132078 - Remove useless null checks after allocating memory with |new| from xpcom/io. r=nfroyd
xpcom/io/nsAppFileLocationProvider.cpp
xpcom/io/nsDirectoryService.cpp
xpcom/io/nsInputStreamTee.cpp
xpcom/io/nsLocalFileUnix.cpp
xpcom/io/nsLocalFileWin.cpp
xpcom/io/nsMultiplexInputStream.cpp
xpcom/io/nsPipe3.cpp
xpcom/io/nsScriptableInputStream.cpp
xpcom/io/nsStorageStream.cpp
xpcom/io/nsStreamUtils.cpp
xpcom/io/nsStringStream.cpp
xpcom/io/nsUnicharInputStream.cpp
--- a/xpcom/io/nsAppFileLocationProvider.cpp
+++ b/xpcom/io/nsAppFileLocationProvider.cpp
@@ -575,28 +575,28 @@ nsAppFileLocationProvider::GetFiles(cons
     static const char* keys[] = { nullptr, NS_USER_PLUGINS_DIR, NS_APP_PLUGINS_DIR, nullptr };
 #endif
     if (!keys[0] && !(keys[0] = PR_GetEnv("MOZ_PLUGIN_PATH"))) {
       static const char nullstr = 0;
       keys[0] = &nullstr;
     }
     *aResult = new nsPathsDirectoryEnumerator(this, keys);
 #endif
-    NS_IF_ADDREF(*aResult);
-    rv = *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
+    NS_ADDREF(*aResult);
+    rv = NS_OK;
   }
   if (!nsCRT::strcmp(aProp, NS_APP_SEARCH_DIR_LIST)) {
     static const char* keys[] = { nullptr, NS_APP_SEARCH_DIR, NS_APP_USER_SEARCH_DIR, nullptr };
     if (!keys[0] && !(keys[0] = PR_GetEnv("MOZ_SEARCH_ENGINE_PATH"))) {
       static const char nullstr = 0;
       keys[0] = &nullstr;
     }
     *aResult = new nsPathsDirectoryEnumerator(this, keys);
-    NS_IF_ADDREF(*aResult);
-    rv = *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
+    NS_ADDREF(*aResult);
+    rv = NS_OK;
   }
   return rv;
 }
 
 #if defined(MOZ_WIDGET_COCOA)
 bool
 nsAppFileLocationProvider::IsOSXLeopard()
 {
--- a/xpcom/io/nsDirectoryService.cpp
+++ b/xpcom/io/nsDirectoryService.cpp
@@ -86,19 +86,16 @@ nsDirectoryService::GetCurrentProcessDir
     if (aLocalFile) {
       *aFile = aLocalFile;
       NS_ADDREF(*aFile);
       return NS_OK;
     }
   }
 
   nsLocalFile* localFile = new nsLocalFile;
-  if (!localFile) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
   NS_ADDREF(localFile);
 
 #ifdef XP_WIN
   wchar_t buf[MAX_PATH + 1];
   SetLastError(ERROR_SUCCESS);
   if (GetModuleFileNameW(0, buf, mozilla::ArrayLength(buf)) &&
       GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
     // chop off the executable name by finding the rightmost backslash
--- a/xpcom/io/nsInputStreamTee.cpp
+++ b/xpcom/io/nsInputStreamTee.cpp
@@ -342,20 +342,16 @@ nsresult
 NS_NewInputStreamTeeAsync(nsIInputStream** aResult,
                           nsIInputStream* aSource,
                           nsIOutputStream* aSink,
                           nsIEventTarget* aEventTarget)
 {
   nsresult rv;
 
   nsCOMPtr<nsIInputStreamTee> tee = new nsInputStreamTee();
-  if (!tee) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   rv = tee->SetSource(aSource);
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   rv = tee->SetSink(aSink);
   if (NS_FAILED(rv)) {
     return rv;
--- a/xpcom/io/nsLocalFileUnix.cpp
+++ b/xpcom/io/nsLocalFileUnix.cpp
@@ -822,20 +822,16 @@ nsLocalFile::CopyToNative(nsIFile* aNewP
     }
 
 #ifdef DEBUG_blizzard
     printf("nsLocalFile::CopyTo() %s -> %s\n", mPath.get(), newPathName.get());
 #endif
 
     // actually create the file.
     nsLocalFile* newFile = new nsLocalFile();
-    if (!newFile) {
-      return NS_ERROR_OUT_OF_MEMORY;
-    }
-
     nsCOMPtr<nsIFile> fileRef(newFile); // release on exit
 
     rv = newFile->InitWithNativePath(newPathName);
     if (NS_FAILED(rv)) {
       return rv;
     }
 
     // get the old permissions
--- a/xpcom/io/nsLocalFileWin.cpp
+++ b/xpcom/io/nsLocalFileWin.cpp
@@ -452,20 +452,16 @@ ShortcutResolver::SetShortcut(bool aUpda
 }
 
 static ShortcutResolver* gResolver = nullptr;
 
 static nsresult
 NS_CreateShortcutResolver()
 {
   gResolver = new ShortcutResolver();
-  if (!gResolver) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   return gResolver->Init();
 }
 
 static void
 NS_DestroyShortcutResolver()
 {
   delete gResolver;
   gResolver = nullptr;
@@ -759,21 +755,17 @@ OpenDir(const nsAFlatString& aName, nsDi
     return NS_ERROR_INVALID_ARG;
   }
 
   *aDir = nullptr;
   if (aName.Length() + 3 >= MAX_PATH) {
     return NS_ERROR_FILE_NAME_TOO_LONG;
   }
 
-  nsDir* d  = PR_NEW(nsDir);
-  if (!d) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
+  nsDir* d  = new nsDir();
   nsAutoString filename(aName);
 
   // If |aName| ends in a slash or backslash, do not append another backslash.
   if (filename.Last() == L'/' || filename.Last() == L'\\') {
     filename.Append('*');
   } else {
     filename.AppendLiteral("\\*");
   }
@@ -781,17 +773,17 @@ OpenDir(const nsAFlatString& aName, nsDi
   filename.ReplaceChar(L'/', L'\\');
 
   // FindFirstFileW Will have a last error of ERROR_DIRECTORY if
   // <file_path>\* is passed in.  If <unknown_path>\* is passed in then
   // ERROR_PATH_NOT_FOUND will be the last error.
   d->handle = ::FindFirstFileW(filename.get(), &(d->data));
 
   if (d->handle == INVALID_HANDLE_VALUE) {
-    PR_Free(d);
+    delete d;
     return ConvertWinError(GetLastError());
   }
   d->firstEntry = true;
 
   *aDir = d;
   return NS_OK;
 }
 
@@ -850,18 +842,18 @@ ReadDir(nsDir* aDir, PRDirFlags aFlags, 
 static nsresult
 CloseDir(nsDir*& aDir)
 {
   if (NS_WARN_IF(!aDir)) {
     return NS_ERROR_INVALID_ARG;
   }
 
   BOOL isOk = FindClose(aDir->handle);
-  // PR_DELETE also nulls out the passed in pointer.
-  PR_DELETE(aDir);
+  delete aDir;
+  aDir = nullptr;
   return isOk ? NS_OK : ConvertWinError(GetLastError());
 }
 
 //-----------------------------------------------------------------------------
 // nsDirEnumerator
 //-----------------------------------------------------------------------------
 
 class nsDirEnumerator MOZ_FINAL
@@ -1013,20 +1005,16 @@ nsLocalFile::nsLocalFileConstructor(nsIS
   if (NS_WARN_IF(!aInstancePtr)) {
     return NS_ERROR_INVALID_ARG;
   }
   if (NS_WARN_IF(aOuter)) {
     return NS_ERROR_NO_AGGREGATION;
   }
 
   nsLocalFile* inst = new nsLocalFile();
-  if (!inst) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   nsresult rv = inst->QueryInterface(aIID, aInstancePtr);
   if (NS_FAILED(rv)) {
     delete inst;
     return rv;
   }
   return NS_OK;
 }
 
@@ -1190,20 +1178,16 @@ nsLocalFile::Resolve()
 // nsLocalFile::nsIFile,nsILocalFile
 //-----------------------------------------------------------------------------
 
 NS_IMETHODIMP
 nsLocalFile::Clone(nsIFile** aFile)
 {
   // Just copy-construct ourselves
   *aFile = new nsLocalFile(*this);
-  if (!*aFile) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   NS_ADDREF(*aFile);
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsLocalFile::InitWithFile(nsIFile* aFile)
 {
@@ -1727,21 +1711,17 @@ nsLocalFile::GetVersionInfoField(const c
     mFollowSymlinks ? mResolvedPath.get() : mWorkingPath.get();
 
   DWORD dummy;
   DWORD size = ::GetFileVersionInfoSizeW(path, &dummy);
   if (!size) {
     return rv;
   }
 
-  void* ver = calloc(size, 1);
-  if (!ver) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
+  void* ver = moz_xcalloc(size, 1);
   if (::GetFileVersionInfoW(path, 0, size, ver)) {
     LANGANDCODEPAGE* translate = nullptr;
     UINT pageCount;
     BOOL queryResult = ::VerQueryValueW(ver, L"\\VarFileInfo\\Translation",
                                         (void**)&translate, &pageCount);
     if (queryResult && translate) {
       for (int32_t i = 0; i < 2; ++i) {
         wchar_t subBlock[MAX_PATH];
@@ -1760,17 +1740,17 @@ nsLocalFile::GetVersionInfoField(const c
           if (!aResult.IsEmpty()) {
             rv = NS_OK;
             break;
           }
         }
       }
     }
   }
-  free(ver);
+  moz_free(ver);
 
   return rv;
 }
 
 NS_IMETHODIMP
 nsLocalFile::SetShortcut(nsIFile* aTargetFile,
                          nsIFile* aWorkingDir,
                          const char16_t* aArgs,
@@ -2029,20 +2009,16 @@ nsLocalFile::CopyMove(nsIFile* aParentDi
       if (followSymlinks) {
         bool isLink;
         newParentDir->IsSymlink(&isLink);
         if (isLink) {
           nsAutoString target;
           newParentDir->GetTarget(target);
 
           nsCOMPtr<nsIFile> realDest = new nsLocalFile();
-          if (!realDest) {
-            return NS_ERROR_OUT_OF_MEMORY;
-          }
-
           rv = realDest->InitWithPath(target);
 
           if (NS_FAILED(rv)) {
             return rv;
           }
 
           return CopyMove(realDest, aNewName, aOptions);
         }
@@ -3226,19 +3202,16 @@ nsLocalFile::SetFollowLinks(bool aFollow
 NS_IMETHODIMP
 nsLocalFile::GetDirectoryEntries(nsISimpleEnumerator** aEntries)
 {
   nsresult rv;
 
   *aEntries = nullptr;
   if (mWorkingPath.EqualsLiteral("\\\\.")) {
     nsDriveEnumerator* drives = new nsDriveEnumerator;
-    if (!drives) {
-      return NS_ERROR_OUT_OF_MEMORY;
-    }
     NS_ADDREF(drives);
     rv = drives->Init();
     if (NS_FAILED(rv)) {
       NS_RELEASE(drives);
       return rv;
     }
     *aEntries = drives;
     return NS_OK;
@@ -3376,19 +3349,16 @@ nsLocalFile::Launch()
   mythread->Dispatch(runnable, NS_DISPATCH_NORMAL);
   return NS_OK;
 }
 
 nsresult
 NS_NewLocalFile(const nsAString& aPath, bool aFollowLinks, nsIFile** aResult)
 {
   nsLocalFile* file = new nsLocalFile();
-  if (!file) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
   NS_ADDREF(file);
 
   file->SetFollowLinks(aFollowLinks);
 
   if (!aPath.IsEmpty()) {
     nsresult rv = file->InitWithPath(aPath);
     if (NS_FAILED(rv)) {
       NS_RELEASE(file);
--- a/xpcom/io/nsMultiplexInputStream.cpp
+++ b/xpcom/io/nsMultiplexInputStream.cpp
@@ -680,19 +680,16 @@ nsMultiplexInputStreamConstructor(nsISup
 {
   *aResult = nullptr;
 
   if (aOuter) {
     return NS_ERROR_NO_AGGREGATION;
   }
 
   nsMultiplexInputStream* inst = new nsMultiplexInputStream();
-  if (!inst) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
 
   NS_ADDREF(inst);
   nsresult rv = inst->QueryInterface(aIID, aResult);
   NS_RELEASE(inst);
 
   return rv;
 }
 
--- a/xpcom/io/nsPipe3.cpp
+++ b/xpcom/io/nsPipe3.cpp
@@ -1606,20 +1606,16 @@ NS_NewPipe2(nsIAsyncInputStream** aPipeI
             bool aNonBlockingInput,
             bool aNonBlockingOutput,
             uint32_t aSegmentSize,
             uint32_t aSegmentCount)
 {
   nsresult rv;
 
   nsPipe* pipe = new nsPipe();
-  if (!pipe) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   rv = pipe->Init(aNonBlockingInput,
                   aNonBlockingOutput,
                   aSegmentSize,
                   aSegmentCount);
   if (NS_FAILED(rv)) {
     NS_ADDREF(pipe);
     NS_RELEASE(pipe);
     return rv;
@@ -1632,18 +1628,15 @@ NS_NewPipe2(nsIAsyncInputStream** aPipeI
 
 nsresult
 nsPipeConstructor(nsISupports* aOuter, REFNSIID aIID, void** aResult)
 {
   if (aOuter) {
     return NS_ERROR_NO_AGGREGATION;
   }
   nsPipe* pipe = new nsPipe();
-  if (!pipe) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
   NS_ADDREF(pipe);
   nsresult rv = pipe->QueryInterface(aIID, aResult);
   NS_RELEASE(pipe);
   return rv;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
--- a/xpcom/io/nsScriptableInputStream.cpp
+++ b/xpcom/io/nsScriptableInputStream.cpp
@@ -125,17 +125,13 @@ nsresult
 nsScriptableInputStream::Create(nsISupports* aOuter, REFNSIID aIID,
                                 void** aResult)
 {
   if (aOuter) {
     return NS_ERROR_NO_AGGREGATION;
   }
 
   nsScriptableInputStream* sis = new nsScriptableInputStream();
-  if (!sis) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   NS_ADDREF(sis);
   nsresult rv = sis->QueryInterface(aIID, aResult);
   NS_RELEASE(sis);
   return rv;
 }
--- a/xpcom/io/nsStorageStream.cpp
+++ b/xpcom/io/nsStorageStream.cpp
@@ -72,20 +72,16 @@ nsStorageStream::~nsStorageStream()
 NS_IMPL_ISUPPORTS(nsStorageStream,
                   nsIStorageStream,
                   nsIOutputStream)
 
 NS_IMETHODIMP
 nsStorageStream::Init(uint32_t aSegmentSize, uint32_t aMaxSize)
 {
   mSegmentedBuffer = new nsSegmentedBuffer();
-  if (!mSegmentedBuffer) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   mSegmentSize = aSegmentSize;
   mSegmentSizeLog2 = mozilla::FloorLog2(aSegmentSize);
 
   // Segment size must be a power of two
   if (mSegmentSize != ((uint32_t)1 << mSegmentSizeLog2)) {
     return NS_ERROR_INVALID_ARG;
   }
 
@@ -405,20 +401,16 @@ nsStorageStream::NewInputStream(int32_t 
                                 nsIInputStream** aInputStream)
 {
   if (NS_WARN_IF(!mSegmentedBuffer)) {
     return NS_ERROR_NOT_INITIALIZED;
   }
 
   nsStorageInputStream* inputStream =
     new nsStorageInputStream(this, mSegmentSize);
-  if (!inputStream) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   NS_ADDREF(inputStream);
 
   nsresult rv = inputStream->Seek(aStartingOffset);
   if (NS_FAILED(rv)) {
     NS_RELEASE(inputStream);
     return rv;
   }
 
@@ -633,20 +625,16 @@ nsStorageInputStream::Clone(nsIInputStre
   return mStorageStream->NewInputStream(mLogicalCursor, aCloneOut);
 }
 
 nsresult
 NS_NewStorageStream(uint32_t aSegmentSize, uint32_t aMaxSize,
                     nsIStorageStream** aResult)
 {
   nsStorageStream* storageStream = new nsStorageStream();
-  if (!storageStream) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   NS_ADDREF(storageStream);
   nsresult rv = storageStream->Init(aSegmentSize, aMaxSize);
   if (NS_FAILED(rv)) {
     NS_RELEASE(storageStream);
     return rv;
   }
   *aResult = storageStream;
   return NS_OK;
--- a/xpcom/io/nsStreamUtils.cpp
+++ b/xpcom/io/nsStreamUtils.cpp
@@ -604,20 +604,16 @@ NS_AsyncCopy(nsIInputStream*         aSo
   nsAStreamCopier* copier;
 
   if (aMode == NS_ASYNCCOPY_VIA_READSEGMENTS) {
     copier = new nsStreamCopierIB();
   } else {
     copier = new nsStreamCopierOB();
   }
 
-  if (!copier) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   // Start() takes an owning ref to the copier...
   NS_ADDREF(copier);
   rv = copier->Start(aSource, aSink, aTarget, aCallback, aClosure, aChunkSize,
                      aCloseSource, aCloseSink, aProgressCallback);
 
   if (aCopierCtx) {
     *aCopierCtx = static_cast<nsISupports*>(static_cast<nsIRunnable*>(copier));
     NS_ADDREF(*aCopierCtx);
--- a/xpcom/io/nsStringStream.cpp
+++ b/xpcom/io/nsStringStream.cpp
@@ -376,20 +376,16 @@ nsStringInputStream::Clone(nsIInputStrea
 nsresult
 NS_NewByteInputStream(nsIInputStream** aStreamResult,
                       const char* aStringToRead, int32_t aLength,
                       nsAssignmentType aAssignment)
 {
   NS_PRECONDITION(aStreamResult, "null out ptr");
 
   nsStringInputStream* stream = new nsStringInputStream();
-  if (!stream) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   NS_ADDREF(stream);
 
   nsresult rv;
   switch (aAssignment) {
     case NS_ASSIGNMENT_COPY:
       rv = stream->SetData(aStringToRead, aLength);
       break;
     case NS_ASSIGNMENT_DEPEND:
@@ -422,20 +418,16 @@ NS_NewStringInputStream(nsIInputStream**
 
 nsresult
 NS_NewCStringInputStream(nsIInputStream** aStreamResult,
                          const nsACString& aStringToRead)
 {
   NS_PRECONDITION(aStreamResult, "null out ptr");
 
   nsStringInputStream* stream = new nsStringInputStream();
-  if (!stream) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   NS_ADDREF(stream);
 
   stream->SetData(aStringToRead);
 
   *aStreamResult = stream;
   return NS_OK;
 }
 
@@ -446,18 +438,14 @@ nsStringInputStreamConstructor(nsISuppor
 {
   *aResult = nullptr;
 
   if (NS_WARN_IF(aOuter)) {
     return NS_ERROR_NO_AGGREGATION;
   }
 
   nsStringInputStream* inst = new nsStringInputStream();
-  if (!inst) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   NS_ADDREF(inst);
   nsresult rv = inst->QueryInterface(aIID, aResult);
   NS_RELEASE(inst);
 
   return rv;
 }
--- a/xpcom/io/nsUnicharInputStream.cpp
+++ b/xpcom/io/nsUnicharInputStream.cpp
@@ -408,37 +408,30 @@ nsSimpleUnicharStreamFactory::LockFactor
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsSimpleUnicharStreamFactory::CreateInstanceFromString(const nsAString& aString,
                                                        nsIUnicharInputStream** aResult)
 {
   StringUnicharInputStream* it = new StringUnicharInputStream(aString);
-  if (!it) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
 
   NS_ADDREF(*aResult = it);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsSimpleUnicharStreamFactory::CreateInstanceFromUTF8Stream(
     nsIInputStream* aStreamToWrap,
     nsIUnicharInputStream** aResult)
 {
   *aResult = nullptr;
 
   // Create converter input stream
   nsRefPtr<UTF8InputStream> it = new UTF8InputStream();
-  if (!it) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
   nsresult rv = it->Init(aStreamToWrap);
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   NS_ADDREF(*aResult = it);
   return NS_OK;
 }