From 8040aa67c95c2514e03dff61373fe858bfd9e7f1 Mon Sep 17 00:00:00 2001 From: Phil Ringnalda Date: Tue, 19 Nov 2013 14:38:29 -0800 Subject: [PATCH] Backed out 4 changesets (bug 672843) for xpcshell bustage Backed out changeset bbb7760083ae (bug 672843) Backed out changeset eaf2fd75d7fc (bug 672843) Backed out changeset eb08cc206b8d (bug 672843) Backed out changeset 6a0e4afd52ab (bug 672843) --- accessible/src/windows/msaa/IUnknownImpl.cpp | 2 +- xpcom/base/ErrorList.h | 4 +- xpcom/base/nsAgg.h | 12 +- xpcom/base/nsCycleCollector.cpp | 33 ++--- xpcom/base/nsDebugImpl.cpp | 3 +- xpcom/base/nsErrorService.cpp | 3 +- xpcom/base/nsGZFileWriter.cpp | 25 ++-- xpcom/base/nsIMemoryReporter.idl | 3 +- xpcom/base/nsMacUtilsImpl.cpp | 3 +- xpcom/base/nsMemoryImpl.cpp | 3 +- xpcom/base/nsMemoryInfoDumper.cpp | 98 +++++--------- xpcom/base/nsMemoryReporterManager.cpp | 18 +-- xpcom/base/nsMessageLoop.cpp | 6 +- xpcom/base/nsSystemInfo.cpp | 12 +- xpcom/build/Omnijar.cpp | 6 +- xpcom/build/nsXPCOMStrings.cpp | 6 +- xpcom/build/nsXPComInit.cpp | 56 ++++---- xpcom/build/perfprobe.cpp | 3 +- xpcom/components/nsCategoryManager.cpp | 26 ++-- xpcom/components/nsComponentManager.cpp | 12 +- xpcom/ds/nsHashPropertyBag.cpp | 3 +- xpcom/ds/nsINIParserImpl.cpp | 3 +- xpcom/ds/nsObserverService.cpp | 24 ++-- xpcom/ds/nsProperties.cpp | 17 +-- xpcom/ds/nsStringEnumerator.cpp | 31 +++-- xpcom/ds/nsSupportsArray.cpp | 3 +- xpcom/ds/nsSupportsPrimitives.cpp | 6 +- xpcom/ds/nsWindowsRegKey.cpp | 74 ++++------- xpcom/glue/nsArrayEnumerator.cpp | 1 + xpcom/glue/nsCOMArray.cpp | 3 +- xpcom/glue/nsComponentManagerUtils.cpp | 18 +-- xpcom/glue/nsDebug.h | 130 +++++++------------ xpcom/glue/nsINIParser.cpp | 3 +- xpcom/glue/nsMemory.cpp | 3 +- xpcom/glue/nsThreadUtils.cpp | 36 ++--- xpcom/glue/nsThreadUtils.h | 3 +- xpcom/io/Base64.cpp | 3 +- xpcom/io/CocoaFileUtils.mm | 20 ++- xpcom/io/nsAnonymousTemporaryFile.cpp | 36 ++--- xpcom/io/nsAppFileLocationProvider.cpp | 19 +-- xpcom/io/nsBinaryStream.cpp | 106 ++++++--------- xpcom/io/nsDirectoryService.cpp | 24 ++-- xpcom/io/nsIOUtil.cpp | 8 +- xpcom/io/nsInputStreamTee.cpp | 16 +-- xpcom/io/nsLocalFileCommon.cpp | 6 +- xpcom/io/nsLocalFileUnix.cpp | 93 +++++-------- xpcom/io/nsLocalFileWin.cpp | 72 ++++------ xpcom/io/nsMultiplexInputStream.cpp | 58 +++------ xpcom/io/nsPipe3.cpp | 3 +- xpcom/io/nsStorageStream.cpp | 31 +++-- xpcom/io/nsStringStream.cpp | 19 +-- xpcom/reflect/xptcall/src/xptcall.cpp | 6 +- xpcom/threads/LazyIdleThread.cpp | 42 ++---- xpcom/threads/nsEnvironment.cpp | 12 +- xpcom/threads/nsMemoryPressure.cpp | 1 + xpcom/threads/nsProcessCommon.cpp | 9 +- xpcom/threads/nsThread.cpp | 48 ++++--- xpcom/threads/nsThreadManager.cpp | 15 +-- xpcom/threads/nsThreadPool.cpp | 9 +- xpcom/threads/nsTimerImpl.cpp | 19 +-- 60 files changed, 499 insertions(+), 868 deletions(-) diff --git a/accessible/src/windows/msaa/IUnknownImpl.cpp b/accessible/src/windows/msaa/IUnknownImpl.cpp index 4a9fa5383b2..f6ae9f6a132 100644 --- a/accessible/src/windows/msaa/IUnknownImpl.cpp +++ b/accessible/src/windows/msaa/IUnknownImpl.cpp @@ -23,7 +23,7 @@ GetHRESULT(nsresult aResult) case NS_OK: return S_OK; - case NS_ERROR_INVALID_ARG: + case NS_ERROR_INVALID_ARG: case NS_ERROR_INVALID_POINTER: return E_INVALIDARG; case NS_ERROR_OUT_OF_MEMORY: diff --git a/xpcom/base/ErrorList.h b/xpcom/base/ErrorList.h index 94cd4893e96..1552bb0e11d 100644 --- a/xpcom/base/ErrorList.h +++ b/xpcom/base/ErrorList.h @@ -16,6 +16,8 @@ /* Returned when a given interface is not supported. */ ERROR(NS_NOINTERFACE, 0x80004002), ERROR(NS_ERROR_NO_INTERFACE, NS_NOINTERFACE), + ERROR(NS_ERROR_INVALID_POINTER, 0x80004003), + ERROR(NS_ERROR_NULL_POINTER, NS_ERROR_INVALID_POINTER), /* Returned when a function aborts */ ERROR(NS_ERROR_ABORT, 0x80004004), /* Returned when a function fails */ @@ -27,8 +29,6 @@ /* Returned when an illegal value is passed */ ERROR(NS_ERROR_ILLEGAL_VALUE, 0x80070057), ERROR(NS_ERROR_INVALID_ARG, NS_ERROR_ILLEGAL_VALUE), - ERROR(NS_ERROR_INVALID_POINTER, NS_ERROR_INVALID_ARG), - ERROR(NS_ERROR_NULL_POINTER, NS_ERROR_INVALID_ARG), /* Returned when a class doesn't allow aggregation */ ERROR(NS_ERROR_NO_AGGREGATION, 0x80040110), /* Returned when an operation can't complete due to an unavailable resource */ diff --git a/xpcom/base/nsAgg.h b/xpcom/base/nsAgg.h index bd1a36d52cc..5b9ff722797 100644 --- a/xpcom/base/nsAgg.h +++ b/xpcom/base/nsAgg.h @@ -285,9 +285,9 @@ static nsresult \ _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \ void **aResult) \ { \ - *aResult = nullptr; \ - if (NS_WARN_IF(aOuter && !aIID.Equals(NS_GET_IID(nsISupports)))) \ - return NS_ERROR_INVALID_ARG; \ + *aResult = nullptr; \ + \ + NS_ENSURE_PROPER_AGGREGATION(aOuter, aIID); \ \ _InstanceClass* inst = new _InstanceClass(aOuter); \ if (!inst) { \ @@ -308,9 +308,9 @@ static nsresult \ _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \ void **aResult) \ { \ - *aResult = nullptr; \ - if (NS_WARN_IF(aOuter && !aIID.Equals(NS_GET_IID(nsISupports)))) \ - return NS_ERROR_INVALID_ARG; \ + *aResult = nullptr; \ + \ + NS_ENSURE_PROPER_AGGREGATION(aOuter, aIID); \ \ _InstanceClass* inst = new _InstanceClass(aOuter); \ if (!inst) { \ diff --git a/xpcom/base/nsCycleCollector.cpp b/xpcom/base/nsCycleCollector.cpp index a88436d36e8..4079ca38ca5 100644 --- a/xpcom/base/nsCycleCollector.cpp +++ b/xpcom/base/nsCycleCollector.cpp @@ -1221,14 +1221,12 @@ public: // want scripts which poll the filesystem looking for gc/cc dumps to // grab a file before we're finished writing to it.) nsCOMPtr gcLogFile = CreateTempFile("incomplete-gc-edges"); - if (NS_WARN_IF(!gcLogFile)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(gcLogFile); // Dump the JS heap. FILE* gcLogANSIFile = nullptr; gcLogFile->OpenANSIFileDesc("w", &gcLogANSIFile); - if (NS_WARN_IF(!gcLogANSIFile)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(gcLogANSIFile); MozillaRegisterDebugFILE(gcLogANSIFile); CollectorData *data = sCollectorData.get(); if (data && data->mRuntime) @@ -1239,13 +1237,11 @@ public: // Strip off "incomplete-". nsCOMPtr gcLogFileFinalDestination = CreateTempFile("gc-edges"); - if (NS_WARN_IF(!gcLogFileFinalDestination)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(gcLogFileFinalDestination); nsAutoString gcLogFileFinalDestinationName; gcLogFileFinalDestination->GetLeafName(gcLogFileFinalDestinationName); - if (NS_WARN_IF(gcLogFileFinalDestinationName.IsEmpty())) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(!gcLogFileFinalDestinationName.IsEmpty()); gcLogFile->MoveTo(/* directory */ nullptr, gcLogFileFinalDestinationName); @@ -1264,12 +1260,10 @@ public: // Open a file for dumping the CC graph. We again prefix with // "incomplete-". mOutFile = CreateTempFile("incomplete-cc-edges"); - if (NS_WARN_IF(!mOutFile)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mOutFile); MOZ_ASSERT(!mStream); mOutFile->OpenANSIFileDesc("w", &mStream); - if (NS_WARN_IF(!mStream)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mStream); MozillaRegisterDebugFILE(mStream); fprintf(mStream, "# WantAllTraces=%s\n", mWantAllTraces ? "true" : "false"); @@ -1394,13 +1388,11 @@ public: // Strip off "incomplete-" from the log file's name. nsCOMPtr logFileFinalDestination = CreateTempFile("cc-edges"); - if (NS_WARN_IF(!logFileFinalDestination)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(logFileFinalDestination); nsAutoString logFileFinalDestinationName; logFileFinalDestination->GetLeafName(logFileFinalDestinationName); - if (NS_WARN_IF(logFileFinalDestinationName.IsEmpty())) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(!logFileFinalDestinationName.IsEmpty()); mOutFile->MoveTo(/* directory = */ nullptr, logFileFinalDestinationName); @@ -1423,8 +1415,7 @@ public: NS_IMETHOD ProcessNext(nsICycleCollectorHandler* aHandler, bool* aCanContinue) { - if (NS_WARN_IF(!aHandler) || NS_WARN_IF(!mWantAfterProcessing)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(aHandler && mWantAfterProcessing); CCGraphDescriber* d = mDescribers.popFirst(); if (d) { switch (d->mType) { @@ -1524,8 +1515,7 @@ nsCycleCollectorLoggerConstructor(nsISupports* aOuter, const nsIID& aIID, void* *aInstancePtr) { - if (NS_WARN_IF(aOuter)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_TRUE(!aOuter, NS_ERROR_NO_AGGREGATION); nsISupports *logger = new nsCycleCollectorLogger(); @@ -2448,8 +2438,7 @@ class CycleCollectorReporter MOZ_FINAL : public MemoryMultiReporter nsIMemoryReporter::KIND_HEAP, \ nsIMemoryReporter::UNITS_BYTES, _amount, \ NS_LITERAL_CSTRING(_desc), aClosure); \ - if (NS_WARN_IF(NS_FAILED(rv))) \ - return rv; \ + NS_ENSURE_SUCCESS(rv, rv); \ } \ } while (0) diff --git a/xpcom/base/nsDebugImpl.cpp b/xpcom/base/nsDebugImpl.cpp index 3cb01f4432a..cd9e12a1d0c 100644 --- a/xpcom/base/nsDebugImpl.cpp +++ b/xpcom/base/nsDebugImpl.cpp @@ -565,8 +565,7 @@ static const nsDebugImpl kImpl; nsresult nsDebugImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr) { - if (NS_WARN_IF(outer)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_NO_AGGREGATION(outer); return const_cast(&kImpl)-> QueryInterface(aIID, aInstancePtr); diff --git a/xpcom/base/nsErrorService.cpp b/xpcom/base/nsErrorService.cpp index 58ef07b817f..dd5464d7e5c 100644 --- a/xpcom/base/nsErrorService.cpp +++ b/xpcom/base/nsErrorService.cpp @@ -65,8 +65,7 @@ NS_IMPL_ISUPPORTS1(nsErrorService, nsIErrorService) nsresult nsErrorService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr) { - if (NS_WARN_IF(outer)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_NO_AGGREGATION(outer); nsRefPtr serv = new nsErrorService(); return serv->QueryInterface(aIID, aInstancePtr); } diff --git a/xpcom/base/nsGZFileWriter.cpp b/xpcom/base/nsGZFileWriter.cpp index 166e199d54c..c1e96cacf47 100644 --- a/xpcom/base/nsGZFileWriter.cpp +++ b/xpcom/base/nsGZFileWriter.cpp @@ -33,25 +33,21 @@ nsGZFileWriter::~nsGZFileWriter() NS_IMETHODIMP nsGZFileWriter::Init(nsIFile* aFile) { - if (NS_WARN_IF(mInitialized) || - NS_WARN_IF(mFinished)) - return NS_ERROR_FAILURE; + NS_ENSURE_FALSE(mInitialized, NS_ERROR_FAILURE); + NS_ENSURE_FALSE(mFinished, NS_ERROR_FAILURE); // Get a FILE out of our nsIFile. Convert that into a file descriptor which // gzip can own. Then close our FILE, leaving only gzip's fd open. FILE* file; nsresult rv = aFile->OpenANSIFileDesc("wb", &file); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); mGZFile = gzdopen(dup(fileno(file)), "wb"); fclose(file); // gzdopen returns nullptr on error. - if (NS_WARN_IF(!mGZFile)) - return NS_ERROR_FAILURE; - + NS_ENSURE_TRUE(mGZFile, NS_ERROR_FAILURE); mInitialized = true; return NS_OK; @@ -60,9 +56,8 @@ nsGZFileWriter::Init(nsIFile* aFile) NS_IMETHODIMP nsGZFileWriter::Write(const nsACString& aStr) { - if (NS_WARN_IF(!mInitialized) || - NS_WARN_IF(mFinished)) - return NS_ERROR_FAILURE; + NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED); + NS_ENSURE_FALSE(mFinished, NS_ERROR_FAILURE); // gzwrite uses a return value of 0 to indicate failure. Otherwise, it // returns the number of uncompressed bytes written. To ensure we can @@ -76,8 +71,7 @@ nsGZFileWriter::Write(const nsACString& aStr) // always be either 0 or aStr.Length(), and we shouldn't have to call it // multiple times in order to get it to read the whole buffer. int rv = gzwrite(mGZFile, aStr.BeginReading(), aStr.Length()); - if (NS_WARN_IF(rv != static_cast(aStr.Length()))) - return NS_ERROR_FAILURE; + NS_ENSURE_TRUE(rv == static_cast(aStr.Length()), NS_ERROR_FAILURE); return NS_OK; } @@ -85,9 +79,8 @@ nsGZFileWriter::Write(const nsACString& aStr) NS_IMETHODIMP nsGZFileWriter::Finish() { - if (NS_WARN_IF(!mInitialized) || - NS_WARN_IF(mFinished)) - return NS_ERROR_FAILURE; + NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED); + NS_ENSURE_FALSE(mFinished, NS_ERROR_FAILURE); mFinished = true; gzclose(mGZFile); diff --git a/xpcom/base/nsIMemoryReporter.idl b/xpcom/base/nsIMemoryReporter.idl index df2154451e1..5a64aa83a91 100644 --- a/xpcom/base/nsIMemoryReporter.idl +++ b/xpcom/base/nsIMemoryReporter.idl @@ -535,8 +535,7 @@ public: { int64_t amount; nsresult rv = GetAmount(&amount); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); return aCallback->Callback(EmptyCString(), mNameAndPath, mKind, mUnits, amount, mDescription, aData); diff --git a/xpcom/base/nsMacUtilsImpl.cpp b/xpcom/base/nsMacUtilsImpl.cpp index bf9828ad849..84a030051dc 100644 --- a/xpcom/base/nsMacUtilsImpl.cpp +++ b/xpcom/base/nsMacUtilsImpl.cpp @@ -89,8 +89,7 @@ nsresult nsMacUtilsImpl::GetArchString(nsAString& archString) NS_IMETHODIMP nsMacUtilsImpl::GetIsUniversalBinary(bool *aIsUniversalBinary) { - if (NS_WARN_IF(!aIsUniversalBinary)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aIsUniversalBinary); *aIsUniversalBinary = false; nsAutoString archString; diff --git a/xpcom/base/nsMemoryImpl.cpp b/xpcom/base/nsMemoryImpl.cpp index 2b25086e13f..f8b9bfc5a23 100644 --- a/xpcom/base/nsMemoryImpl.cpp +++ b/xpcom/base/nsMemoryImpl.cpp @@ -93,8 +93,7 @@ nsMemoryImpl::IsLowMemoryPlatform(bool *result) /*static*/ nsresult nsMemoryImpl::Create(nsISupports* outer, const nsIID& aIID, void **aResult) { - if (NS_WARN_IF(outer)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_NO_AGGREGATION(outer); return sGlobalMemory.QueryInterface(aIID, aResult); } diff --git a/xpcom/base/nsMemoryInfoDumper.cpp b/xpcom/base/nsMemoryInfoDumper.cpp index 23a4d5fcc5e..c5ae19f0840 100644 --- a/xpcom/base/nsMemoryInfoDumper.cpp +++ b/xpcom/base/nsMemoryInfoDumper.cpp @@ -416,18 +416,15 @@ public: } } else { rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(file)); - if (NS_WARN_IF(NS_FAILED(rv))) - return -1; + NS_ENSURE_SUCCESS(rv, -1); } rv = file->AppendNative(NS_LITERAL_CSTRING("debug_info_trigger")); - if (NS_WARN_IF(NS_FAILED(rv))) - return -1; + NS_ENSURE_SUCCESS(rv, -1); nsAutoCString path; rv = file->GetNativePath(path); - if (NS_WARN_IF(NS_FAILED(rv))) - return -1; + NS_ENSURE_SUCCESS(rv, -1); // unlink might fail because the file doesn't exist, or for other reasons. // But we don't care it fails; any problems will be detected later, when we @@ -609,8 +606,7 @@ namespace mozilla { #define DUMP(o, s) \ do { \ nsresult rv = (o)->Write(s); \ - if (NS_WARN_IF(NS_FAILED(rv))) \ - return rv; \ + NS_ENSURE_SUCCESS(rv, rv); \ } while (0) static nsresult @@ -684,8 +680,7 @@ public: nsISupports *aData) { nsCOMPtr writer = do_QueryInterface(aData); - if (NS_WARN_IF(!writer)) - return NS_ERROR_FAILURE; + NS_ENSURE_TRUE(writer, NS_ERROR_FAILURE); nsresult rv = DumpReport(writer, mIsFirst, aProcess, aPath, aKind, aUnits, aAmount, aDescription); @@ -727,8 +722,7 @@ nsMemoryInfoDumper::OpenTempFile(const nsACString &aFilename, nsIFile* *aFile) nsresult rv; if (!*aFile) { rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, aFile); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } #ifdef ANDROID @@ -737,8 +731,7 @@ nsMemoryInfoDumper::OpenTempFile(const nsACString &aFilename, nsIFile* *aFile) // users to be able to remove these files, so we write them into a // subdirectory of the temp directory and chmod 777 that directory. rv = (*aFile)->AppendNative(NS_LITERAL_CSTRING("memory-reports")); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); // It's OK if this fails; that probably just means that the directory already // exists. @@ -746,8 +739,7 @@ nsMemoryInfoDumper::OpenTempFile(const nsACString &aFilename, nsIFile* *aFile) nsAutoCString dirPath; rv = (*aFile)->GetNativePath(dirPath); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); while (chmod(dirPath.get(), 0777) == -1 && errno == EINTR) {} #endif @@ -755,12 +747,10 @@ nsMemoryInfoDumper::OpenTempFile(const nsACString &aFilename, nsIFile* *aFile) nsCOMPtr file(*aFile); rv = file->AppendNative(aFilename); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = file->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0666); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); #ifdef ANDROID // Make this file world-read/writable; the permissions passed to the @@ -768,8 +758,7 @@ nsMemoryInfoDumper::OpenTempFile(const nsACString &aFilename, nsIFile* *aFile) // umask. nsAutoCString path; rv = file->GetNativePath(path); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); while (chmod(path.get(), 0666) == -1 && errno == EINTR) {} #endif @@ -812,8 +801,7 @@ DumpHeader(nsIGZFileWriter* aWriter) nsCOMPtr mgr = do_GetService("@mozilla.org/memory-reporter-manager;1"); - if (NS_WARN_IF(!mgr)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mgr); DUMP(aWriter, mgr->GetHasMozMallocUsableSize() ? "true" : "false"); DUMP(aWriter, ",\n"); @@ -884,12 +872,11 @@ DumpProcessMemoryInfoToTempDir(const nsAString& aIdentifier) rv = nsMemoryInfoDumper::OpenTempFile(NS_LITERAL_CSTRING("incomplete-") + mrFilename, getter_AddRefs(mrTmpFile)); - if (NS_WARN_IF(NS_FAILED(rv))) return rv; + NS_ENSURE_SUCCESS(rv, rv); nsRefPtr mrWriter = new nsGZFileWriter(); rv = mrWriter->Init(mrTmpFile); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); // Dump the memory reports to the file. DumpProcessMemoryReportsToGZFileWriter(mrWriter); @@ -907,13 +894,11 @@ DumpProcessMemoryInfoToTempDir(const nsAString& aIdentifier) nsCOMPtr dmdFile; rv = nsMemoryInfoDumper::OpenTempFile(dmdFilename, getter_AddRefs(dmdFile)); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); nsRefPtr dmdWriter = new nsGZFileWriter(); rv = dmdWriter->Init(dmdFile); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); // Dump DMD output to the file. @@ -922,8 +907,7 @@ DumpProcessMemoryInfoToTempDir(const nsAString& aIdentifier) dmd::Dump(w); rv = dmdWriter->Finish(); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); #endif // MOZ_DMD // The call to Finish() deallocates the memory allocated by mrWriter's first @@ -932,51 +916,42 @@ DumpProcessMemoryInfoToTempDir(const nsAString& aIdentifier) // them -- by "heap-allocated" if nothing else -- we want DMD to see it as // well. So we deliberately don't call Finish() until after DMD finishes. rv = mrWriter->Finish(); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); // Rename the memory reports file, now that we're done writing all the files. // Its final name is "memory-report<-identifier>-.json.gz". nsCOMPtr mrFinalFile; rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mrFinalFile)); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); #ifdef ANDROID rv = mrFinalFile->AppendNative(NS_LITERAL_CSTRING("memory-reports")); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); #endif rv = mrFinalFile->AppendNative(mrFilename); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = mrFinalFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); nsAutoString mrActualFinalFilename; rv = mrFinalFile->GetLeafName(mrActualFinalFilename); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = mrTmpFile->MoveTo(/* directory */ nullptr, mrActualFinalFilename); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); // Write a message to the console. nsCOMPtr cs = do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); nsString path; mrTmpFile->GetPath(path); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); nsString msg = NS_LITERAL_STRING( "nsIMemoryInfoDumper dumped reports to "); @@ -1013,8 +988,7 @@ nsMemoryInfoDumper::DumpMemoryInfoToTempDir(const nsAString& aIdentifier, /* dumpChildProcesses = */ false); nsCOMPtr mgr = do_GetService("@mozilla.org/memory-reporter-manager;1"); - if (NS_WARN_IF(!mgr)) - return NS_ERROR_FAILURE; + NS_ENSURE_TRUE(mgr, NS_ERROR_FAILURE); nsCOMPtr runnable; mgr->MinimizeMemoryUsage(callback, getter_AddRefs(runnable)); return NS_OK; @@ -1069,34 +1043,28 @@ nsMemoryInfoDumper::DumpMemoryReportsToNamedFile( nsCOMPtr mrFile; nsresult rv = NS_NewLocalFile(aFilename, false, getter_AddRefs(mrFile)); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); mrFile->InitWithPath(aFilename); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); bool exists; rv = mrFile->Exists(&exists); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); if (!exists) { rv = mrFile->Create(nsIFile::NORMAL_FILE_TYPE, 0644); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } // Write the memory reports to the file. nsRefPtr mrWriter = new nsGZFileWriter(); rv = mrWriter->Init(mrFile); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = DumpHeader(mrWriter); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); // Process reports and finish up. nsRefPtr dumpReport = new DumpReportCallback(); diff --git a/xpcom/base/nsMemoryReporterManager.cpp b/xpcom/base/nsMemoryReporterManager.cpp index aa6cc938b46..3b470b9ae16 100644 --- a/xpcom/base/nsMemoryReporterManager.cpp +++ b/xpcom/base/nsMemoryReporterManager.cpp @@ -102,8 +102,7 @@ private: *aAmount = 0; FILE *f = fopen("/proc/self/smaps", "r"); - if (NS_WARN_IF(!f)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(f); int64_t total = 0; char line[256]; @@ -753,8 +752,7 @@ public: nsIMemoryReporter::KIND_HEAP, \ nsIMemoryReporter::UNITS_BYTES, _amount, \ NS_LITERAL_CSTRING(_desc), aData); \ - if (NS_WARN_IF(NS_FAILED(rv))) \ - return rv; \ + NS_ENSURE_SUCCESS(rv, rv); \ } while (0) REPORT("explicit/dmd/stack-traces/used", @@ -1286,8 +1284,7 @@ NS_IMPL_ISUPPORTS1(ExplicitCallback, nsIHandleReportCallback) NS_IMETHODIMP nsMemoryReporterManager::GetExplicit(int64_t* aAmount) { - if (NS_WARN_IF(!aAmount)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aAmount); *aAmount = 0; #ifndef HAVE_JEMALLOC_STATS return NS_ERROR_NOT_AVAILABLE; @@ -1553,8 +1550,7 @@ NS_IMETHODIMP nsMemoryReporterManager::MinimizeMemoryUsage(nsIRunnable* aCallback, nsICancelableRunnable** aResult) { - if (NS_WARN_IF(!aResult)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aResult); nsRefPtr runnable = new MinimizeMemoryUsageRunnable(aCallback); @@ -1577,8 +1573,7 @@ nsMemoryReporterManager::SizeOfTab(nsIDOMWindow* aTopWindow, { nsCOMPtr global = do_QueryInterface(aTopWindow); nsCOMPtr piWindow = do_QueryInterface(aTopWindow); - if (NS_WARN_IF(!global) || NS_WARN_IF(!piWindow)) - return NS_ERROR_FAILURE; + NS_ENSURE_TRUE(!!global && !!piWindow, NS_ERROR_FAILURE); TimeStamp t1 = TimeStamp::Now(); @@ -1588,8 +1583,7 @@ nsMemoryReporterManager::SizeOfTab(nsIDOMWindow* aTopWindow, nsresult rv = mSizeOfTabFns.mJS(global->GetGlobalJSObject(), &jsObjectsSize, &jsStringsSize, &jsPrivateSize, &jsOtherSize); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); TimeStamp t2 = TimeStamp::Now(); diff --git a/xpcom/base/nsMessageLoop.cpp b/xpcom/base/nsMessageLoop.cpp index 8ef4c17a39a..0f1bd536e4e 100644 --- a/xpcom/base/nsMessageLoop.cpp +++ b/xpcom/base/nsMessageLoop.cpp @@ -87,8 +87,7 @@ nsresult MessageLoopIdleTask::Init(uint32_t aEnsureRunsAfterMS) { mTimer = do_CreateInstance("@mozilla.org/timer;1"); - if (NS_WARN_IF(!mTimer)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mTimer); nsRefPtr callback = new MessageLoopTimerCallback(this); @@ -154,8 +153,7 @@ nsMessageLoopConstructor(nsISupports* aOuter, const nsIID& aIID, void** aInstancePtr) { - if (NS_WARN_IF(aOuter)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_FALSE(aOuter, NS_ERROR_NO_AGGREGATION); nsISupports* messageLoop = new nsMessageLoop(); return messageLoop->QueryInterface(aIID, aInstancePtr); } diff --git a/xpcom/base/nsSystemInfo.cpp b/xpcom/base/nsSystemInfo.cpp index 0c32ac7901a..9439092ee94 100644 --- a/xpcom/base/nsSystemInfo.cpp +++ b/xpcom/base/nsSystemInfo.cpp @@ -165,8 +165,7 @@ nsSystemInfo::Init() if (PR_GetSystemInfo(items[i].cmd, buf, sizeof(buf)) == PR_SUCCESS) { rv = SetPropertyAsACString(NS_ConvertASCIItoUTF16(items[i].name), nsDependentCString(buf)); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } else { NS_WARNING("PR_GetSystemInfo failed"); @@ -183,8 +182,7 @@ nsSystemInfo::Init() for (uint32_t i = 0; i < ArrayLength(cpuPropItems); i++) { rv = SetPropertyAsBool(NS_ConvertASCIItoUTF16(cpuPropItems[i].name), cpuPropItems[i].propfun()); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } #ifdef XP_WIN @@ -193,8 +191,7 @@ nsSystemInfo::Init() NS_WARN_IF_FALSE(gotWow64Value, "IsWow64Process failed"); if (gotWow64Value) { rv = SetPropertyAsBool(NS_LITERAL_STRING("isWow64"), !!isWow64); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } nsAutoCString hddModel, hddRevision; if (NS_SUCCEEDED(GetProfileHDDInfo(hddModel, hddRevision))) { @@ -213,8 +210,7 @@ nsSystemInfo::Init() rv = SetPropertyAsACString(NS_LITERAL_STRING("secondaryLibrary"), nsDependentCString(gtkver)); PR_smprintf_free(gtkver); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } #endif diff --git a/xpcom/build/Omnijar.cpp b/xpcom/build/Omnijar.cpp index 3bc74178821..4d75c7ef7b5 100644 --- a/xpcom/build/Omnijar.cpp +++ b/xpcom/build/Omnijar.cpp @@ -145,8 +145,7 @@ Omnijar::GetURIString(Type aType, nsACString &result) nsAutoCString omniJarSpec; if (sPath[aType]) { nsresult rv = NS_GetURLSpecFromActualFile(sPath[aType], omniJarSpec); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); result = "jar:"; if (sIsNested[aType]) @@ -159,8 +158,7 @@ Omnijar::GetURIString(Type aType, nsACString &result) nsCOMPtr dir; nsDirectoryService::gService->Get(SPROP(aType), NS_GET_IID(nsIFile), getter_AddRefs(dir)); nsresult rv = NS_GetURLSpecFromActualFile(dir, result); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } result += "/"; return NS_OK; diff --git a/xpcom/build/nsXPCOMStrings.cpp b/xpcom/build/nsXPCOMStrings.cpp index 02796a835b1..d5df93ac321 100644 --- a/xpcom/build/nsXPCOMStrings.cpp +++ b/xpcom/build/nsXPCOMStrings.cpp @@ -40,8 +40,7 @@ NS_StringContainerInit2(nsStringContainer &aContainer, { if (aDataLength == UINT32_MAX) { - if (NS_WARN_IF(aFlags & NS_STRING_CONTAINER_INIT_SUBSTRING)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(!(aFlags & NS_STRING_CONTAINER_INIT_SUBSTRING)); aDataLength = nsCharTraits::length(aData); } @@ -201,8 +200,7 @@ NS_CStringContainerInit2(nsCStringContainer &aContainer, { if (aDataLength == UINT32_MAX) { - if (NS_WARN_IF(aFlags & NS_CSTRING_CONTAINER_INIT_SUBSTRING)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(!(aFlags & NS_CSTRING_CONTAINER_INIT_SUBSTRING)); aDataLength = nsCharTraits::length(aData); } diff --git a/xpcom/build/nsXPComInit.cpp b/xpcom/build/nsXPComInit.cpp index c3784f3f972..de58a8acd9e 100644 --- a/xpcom/build/nsXPComInit.cpp +++ b/xpcom/build/nsXPComInit.cpp @@ -219,8 +219,7 @@ nsThreadManagerGetSingleton(nsISupports* outer, void* *aInstancePtr) { NS_ASSERTION(aInstancePtr, "null outptr"); - if (NS_WARN_IF(outer)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_TRUE(!outer, NS_ERROR_NO_AGGREGATION); return nsThreadManager::get()->QueryInterface(aIID, aInstancePtr); } @@ -233,8 +232,7 @@ nsXPTIInterfaceInfoManagerGetSingleton(nsISupports* outer, void* *aInstancePtr) { NS_ASSERTION(aInstancePtr, "null outptr"); - if (NS_WARN_IF(outer)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_TRUE(!outer, NS_ERROR_NO_AGGREGATION); nsCOMPtr iim (XPTInterfaceInfoManager::GetSingleton()); @@ -315,16 +313,16 @@ static nsIDebug* gDebug = nullptr; EXPORT_XPCOM_API(nsresult) NS_GetDebug(nsIDebug** result) { - return nsDebugImpl::Create(nullptr, - NS_GET_IID(nsIDebug), + return nsDebugImpl::Create(nullptr, + NS_GET_IID(nsIDebug), (void**) result); } EXPORT_XPCOM_API(nsresult) NS_GetTraceRefcnt(nsITraceRefcnt** result) { - return nsTraceRefcntImpl::Create(nullptr, - NS_GET_IID(nsITraceRefcnt), + return nsTraceRefcntImpl::Create(nullptr, + NS_GET_IID(nsITraceRefcnt), (void**) result); } @@ -413,38 +411,38 @@ NS_InitXPCOM2(nsIServiceManager* *result, if (!AtExitManager::AlreadyRegistered()) { sExitManager = new AtExitManager(); + NS_ENSURE_STATE(sExitManager); } if (!MessageLoop::current()) { sMessageLoop = new MessageLoopForUI(MessageLoop::TYPE_MOZILLA_UI); + NS_ENSURE_STATE(sMessageLoop); } if (XRE_GetProcessType() == GeckoProcessType_Default && !BrowserProcessSubThread::GetMessageLoop(BrowserProcessSubThread::IO)) { scoped_ptr ioThread( new BrowserProcessSubThread(BrowserProcessSubThread::IO)); + NS_ENSURE_TRUE(ioThread.get(), NS_ERROR_OUT_OF_MEMORY); base::Thread::Options options; options.message_loop_type = MessageLoop::TYPE_IO; - if (NS_WARN_IF(!ioThread->StartWithOptions(options))) - return NS_ERROR_FAILURE; + NS_ENSURE_TRUE(ioThread->StartWithOptions(options), NS_ERROR_FAILURE); sIOThread = ioThread.release(); } // Establish the main thread here. rv = nsThreadManager::get()->Init(); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + if (NS_FAILED(rv)) return rv; // Set up the timer globals/timer thread rv = nsTimerImpl::Startup(); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); #ifndef ANDROID // If the locale hasn't already been setup by our embedder, - // get us out of the "C" locale and into the system + // get us out of the "C" locale and into the system if (strcmp(setlocale(LC_ALL, nullptr), "C") == 0) setlocale(LC_ALL, ""); #endif @@ -495,21 +493,18 @@ NS_InitXPCOM2(nsIServiceManager* *result, CommandLine::Init(0, nullptr); #else nsCOMPtr binaryFile; - nsDirectoryService::gService->Get(NS_XPCOM_CURRENT_PROCESS_DIR, - NS_GET_IID(nsIFile), + nsDirectoryService::gService->Get(NS_XPCOM_CURRENT_PROCESS_DIR, + NS_GET_IID(nsIFile), getter_AddRefs(binaryFile)); - if (NS_WARN_IF(!binaryFile)) - return NS_ERROR_FAILURE; - + NS_ENSURE_STATE(binaryFile); + rv = binaryFile->AppendNative(NS_LITERAL_CSTRING("nonexistent-executable")); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; - + NS_ENSURE_SUCCESS(rv, rv); + nsCString binaryPath; rv = binaryFile->GetNativePath(binaryPath); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; - + NS_ENSURE_SUCCESS(rv, rv); + static char const *const argv = { strdup(binaryPath.get()) }; CommandLine::Init(1, &argv); #endif @@ -573,7 +568,7 @@ NS_InitXPCOM2(nsIServiceManager* *result, mozilla::AvailableMemoryTracker::Activate(); // Notify observers of xpcom autoregistration start - NS_CreateServicesFromCategory(NS_XPCOM_STARTUP_CATEGORY, + NS_CreateServicesFromCategory(NS_XPCOM_STARTUP_CATEGORY, nullptr, NS_XPCOM_STARTUP_OBSERVER_ID); #ifdef XP_WIN @@ -631,9 +626,7 @@ ShutdownXPCOM(nsIServiceManager* servMgr) // Make sure the hang monitor is enabled for shutdown. HangMonitor::NotifyActivity(); - if (!NS_IsMainThread()) { - NS_RUNTIMEABORT("Shutdown on wrong thread"); - } + NS_ENSURE_STATE(NS_IsMainThread()); nsresult rv; nsCOMPtr moduleLoaders; @@ -644,8 +637,7 @@ ShutdownXPCOM(nsIServiceManager* servMgr) // servicemanager shutdown nsCOMPtr thread = do_GetCurrentThread(); - if (NS_WARN_IF(!thread)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(thread); nsRefPtr observerService; CallGetService("@mozilla.org/observer-service;1", diff --git a/xpcom/build/perfprobe.cpp b/xpcom/build/perfprobe.cpp index 6c584b50c5a..f74eb978e82 100644 --- a/xpcom/build/perfprobe.cpp +++ b/xpcom/build/perfprobe.cpp @@ -213,8 +213,7 @@ nsresult ProbeManager::StartSession(nsTArray> &aProbes) used only for unregistration*/ ); delete[] probes; - if (NS_WARN_IF(result != ERROR_SUCCESS)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_TRUE(result == ERROR_SUCCESS, NS_ERROR_UNEXPECTED); return NS_OK; } diff --git a/xpcom/components/nsCategoryManager.cpp b/xpcom/components/nsCategoryManager.cpp index 6e2b43a635a..b35dc180665 100644 --- a/xpcom/components/nsCategoryManager.cpp +++ b/xpcom/components/nsCategoryManager.cpp @@ -299,8 +299,7 @@ CategoryNode::DeleteLeaf(const char* aEntryName) NS_METHOD CategoryNode::Enumerate(nsISimpleEnumerator **_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); MutexAutoLock lock(mLock); EntryEnumerator* enumObj = EntryEnumerator::Create(mTable); @@ -594,10 +593,9 @@ nsCategoryManager::GetCategoryEntry( const char *aCategoryName, const char *aEntryName, char **_retval ) { - if (NS_WARN_IF(!aCategoryName) || - NS_WARN_IF(!aEntryName) || - NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG;; + NS_ENSURE_ARG_POINTER(aCategoryName); + NS_ENSURE_ARG_POINTER(aEntryName); + NS_ENSURE_ARG_POINTER(_retval); nsresult status = NS_ERROR_NOT_AVAILABLE; @@ -689,9 +687,8 @@ nsCategoryManager::DeleteCategoryEntry( const char *aCategoryName, const char *aEntryName, bool aDontPersist) { - if (NS_WARN_IF(!aCategoryName) || - NS_WARN_IF(!aEntryName)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aCategoryName); + NS_ENSURE_ARG_POINTER(aEntryName); /* Note: no errors are reported since failure to delete @@ -718,8 +715,7 @@ nsCategoryManager::DeleteCategoryEntry( const char *aCategoryName, NS_IMETHODIMP nsCategoryManager::DeleteCategory( const char *aCategoryName ) { - if (NS_WARN_IF(!aCategoryName)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aCategoryName); // the categories are arena-allocated, so we don't // actually delete them. We just remove all of the @@ -744,9 +740,8 @@ NS_IMETHODIMP nsCategoryManager::EnumerateCategory( const char *aCategoryName, nsISimpleEnumerator **_retval ) { - if (NS_WARN_IF(!aCategoryName) || - NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aCategoryName); + NS_ENSURE_ARG_POINTER(_retval); CategoryNode* category; { @@ -764,8 +759,7 @@ nsCategoryManager::EnumerateCategory( const char *aCategoryName, NS_IMETHODIMP nsCategoryManager::EnumerateCategories(nsISimpleEnumerator **_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); MutexAutoLock lock(mLock); CategoryEnumerator* enumObj = CategoryEnumerator::Create(mTable); diff --git a/xpcom/components/nsComponentManager.cpp b/xpcom/components/nsComponentManager.cpp index af51024ac79..0d27e9f17f8 100644 --- a/xpcom/components/nsComponentManager.cpp +++ b/xpcom/components/nsComponentManager.cpp @@ -932,9 +932,8 @@ nsComponentManagerImpl::GetClassObjectByContractID(const char *contractID, const nsIID &aIID, void **aResult) { - if (NS_WARN_IF(!aResult) || - NS_WARN_IF(!contractID)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aResult); + NS_ENSURE_ARG_POINTER(contractID); nsresult rv; @@ -1053,8 +1052,7 @@ nsComponentManagerImpl::CreateInstanceByContractID(const char *aContractID, const nsIID &aIID, void **aResult) { - if (NS_WARN_IF(!aContractID)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aContractID); // test this first, since there's no point in creating a component during // shutdown -- whether it's available or not would depend on the order it @@ -1609,9 +1607,7 @@ NS_IMETHODIMP nsComponentManagerImpl::IsContractIDRegistered(const char *aClass, bool *_retval) { - if (NS_WARN_IF(!aClass)) - return NS_ERROR_INVALID_ARG; - + NS_ENSURE_ARG_POINTER(aClass); nsFactoryEntry *entry = GetFactoryEntry(aClass, strlen(aClass)); if (entry) diff --git a/xpcom/ds/nsHashPropertyBag.cpp b/xpcom/ds/nsHashPropertyBag.cpp index 9f60de99aed..29ea0c8c3eb 100644 --- a/xpcom/ds/nsHashPropertyBag.cpp +++ b/xpcom/ds/nsHashPropertyBag.cpp @@ -64,8 +64,7 @@ nsHashPropertyBag::GetProperty(const nsAString& name, nsIVariant* *_retval) NS_IMETHODIMP nsHashPropertyBag::SetProperty(const nsAString& name, nsIVariant *value) { - if (NS_WARN_IF(!value)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(value); mPropertyHash.Put(name, value); diff --git a/xpcom/ds/nsINIParserImpl.cpp b/xpcom/ds/nsINIParserImpl.cpp index 86d6241356c..1e442143637 100644 --- a/xpcom/ds/nsINIParserImpl.cpp +++ b/xpcom/ds/nsINIParserImpl.cpp @@ -51,8 +51,7 @@ nsINIParserFactory::CreateInstance(nsISupports* aOuter, REFNSIID aIID, void **aResult) { - if (NS_WARN_IF(aOuter)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_NO_AGGREGATION(aOuter); // We are our own singleton. return QueryInterface(aIID, aResult); diff --git a/xpcom/ds/nsObserverService.cpp b/xpcom/ds/nsObserverService.cpp index f79fc75505b..b62b5d18853 100644 --- a/xpcom/ds/nsObserverService.cpp +++ b/xpcom/ds/nsObserverService.cpp @@ -150,8 +150,7 @@ ObserverServiceReporter::CollectReports(nsIMemoryReporterCallback* cb, "respect to the number of windows."), aClosure); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } rv = cb->Callback(/* process */ EmptyCString(), @@ -163,8 +162,7 @@ ObserverServiceReporter::CollectReports(nsIMemoryReporterCallback* cb, "observer service."), aClosure); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = cb->Callback(/* process */ EmptyCString(), NS_LITERAL_CSTRING("observer-service/referent/weak/alive"), @@ -175,8 +173,7 @@ ObserverServiceReporter::CollectReports(nsIMemoryReporterCallback* cb, "observer service that are still alive."), aClosure); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = cb->Callback(/* process */ EmptyCString(), NS_LITERAL_CSTRING("observer-service/referent/weak/dead"), @@ -187,8 +184,7 @@ ObserverServiceReporter::CollectReports(nsIMemoryReporterCallback* cb, "observer service that are dead."), aClosure); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); return NS_OK; } @@ -270,8 +266,7 @@ nsObserverService::AddObserver(nsIObserver* anObserver, const char* aTopic, (void*) anObserver, aTopic)); NS_ENSURE_VALIDCALL - if (NS_WARN_IF(!anObserver) || NS_WARN_IF(!aTopic)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(anObserver && aTopic); if (mozilla::net::IsNeckoChild() && !strncmp(aTopic, "http-on-", 8)) { return NS_ERROR_NOT_IMPLEMENTED; @@ -290,8 +285,7 @@ nsObserverService::RemoveObserver(nsIObserver* anObserver, const char* aTopic) LOG(("nsObserverService::RemoveObserver(%p: %s)", (void*) anObserver, aTopic)); NS_ENSURE_VALIDCALL - if (NS_WARN_IF(!anObserver) || NS_WARN_IF(!aTopic)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(anObserver && aTopic); nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic); if (!observerList) @@ -308,8 +302,7 @@ nsObserverService::EnumerateObservers(const char* aTopic, nsISimpleEnumerator** anEnumerator) { NS_ENSURE_VALIDCALL - if (NS_WARN_IF(!anEnumerator) || NS_WARN_IF(!aTopic)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aTopic && anEnumerator); nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic); if (!observerList) @@ -326,8 +319,7 @@ NS_IMETHODIMP nsObserverService::NotifyObservers(nsISupports *aSubject, LOG(("nsObserverService::NotifyObservers(%s)", aTopic)); NS_ENSURE_VALIDCALL - if (NS_WARN_IF(!aTopic)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aTopic); nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic); if (observerList) diff --git a/xpcom/ds/nsProperties.cpp b/xpcom/ds/nsProperties.cpp index 8df2e9445c8..27d1f28779e 100644 --- a/xpcom/ds/nsProperties.cpp +++ b/xpcom/ds/nsProperties.cpp @@ -15,8 +15,7 @@ NS_INTERFACE_MAP_END NS_IMETHODIMP nsProperties::Get(const char* prop, const nsIID & uuid, void* *result) { - if (NS_WARN_IF(!prop)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(prop); nsCOMPtr value; if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value))) { @@ -28,8 +27,7 @@ nsProperties::Get(const char* prop, const nsIID & uuid, void* *result) NS_IMETHODIMP nsProperties::Set(const char* prop, nsISupports* value) { - if (NS_WARN_IF(!prop)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(prop); Put(prop, value); return NS_OK; } @@ -37,8 +35,7 @@ nsProperties::Set(const char* prop, nsISupports* value) NS_IMETHODIMP nsProperties::Undefine(const char* prop) { - if (NS_WARN_IF(!prop)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(prop); nsCOMPtr value; if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value))) @@ -51,8 +48,7 @@ nsProperties::Undefine(const char* prop) NS_IMETHODIMP nsProperties::Has(const char* prop, bool *result) { - if (NS_WARN_IF(!prop)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(prop); nsCOMPtr value; *result = nsProperties_HashBase::Get(prop, @@ -86,11 +82,12 @@ GetKeysEnumerate(const char *key, nsISupports* data, NS_IMETHODIMP nsProperties::GetKeys(uint32_t *count, char ***keys) { - if (NS_WARN_IF(!count) || NS_WARN_IF(!keys)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(count); + NS_ENSURE_ARG(keys); uint32_t n = Count(); char ** k = (char **) nsMemory::Alloc(n * sizeof(char *)); + NS_ENSURE_TRUE(k, NS_ERROR_OUT_OF_MEMORY); GetKeysEnumData gked; gked.keys = k; diff --git a/xpcom/ds/nsStringEnumerator.cpp b/xpcom/ds/nsStringEnumerator.cpp index 8af28335460..c71bd5cebbe 100644 --- a/xpcom/ds/nsStringEnumerator.cpp +++ b/xpcom/ds/nsStringEnumerator.cpp @@ -84,6 +84,7 @@ NS_IMPL_ISUPPORTS3(nsStringEnumerator, NS_IMETHODIMP nsStringEnumerator::HasMore(bool* aResult) { + NS_ENSURE_ARG_POINTER(aResult); *aResult = mIndex < Count(); return NS_OK; } @@ -118,8 +119,7 @@ nsStringEnumerator::GetNext(nsISupports** aResult) NS_IMETHODIMP nsStringEnumerator::GetNext(nsAString& aResult) { - if (NS_WARN_IF(mIndex >= Count())) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_TRUE(mIndex < Count(), NS_ERROR_UNEXPECTED); if (mIsUnicode) aResult = mArray->ElementAt(mIndex++); @@ -132,8 +132,7 @@ nsStringEnumerator::GetNext(nsAString& aResult) NS_IMETHODIMP nsStringEnumerator::GetNext(nsACString& aResult) { - if (NS_WARN_IF(mIndex >= Count())) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_TRUE(mIndex < Count(), NS_ERROR_UNEXPECTED); if (mIsUnicode) CopyUTF16toUTF8(mArray->ElementAt(mIndex++), aResult); @@ -161,8 +160,8 @@ nsresult NS_NewStringEnumerator(nsIStringEnumerator** aResult, const nsTArray* aArray, nsISupports* aOwner) { - if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aResult); + NS_ENSURE_ARG_POINTER(aArray); *aResult = new nsStringEnumerator(aArray, aOwner); return StringEnumeratorTail(aResult); @@ -173,8 +172,8 @@ nsresult NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, const nsTArray* aArray, nsISupports* aOwner) { - if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aResult); + NS_ENSURE_ARG_POINTER(aArray); *aResult = new nsStringEnumerator(aArray, aOwner); return StringEnumeratorTail(aResult); @@ -184,8 +183,8 @@ nsresult NS_NewAdoptingStringEnumerator(nsIStringEnumerator** aResult, nsTArray* aArray) { - if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aResult); + NS_ENSURE_ARG_POINTER(aArray); *aResult = new nsStringEnumerator(aArray, true); return StringEnumeratorTail(aResult); @@ -195,8 +194,8 @@ nsresult NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, nsTArray* aArray) { - if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aResult); + NS_ENSURE_ARG_POINTER(aArray); *aResult = new nsStringEnumerator(aArray, true); return StringEnumeratorTail(aResult); @@ -207,8 +206,8 @@ nsresult NS_NewStringEnumerator(nsIStringEnumerator** aResult, const nsTArray* aArray) { - if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aResult); + NS_ENSURE_ARG_POINTER(aArray); *aResult = new nsStringEnumerator(aArray, false); return StringEnumeratorTail(aResult); @@ -218,8 +217,8 @@ nsresult NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, const nsTArray* aArray) { - if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aResult); + NS_ENSURE_ARG_POINTER(aArray); *aResult = new nsStringEnumerator(aArray, false); return StringEnumeratorTail(aResult); diff --git a/xpcom/ds/nsSupportsArray.cpp b/xpcom/ds/nsSupportsArray.cpp index 1c742f0eff6..176c73cf28d 100644 --- a/xpcom/ds/nsSupportsArray.cpp +++ b/xpcom/ds/nsSupportsArray.cpp @@ -585,8 +585,7 @@ nsSupportsArray::Clone(nsISupportsArray** aResult) { nsCOMPtr newArray; nsresult rv = NS_NewISupportsArray(getter_AddRefs(newArray)); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); uint32_t count = 0; Count(&count); diff --git a/xpcom/ds/nsSupportsPrimitives.cpp b/xpcom/ds/nsSupportsPrimitives.cpp index fb6d61427fa..a5c4f1d6146 100644 --- a/xpcom/ds/nsSupportsPrimitives.cpp +++ b/xpcom/ds/nsSupportsPrimitives.cpp @@ -810,8 +810,7 @@ nsSupportsDependentCString::nsSupportsDependentCString(const char* aStr) NS_IMETHODIMP nsSupportsDependentCString::GetType(uint16_t *aType) { - if (NS_WARN_IF(!aType)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aType); *aType = TYPE_CSTRING; return NS_OK; @@ -827,8 +826,7 @@ nsSupportsDependentCString::GetData(nsACString& aData) NS_IMETHODIMP nsSupportsDependentCString::ToString(char **_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); *_retval = ToNewCString(mData); if (!*_retval) diff --git a/xpcom/ds/nsWindowsRegKey.cpp b/xpcom/ds/nsWindowsRegKey.cpp index 25b51b4e79b..524e382061b 100644 --- a/xpcom/ds/nsWindowsRegKey.cpp +++ b/xpcom/ds/nsWindowsRegKey.cpp @@ -103,8 +103,7 @@ NS_IMETHODIMP nsWindowsRegKey::OpenChild(const nsAString &path, uint32_t mode, nsIWindowsRegKey **result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); nsCOMPtr child = new nsWindowsRegKey(); @@ -120,8 +119,7 @@ NS_IMETHODIMP nsWindowsRegKey::CreateChild(const nsAString &path, uint32_t mode, nsIWindowsRegKey **result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); nsCOMPtr child = new nsWindowsRegKey(); @@ -136,15 +134,13 @@ nsWindowsRegKey::CreateChild(const nsAString &path, uint32_t mode, NS_IMETHODIMP nsWindowsRegKey::GetChildCount(uint32_t *result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); DWORD numSubKeys; LONG rv = RegQueryInfoKeyW(mKey, nullptr, nullptr, nullptr, &numSubKeys, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr); - if (rv != ERROR_SUCCESS) - return NS_ERROR_FAILURE; + NS_ENSURE_STATE(rv == ERROR_SUCCESS); *result = numSubKeys; return NS_OK; @@ -153,8 +149,7 @@ nsWindowsRegKey::GetChildCount(uint32_t *result) NS_IMETHODIMP nsWindowsRegKey::GetChildName(uint32_t index, nsAString &result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); FILETIME lastWritten; @@ -174,8 +169,7 @@ nsWindowsRegKey::GetChildName(uint32_t index, nsAString &result) NS_IMETHODIMP nsWindowsRegKey::HasChild(const nsAString &name, bool *result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); // Check for the existence of a child key by opening the key with minimal // rights. Perhaps there is a more efficient way to do this? @@ -193,15 +187,13 @@ nsWindowsRegKey::HasChild(const nsAString &name, bool *result) NS_IMETHODIMP nsWindowsRegKey::GetValueCount(uint32_t *result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); DWORD numValues; LONG rv = RegQueryInfoKeyW(mKey, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &numValues, nullptr, nullptr, nullptr, nullptr); - if (rv != ERROR_SUCCESS) - return NS_ERROR_FAILURE; + NS_ENSURE_STATE(rv == ERROR_SUCCESS); *result = numValues; return NS_OK; @@ -210,8 +202,7 @@ nsWindowsRegKey::GetValueCount(uint32_t *result) NS_IMETHODIMP nsWindowsRegKey::GetValueName(uint32_t index, nsAString &result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); PRUnichar nameBuf[MAX_VALUE_NAME_LEN]; DWORD nameLen = sizeof(nameBuf) / sizeof(nameBuf[0]); @@ -229,8 +220,7 @@ nsWindowsRegKey::GetValueName(uint32_t index, nsAString &result) NS_IMETHODIMP nsWindowsRegKey::HasValue(const nsAString &name, bool *result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, nullptr, nullptr, nullptr); @@ -242,8 +232,7 @@ nsWindowsRegKey::HasValue(const nsAString &name, bool *result) NS_IMETHODIMP nsWindowsRegKey::RemoveChild(const nsAString &name) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); LONG rv = RegDeleteKeyW(mKey, PromiseFlatString(name).get()); @@ -253,8 +242,7 @@ nsWindowsRegKey::RemoveChild(const nsAString &name) NS_IMETHODIMP nsWindowsRegKey::RemoveValue(const nsAString &name) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); LONG rv = RegDeleteValueW(mKey, PromiseFlatString(name).get()); @@ -264,8 +252,7 @@ nsWindowsRegKey::RemoveValue(const nsAString &name) NS_IMETHODIMP nsWindowsRegKey::GetValueType(const nsAString &name, uint32_t *result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, (LPDWORD) result, nullptr, nullptr); @@ -276,8 +263,7 @@ nsWindowsRegKey::GetValueType(const nsAString &name, uint32_t *result) NS_IMETHODIMP nsWindowsRegKey::ReadStringValue(const nsAString &name, nsAString &result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); DWORD type, size; @@ -289,12 +275,12 @@ nsWindowsRegKey::ReadStringValue(const nsAString &name, nsAString &result) // This must be a string type in order to fetch the value as a string. // We're being a bit forgiving here by allowing types other than REG_SZ. - if (type != REG_SZ && type == REG_EXPAND_SZ && type == REG_MULTI_SZ) - return NS_ERROR_FAILURE; + NS_ENSURE_STATE(type == REG_SZ || + type == REG_EXPAND_SZ || + type == REG_MULTI_SZ); // The buffer size must be a multiple of 2. - if (size % 2 != 0) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(size % 2 == 0); if (size == 0) { result.Truncate(); @@ -351,8 +337,7 @@ nsWindowsRegKey::ReadStringValue(const nsAString &name, nsAString &result) NS_IMETHODIMP nsWindowsRegKey::ReadIntValue(const nsAString &name, uint32_t *result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); DWORD size = sizeof(*result); LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, nullptr, @@ -364,8 +349,7 @@ nsWindowsRegKey::ReadIntValue(const nsAString &name, uint32_t *result) NS_IMETHODIMP nsWindowsRegKey::ReadInt64Value(const nsAString &name, uint64_t *result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); DWORD size = sizeof(*result); LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, nullptr, @@ -377,8 +361,7 @@ nsWindowsRegKey::ReadInt64Value(const nsAString &name, uint64_t *result) NS_IMETHODIMP nsWindowsRegKey::ReadBinaryValue(const nsAString &name, nsACString &result) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); DWORD size; LONG rv = RegQueryValueExW(mKey, PromiseFlatString(name).get(), 0, @@ -402,8 +385,7 @@ nsWindowsRegKey::ReadBinaryValue(const nsAString &name, nsACString &result) NS_IMETHODIMP nsWindowsRegKey::WriteStringValue(const nsAString &name, const nsAString &value) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); // Need to indicate complete size of buffer including null terminator. const nsString &flatValue = PromiseFlatString(value); @@ -418,8 +400,7 @@ nsWindowsRegKey::WriteStringValue(const nsAString &name, const nsAString &value) NS_IMETHODIMP nsWindowsRegKey::WriteIntValue(const nsAString &name, uint32_t value) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); LONG rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_DWORD, (const BYTE *) &value, sizeof(value)); @@ -430,8 +411,7 @@ nsWindowsRegKey::WriteIntValue(const nsAString &name, uint32_t value) NS_IMETHODIMP nsWindowsRegKey::WriteInt64Value(const nsAString &name, uint64_t value) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); LONG rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_QWORD, (const BYTE *) &value, sizeof(value)); @@ -442,8 +422,7 @@ nsWindowsRegKey::WriteInt64Value(const nsAString &name, uint64_t value) NS_IMETHODIMP nsWindowsRegKey::WriteBinaryValue(const nsAString &name, const nsACString &value) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); const nsCString &flatValue = PromiseFlatCString(value); LONG rv = RegSetValueExW(mKey, PromiseFlatString(name).get(), 0, REG_BINARY, @@ -455,8 +434,7 @@ nsWindowsRegKey::WriteBinaryValue(const nsAString &name, const nsACString &value NS_IMETHODIMP nsWindowsRegKey::StartWatching(bool recurse) { - if (NS_WARN_IF(!mKey)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); if (mWatchEvent) return NS_OK; diff --git a/xpcom/glue/nsArrayEnumerator.cpp b/xpcom/glue/nsArrayEnumerator.cpp index 5c2588f5974..572fc665a43 100644 --- a/xpcom/glue/nsArrayEnumerator.cpp +++ b/xpcom/glue/nsArrayEnumerator.cpp @@ -180,6 +180,7 @@ nsCOMArrayEnumerator::operator new (size_t size, const nsCOMArray_base& aArray) // do the actual allocation nsCOMArrayEnumerator * result = static_cast(::operator new(size)); + NS_ENSURE_TRUE(result, nullptr); // now need to copy over the values, and addref each one // now this might seem like a lot of work, but we're actually just diff --git a/xpcom/glue/nsCOMArray.cpp b/xpcom/glue/nsCOMArray.cpp index 7b227a6c5a3..99b1b5e481a 100644 --- a/xpcom/glue/nsCOMArray.cpp +++ b/xpcom/glue/nsCOMArray.cpp @@ -57,8 +57,7 @@ int32_t nsCOMArray_base::IndexOfObject(nsISupports* aObject) const { nsCOMPtr supports = do_QueryInterface(aObject); - if (NS_WARN_IF(!supports)) - return -1; + NS_ENSURE_TRUE(supports, -1); uint32_t i, count; int32_t retval = -1; diff --git a/xpcom/glue/nsComponentManagerUtils.cpp b/xpcom/glue/nsComponentManagerUtils.cpp index 21decc4f4fa..f20d5e72e3b 100644 --- a/xpcom/glue/nsComponentManagerUtils.cpp +++ b/xpcom/glue/nsComponentManagerUtils.cpp @@ -46,8 +46,7 @@ nsresult CallGetService(const nsCID &aCID, const nsIID &aIID, void **aResult) { nsComponentManagerImpl *compMgr = nsComponentManagerImpl::gComponentManager; - if (NS_WARN_IF(!compMgr)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(compMgr, NS_ERROR_NOT_INITIALIZED); return compMgr->nsComponentManagerImpl::GetService(aCID, aIID, aResult); } @@ -56,8 +55,7 @@ nsresult CallGetService(const char *aContractID, const nsIID &aIID, void **aResult) { nsComponentManagerImpl *compMgr = nsComponentManagerImpl::gComponentManager; - if (NS_WARN_IF(!compMgr)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(compMgr, NS_ERROR_NOT_INITIALIZED); return compMgr-> nsComponentManagerImpl::GetServiceByContractID(aContractID, @@ -121,8 +119,7 @@ CallCreateInstance(const nsCID &aCID, nsISupports *aDelegate, const nsIID &aIID, void **aResult) { nsComponentManagerImpl *compMgr = nsComponentManagerImpl::gComponentManager; - if (NS_WARN_IF(!compMgr)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(compMgr, NS_ERROR_NOT_INITIALIZED); return compMgr-> nsComponentManagerImpl::CreateInstance(aCID, aDelegate, aIID, aResult); @@ -133,8 +130,7 @@ CallCreateInstance(const char *aContractID, nsISupports *aDelegate, const nsIID &aIID, void **aResult) { nsComponentManagerImpl *compMgr = nsComponentManagerImpl::gComponentManager; - if (NS_WARN_IF(!compMgr)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(compMgr, NS_ERROR_NOT_INITIALIZED); return compMgr-> nsComponentManagerImpl::CreateInstanceByContractID(aContractID, @@ -146,8 +142,7 @@ nsresult CallGetClassObject(const nsCID &aCID, const nsIID &aIID, void **aResult) { nsComponentManagerImpl *compMgr = nsComponentManagerImpl::gComponentManager; - if (NS_WARN_IF(!compMgr)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(compMgr, NS_ERROR_NOT_INITIALIZED); return compMgr-> nsComponentManagerImpl::GetClassObject(aCID, aIID, aResult); @@ -157,8 +152,7 @@ nsresult CallGetClassObject(const char *aContractID, const nsIID &aIID, void **aResult) { nsComponentManagerImpl *compMgr = nsComponentManagerImpl::gComponentManager; - if (NS_WARN_IF(!compMgr)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(compMgr, NS_ERROR_NOT_INITIALIZED); return compMgr-> nsComponentManagerImpl::GetClassObjectByContractID(aContractID, aIID, diff --git a/xpcom/glue/nsDebug.h b/xpcom/glue/nsDebug.h index 8d3f543829c..1e656c8127f 100644 --- a/xpcom/glue/nsDebug.h +++ b/xpcom/glue/nsDebug.h @@ -12,7 +12,7 @@ #ifndef nsError_h__ #include "nsError.h" -#endif +#endif #include "nsXPCOM.h" #include "mozilla/Assertions.h" @@ -22,35 +22,7 @@ #include "prprf.h" #endif -/** - * Warn if the given condition is true. The condition is evaluated in both - * release and debug builds, and the result is an expression which can be - * used in subsequent expressions, such as: - * - * if (NS_WARN_IF(NS_FAILED(rv)) - * return rv; - * - * This explicit warning and return is preferred to the NS_ENSURE_* macros - * which hide the warning and the return control flow. - * - * @note This is C++-only - */ -#ifdef __cplusplus #ifdef DEBUG -inline bool NS_warn_if_impl(bool condition, const char* expr, const char* file, - int32_t line) -{ - if (MOZ_UNLIKELY(condition)) { - NS_DebugBreak(NS_DEBUG_WARNING, nullptr, expr, file, line); - } - return condition; -} -#define NS_WARN_IF(condition) \ - NS_warn_if_impl(condition, #condition, __FILE__, __LINE__) -#else -#define NS_WARN_IF(condition) (bool)(condition) -#endif -#endif /** * Abort the execution of the program if the expression evaluates to @@ -68,16 +40,12 @@ inline bool NS_warn_if_impl(bool condition, const char* expr, const char* file, * Note also that the non-debug version of this macro does not * evaluate the message argument. */ -#ifdef DEBUG #define NS_ABORT_IF_FALSE(_expr, _msg) \ do { \ if (!(_expr)) { \ NS_DebugBreak(NS_DEBUG_ABORT, _msg, #_expr, __FILE__, __LINE__); \ } \ } while(0) -#else -#define NS_ABORT_IF_FALSE(_expr, _msg) do { /* nothing */ } while(0) -#endif /** * Warn if a given condition is false. @@ -87,105 +55,103 @@ inline bool NS_warn_if_impl(bool condition, const char* expr, const char* file, * Note also that the non-debug version of this macro does not * evaluate the message argument. */ -#ifdef DEBUG #define NS_WARN_IF_FALSE(_expr,_msg) \ do { \ if (!(_expr)) { \ NS_DebugBreak(NS_DEBUG_WARNING, _msg, #_expr, __FILE__, __LINE__); \ } \ } while(0) -#else -#define NS_WARN_IF_FALSE(_expr, _msg) do { /* nothing */ } while(0) -#endif +/** + * Test a precondition for truth. If the expression is not true then + * trigger a program failure. + */ +#define NS_PRECONDITION(expr, str) \ + do { \ + if (!(expr)) { \ + NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \ + } \ + } while(0) /** * Test an assertion for truth. If the expression is not true then * trigger a program failure. - * - * Note that the non-debug version of this macro does not - * evaluate the message argument. */ -#ifdef DEBUG #define NS_ASSERTION(expr, str) \ do { \ if (!(expr)) { \ NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \ } \ } while(0) -#else -#define NS_ASSERTION(expr, str) do { /* nothing */ } while(0) -#endif /** - * NS_PRECONDITION/POSTCONDITION are synonyms for NS_ASSERTION. + * Test a post-condition for truth. If the expression is not true then + * trigger a program failure. */ -#define NS_PRECONDITION(expr, str) NS_ASSERTION(expr, str) -#define NS_POSTCONDITION(expr, str) NS_ASSERTION(expr, str) +#define NS_POSTCONDITION(expr, str) \ + do { \ + if (!(expr)) { \ + NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \ + } \ + } while(0) /** * This macros triggers a program failure if executed. It indicates that * an attempt was made to execute some unimplemented functionality. */ -#ifdef DEBUG #define NS_NOTYETIMPLEMENTED(str) \ NS_DebugBreak(NS_DEBUG_ASSERTION, str, "NotYetImplemented", __FILE__, __LINE__) -#else -#define NS_NOTYETIMPLEMENTED(str) do { /* nothing */ } while(0) -#endif /** * This macros triggers a program failure if executed. It indicates that - * an attempt was made to execute a codepath which should not be reachable. + * an attempt was made to execute some unimplemented functionality. */ -#ifdef DEBUG #define NS_NOTREACHED(str) \ NS_DebugBreak(NS_DEBUG_ASSERTION, str, "Not Reached", __FILE__, __LINE__) -#else -#define NS_NOTREACHED(str) do { /* nothing */ } while(0) -#endif /** * Log an error message. */ -#ifdef DEBUG #define NS_ERROR(str) \ NS_DebugBreak(NS_DEBUG_ASSERTION, str, "Error", __FILE__, __LINE__) -#else -#define NS_ERROR(str) do { /* nothing */ } while(0) -#endif /** * Log a warning message. */ -#ifdef DEBUG #define NS_WARNING(str) \ NS_DebugBreak(NS_DEBUG_WARNING, str, nullptr, __FILE__, __LINE__) -#else -#define NS_WARNING(str) do { /* nothing */ } while(0) -#endif /** - * Trigger an debug-only abort. - * - * @see NS_RUNTIMEABORT for release-mode asserts. + * Trigger an abort */ -#ifdef DEBUG #define NS_ABORT() \ NS_DebugBreak(NS_DEBUG_ABORT, nullptr, nullptr, __FILE__, __LINE__) -#else -#define NS_ABORT() do { /* nothing */ } while(0) -#endif /** - * Trigger a debugger breakpoint, only in debug builds. + * Cause a break */ -#ifdef DEBUG #define NS_BREAK() \ NS_DebugBreak(NS_DEBUG_BREAK, nullptr, nullptr, __FILE__, __LINE__) -#else + +#else /* DEBUG */ + +/** + * The non-debug version of these macros do not evaluate the + * expression or the message arguments to the macro. + */ +#define NS_ABORT_IF_FALSE(_expr, _msg) do { /* nothing */ } while(0) +#define NS_WARN_IF_FALSE(_expr, _msg) do { /* nothing */ } while(0) +#define NS_PRECONDITION(expr, str) do { /* nothing */ } while(0) +#define NS_ASSERTION(expr, str) do { /* nothing */ } while(0) +#define NS_POSTCONDITION(expr, str) do { /* nothing */ } while(0) +#define NS_NOTYETIMPLEMENTED(str) do { /* nothing */ } while(0) +#define NS_NOTREACHED(str) do { /* nothing */ } while(0) +#define NS_ERROR(str) do { /* nothing */ } while(0) +#define NS_WARNING(str) do { /* nothing */ } while(0) +#define NS_ABORT() do { /* nothing */ } while(0) #define NS_BREAK() do { /* nothing */ } while(0) -#endif + +#endif /* ! DEBUG */ /****************************************************************************** ** Macros for static assertions. These are used by the sixgill tool. @@ -264,7 +230,7 @@ inline bool NS_warn_if_impl(bool condition, const char* expr, const char* file, /****************************************************************************** ** Macros for terminating execution when an unrecoverable condition is -** reached. These need to be compiled regardless of the DEBUG flag. +** reached. These need to be compiled regardless of the DEBUG flag. ******************************************************************************/ /** @@ -276,11 +242,10 @@ inline bool NS_warn_if_impl(bool condition, const char* expr, const char* file, NS_DebugBreak(NS_DEBUG_ABORT, msg, nullptr, __FILE__, __LINE__) -/* Macros for checking the trueness of an expression passed in within an - * interface implementation. These need to be compiled regardless of the - * DEBUG flag. New code should use NS_WARN_IF(condition) instead! - * @status deprecated - */ +/* Macros for checking the trueness of an expression passed in within an + * interface implementation. These need to be compiled regardless of the */ +/* DEBUG flag +******************************************************************************/ #define NS_ENSURE_TRUE(x, ret) \ do { \ @@ -375,6 +340,9 @@ inline bool NS_warn_if_impl(bool condition, const char* expr, const char* file, #define NS_ENSURE_NO_AGGREGATION(outer) \ NS_ENSURE_FALSE(outer, NS_ERROR_NO_AGGREGATION) +#define NS_ENSURE_PROPER_AGGREGATION(outer, iid) \ + NS_ENSURE_FALSE(outer && !iid.Equals(NS_GET_IID(nsISupports)), NS_ERROR_INVALID_ARG) + /*****************************************************************************/ #ifdef XPCOM_GLUE diff --git a/xpcom/glue/nsINIParser.cpp b/xpcom/glue/nsINIParser.cpp index 9ce71aa9b0f..918c2f2427d 100644 --- a/xpcom/glue/nsINIParser.cpp +++ b/xpcom/glue/nsINIParser.cpp @@ -65,8 +65,7 @@ nsINIParser::Init(nsIFile* aFile) #ifdef XP_WIN nsAutoString path; nsresult rv = aFile->GetPath(path); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); fd = _wfopen(path.get(), READ_BINARYMODE); #else diff --git a/xpcom/glue/nsMemory.cpp b/xpcom/glue/nsMemory.cpp index f77567a3f68..6b63420c9ec 100644 --- a/xpcom/glue/nsMemory.cpp +++ b/xpcom/glue/nsMemory.cpp @@ -19,8 +19,7 @@ nsMemory::HeapMinimize(bool aImmediate) { nsCOMPtr mem; nsresult rv = NS_GetMemoryManager(getter_AddRefs(mem)); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); return mem->HeapMinimize(aImmediate); } diff --git a/xpcom/glue/nsThreadUtils.cpp b/xpcom/glue/nsThreadUtils.cpp index c89faa4e774..a99c2dea3ca 100644 --- a/xpcom/glue/nsThreadUtils.cpp +++ b/xpcom/glue/nsThreadUtils.cpp @@ -70,18 +70,15 @@ NS_NewThread(nsIThread **result, nsIRunnable *event, uint32_t stackSize) nsresult rv; nsCOMPtr mgr = do_GetService(NS_THREADMANAGER_CONTRACTID, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = mgr->NewThread(0, stackSize, getter_AddRefs(thread)); #endif - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); if (event) { rv = thread->Dispatch(event, NS_DISPATCH_NORMAL); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } *result = nullptr; @@ -98,8 +95,7 @@ NS_GetCurrentThread(nsIThread **result) nsresult rv; nsCOMPtr mgr = do_GetService(NS_THREADMANAGER_CONTRACTID, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); return mgr->GetCurrentThread(result); #endif } @@ -113,8 +109,7 @@ NS_GetMainThread(nsIThread **result) nsresult rv; nsCOMPtr mgr = do_GetService(NS_THREADMANAGER_CONTRACTID, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); return mgr->GetMainThread(result); #endif } @@ -166,8 +161,7 @@ NS_DispatchToCurrentThread(nsIRunnable *event) #else nsCOMPtr thread; nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread)); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); #endif return thread->Dispatch(event, NS_DISPATCH_NORMAL); } @@ -177,8 +171,7 @@ NS_DispatchToMainThread(nsIRunnable *event, uint32_t dispatchFlags) { nsCOMPtr thread; nsresult rv = NS_GetMainThread(getter_AddRefs(thread)); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); return thread->Dispatch(event, dispatchFlags); } @@ -191,15 +184,13 @@ NS_ProcessPendingEvents(nsIThread *thread, PRIntervalTime timeout) #ifdef MOZILLA_INTERNAL_API if (!thread) { thread = NS_GetCurrentThread(); - if (NS_WARN_IF(!thread)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(thread); } #else nsCOMPtr current; if (!thread) { rv = NS_GetCurrentThread(getter_AddRefs(current)); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); thread = current.get(); } #endif @@ -234,8 +225,7 @@ NS_HasPendingEvents(nsIThread *thread) return hasPendingEvents(current); #else thread = NS_GetCurrentThread(); - if (NS_WARN_IF(!thread)) - return false; + NS_ENSURE_TRUE(thread, false); #endif } return hasPendingEvents(thread); @@ -247,15 +237,13 @@ NS_ProcessNextEvent(nsIThread *thread, bool mayWait) #ifdef MOZILLA_INTERNAL_API if (!thread) { thread = NS_GetCurrentThread(); - if (NS_WARN_IF(!thread)) - return false; + NS_ENSURE_TRUE(thread, false); } #else nsCOMPtr current; if (!thread) { NS_GetCurrentThread(getter_AddRefs(current)); - if (NS_WARN_IF(!current)) - return false; + NS_ENSURE_TRUE(current, false); thread = current.get(); } #endif diff --git a/xpcom/glue/nsThreadUtils.h b/xpcom/glue/nsThreadUtils.h index c567de3a466..e53cc013153 100644 --- a/xpcom/glue/nsThreadUtils.h +++ b/xpcom/glue/nsThreadUtils.h @@ -71,8 +71,7 @@ NS_NewNamedThread(const char (&name)[LEN], uint32_t stackSize = nsIThreadManager::DEFAULT_STACK_SIZE) { nsresult rv = NS_NewThread(result, nullptr, stackSize); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); NS_SetThreadName(*result, name); if (initialEvent) { rv = (*result)->Dispatch(initialEvent, NS_DISPATCH_NORMAL); diff --git a/xpcom/io/Base64.cpp b/xpcom/io/Base64.cpp index 4a6419eca57..94fec02775a 100644 --- a/xpcom/io/Base64.cpp +++ b/xpcom/io/Base64.cpp @@ -163,8 +163,7 @@ EncodeInputStream(nsIInputStream *aInputStream, if (!aCount) { rv = aInputStream->Available(&count64); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); // if count64 is over 4GB, it will be failed at the below condition, // then will return NS_ERROR_OUT_OF_MEMORY aCount = (uint32_t)count64; diff --git a/xpcom/io/CocoaFileUtils.mm b/xpcom/io/CocoaFileUtils.mm index 3f687274553..ca4e1b4fc3b 100644 --- a/xpcom/io/CocoaFileUtils.mm +++ b/xpcom/io/CocoaFileUtils.mm @@ -16,8 +16,7 @@ nsresult RevealFileInFinder(CFURLRef url) { NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; - if (NS_WARN_IF(!url)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(url); NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init]; BOOL success = [[NSWorkspace sharedWorkspace] selectFile:[(NSURL*)url path] inFileViewerRootedAtPath:@""]; @@ -32,8 +31,7 @@ nsresult OpenURL(CFURLRef url) { NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; - if (NS_WARN_IF(!url)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(url); NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init]; BOOL success = [[NSWorkspace sharedWorkspace] openURL:(NSURL*)url]; @@ -48,8 +46,8 @@ nsresult GetFileCreatorCode(CFURLRef url, OSType *creatorCode) { NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; - if (NS_WARN_IF(!url) || NS_WARN_IF(!creatorCode)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(url); + NS_ENSURE_ARG_POINTER(creatorCode); nsAutoreleasePool localPool; @@ -78,8 +76,7 @@ nsresult SetFileCreatorCode(CFURLRef url, OSType creatorCode) { NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; - if (NS_WARN_IF(!url)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(url); NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init]; NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:creatorCode] forKey:NSFileHFSCreatorCode]; @@ -94,8 +91,8 @@ nsresult GetFileTypeCode(CFURLRef url, OSType *typeCode) { NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; - if (NS_WARN_IF(!url) || NS_WARN_IF(!typeCode)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(url); + NS_ENSURE_ARG_POINTER(typeCode); nsAutoreleasePool localPool; @@ -124,8 +121,7 @@ nsresult SetFileTypeCode(CFURLRef url, OSType typeCode) { NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; - if (NS_WARN_IF(!url)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(url); NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init]; NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:typeCode] forKey:NSFileHFSTypeCode]; diff --git a/xpcom/io/nsAnonymousTemporaryFile.cpp b/xpcom/io/nsAnonymousTemporaryFile.cpp index de0cc43caf2..aaac18958a2 100644 --- a/xpcom/io/nsAnonymousTemporaryFile.cpp +++ b/xpcom/io/nsAnonymousTemporaryFile.cpp @@ -50,23 +50,19 @@ using namespace mozilla; static nsresult GetTempDir(nsIFile** aTempDir) { - if (NS_WARN_IF(!aTempDir)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aTempDir); nsCOMPtr tmpFile; nsresult rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(tmpFile)); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv,rv); #ifdef XP_WIN // On windows DELETE_ON_CLOSE is unreliable, so we store temporary files // in a subdir of the temp dir and delete that in an idle service observer // to ensure it's been cleared. rv = tmpFile->AppendNative(nsDependentCString("mozilla-temp-files")); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv,rv); rv = tmpFile->Create(nsIFile::DIRECTORY_TYPE, 0700); - if (rv != NS_ERROR_FILE_ALREADY_EXISTS && NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_TRUE(rv == NS_ERROR_FILE_ALREADY_EXISTS || NS_SUCCEEDED(rv), rv); #endif tmpFile.forget(aTempDir); @@ -77,14 +73,12 @@ GetTempDir(nsIFile** aTempDir) nsresult NS_OpenAnonymousTemporaryFile(PRFileDesc** aOutFileDesc) { - if (NS_WARN_IF(!aOutFileDesc)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aOutFileDesc); nsresult rv; nsCOMPtr tmpFile; rv = GetTempDir(getter_AddRefs(tmpFile)); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv,rv); // Give the temp file a name with a random element. CreateUnique will also // append a counter to the name if it encounters a name collision. Adding @@ -96,12 +90,10 @@ NS_OpenAnonymousTemporaryFile(PRFileDesc** aOutFileDesc) name.AppendInt(rand()); rv = tmpFile->AppendNative(name); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv,rv); rv = tmpFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0700); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv,rv); rv = tmpFile->OpenNSPRFileDesc(PR_RDWR | nsIFile::DELETE_ON_CLOSE, PR_IRWXU, aOutFileDesc); @@ -154,19 +146,16 @@ public: // service is installed when running in xpcshell, and this interferes with // the fake idle service, causing xpcshell-test failures. mTimer = do_CreateInstance(NS_TIMER_CONTRACTID); - if (NS_WARN_IF(!mTimer)) - return NS_ERROR_FAILURE; + NS_ENSURE_TRUE(mTimer != nullptr, NS_ERROR_FAILURE); nsresult rv = mTimer->Init(this, SCHEDULE_TIMEOUT_MS, nsITimer::TYPE_ONE_SHOT); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); // Register shutdown observer so we can cancel the timer if we shutdown before // the timer runs. nsCOMPtr obsSrv = services::GetObserverService(); - if (NS_WARN_IF(!obsSrv)) - return NS_ERROR_FAILURE; + NS_ENSURE_TRUE(obsSrv != nullptr, NS_ERROR_FAILURE); return obsSrv->AddObserver(this, XPCOM_SHUTDOWN_TOPIC, false); } @@ -222,8 +211,7 @@ public: void RemoveAnonTempFileFiles() { nsCOMPtr tmpDir; nsresult rv = GetTempDir(getter_AddRefs(tmpDir)); - if (NS_WARN_IF(NS_FAILED(rv))) - return; + NS_ENSURE_SUCCESS_VOID(rv); // Remove the directory recursively. tmpDir->Remove(true); diff --git a/xpcom/io/nsAppFileLocationProvider.cpp b/xpcom/io/nsAppFileLocationProvider.cpp index c0f77437c7a..b0243c514e7 100644 --- a/xpcom/io/nsAppFileLocationProvider.cpp +++ b/xpcom/io/nsAppFileLocationProvider.cpp @@ -87,12 +87,10 @@ NS_IMPL_ISUPPORTS2(nsAppFileLocationProvider, nsIDirectoryServiceProvider, nsIDi NS_IMETHODIMP nsAppFileLocationProvider::GetFile(const char *prop, bool *persistent, nsIFile **_retval) { - if (NS_WARN_IF(!prop)) - return NS_ERROR_INVALID_ARG; - nsCOMPtr localFile; nsresult rv = NS_ERROR_FAILURE; + NS_ENSURE_ARG(prop); *_retval = nullptr; *persistent = true; @@ -252,8 +250,7 @@ nsAppFileLocationProvider::GetFile(const char *prop, bool *persistent, nsIFile * NS_METHOD nsAppFileLocationProvider::CloneMozBinDirectory(nsIFile **aLocalFile) { - if (NS_WARN_IF(!aLocalFile)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aLocalFile); nsresult rv; if (!mMozBinDirectory) @@ -294,8 +291,7 @@ NS_METHOD nsAppFileLocationProvider::CloneMozBinDirectory(nsIFile **aLocalFile) //---------------------------------------------------------------------------------------- NS_METHOD nsAppFileLocationProvider::GetProductDirectory(nsIFile **aLocalFile, bool aLocal) { - if (NS_WARN_IF(!aLocalFile)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aLocalFile); nsresult rv; bool exists; @@ -356,8 +352,7 @@ NS_METHOD nsAppFileLocationProvider::GetProductDirectory(nsIFile **aLocalFile, b //---------------------------------------------------------------------------------------- NS_METHOD nsAppFileLocationProvider::GetDefaultUserProfileRoot(nsIFile **aLocalFile, bool aLocal) { - if (NS_WARN_IF(!aLocalFile)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aLocalFile); nsresult rv; nsCOMPtr localDir; @@ -421,8 +416,7 @@ class nsAppDirectoryEnumerator : public nsISimpleEnumerator NS_IMETHOD GetNext(nsISupports **result) { - if (NS_WARN_IF(!result)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(result); *result = nullptr; bool hasMore; @@ -517,8 +511,7 @@ class nsPathsDirectoryEnumerator : public nsAppDirectoryEnumerator NS_IMETHODIMP nsAppFileLocationProvider::GetFiles(const char *prop, nsISimpleEnumerator **_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); *_retval = nullptr; nsresult rv = NS_ERROR_FAILURE; diff --git a/xpcom/io/nsBinaryStream.cpp b/xpcom/io/nsBinaryStream.cpp index 67784f9eea5..e3917845870 100644 --- a/xpcom/io/nsBinaryStream.cpp +++ b/xpcom/io/nsBinaryStream.cpp @@ -34,24 +34,21 @@ NS_IMPL_ISUPPORTS3(nsBinaryOutputStream, nsIObjectOutputStream, nsIBinaryOutputS NS_IMETHODIMP nsBinaryOutputStream::Flush() { - if (NS_WARN_IF(!mOutputStream)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mOutputStream); return mOutputStream->Flush(); } NS_IMETHODIMP nsBinaryOutputStream::Close() { - if (NS_WARN_IF(!mOutputStream)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mOutputStream); return mOutputStream->Close(); } NS_IMETHODIMP nsBinaryOutputStream::Write(const char *aBuf, uint32_t aCount, uint32_t *aActualBytes) { - if (NS_WARN_IF(!mOutputStream)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mOutputStream); return mOutputStream->Write(aBuf, aCount, aActualBytes); } @@ -72,16 +69,14 @@ nsBinaryOutputStream::WriteSegments(nsReadSegmentFun reader, void * closure, uin NS_IMETHODIMP nsBinaryOutputStream::IsNonBlocking(bool *aNonBlocking) { - if (NS_WARN_IF(!mOutputStream)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mOutputStream); return mOutputStream->IsNonBlocking(aNonBlocking); } nsresult nsBinaryOutputStream::WriteFully(const char *aBuf, uint32_t aCount) { - if (NS_WARN_IF(!mOutputStream)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mOutputStream); nsresult rv; uint32_t bytesWritten; @@ -96,8 +91,7 @@ nsBinaryOutputStream::WriteFully(const char *aBuf, uint32_t aCount) NS_IMETHODIMP nsBinaryOutputStream::SetOutputStream(nsIOutputStream *aOutputStream) { - if (NS_WARN_IF(!aOutputStream)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aOutputStream); mOutputStream = aOutputStream; mBufferAccess = do_QueryInterface(aOutputStream); return NS_OK; @@ -251,25 +245,23 @@ nsBinaryOutputStream::WriteCompoundObject(nsISupports* aObject, const nsIID& aIID, bool aIsStrongRef) { - nsCOMPtr classInfo = do_QueryInterface(aObject); - nsCOMPtr serializable = do_QueryInterface(aObject); - // Can't deal with weak refs - if (NS_WARN_IF(!aIsStrongRef)) - return NS_ERROR_UNEXPECTED; - if (NS_WARN_IF(!classInfo) || NS_WARN_IF(!serializable)) - return NS_ERROR_NOT_AVAILABLE; + NS_ENSURE_TRUE(aIsStrongRef, NS_ERROR_UNEXPECTED); + + nsCOMPtr classInfo = do_QueryInterface(aObject); + NS_ENSURE_TRUE(classInfo, NS_ERROR_NOT_AVAILABLE); + + nsCOMPtr serializable = do_QueryInterface(aObject); + NS_ENSURE_TRUE(serializable, NS_ERROR_NOT_AVAILABLE); nsCID cid; classInfo->GetClassIDNoAlloc(&cid); nsresult rv = WriteID(cid); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; - + NS_ENSURE_SUCCESS(rv, rv); + rv = WriteID(aIID); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); return serializable->Write(this); } @@ -278,21 +270,17 @@ NS_IMETHODIMP nsBinaryOutputStream::WriteID(const nsIID& aIID) { nsresult rv = Write32(aIID.m0); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = Write16(aIID.m1); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = Write16(aIID.m2); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); for (int i = 0; i < 8; ++i) { rv = Write8(aIID.m3[i]); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } return NS_OK; @@ -318,16 +306,14 @@ NS_IMPL_ISUPPORTS3(nsBinaryInputStream, nsIObjectInputStream, nsIBinaryInputStre NS_IMETHODIMP nsBinaryInputStream::Available(uint64_t* aResult) { - if (NS_WARN_IF(!mInputStream)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mInputStream); return mInputStream->Available(aResult); } NS_IMETHODIMP nsBinaryInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t *aNumRead) { - if (NS_WARN_IF(!mInputStream)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mInputStream); // mInputStream might give us short reads, so deal with that. uint32_t totalRead = 0; @@ -397,8 +383,7 @@ ReadSegmentForwardingThunk(nsIInputStream* aStream, NS_IMETHODIMP nsBinaryInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, uint32_t count, uint32_t *_retval) { - if (NS_WARN_IF(!mInputStream)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mInputStream); ReadSegmentsClosure thunkClosure = { this, closure, writer, NS_OK, 0 }; @@ -431,24 +416,21 @@ nsBinaryInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, uint NS_IMETHODIMP nsBinaryInputStream::IsNonBlocking(bool *aNonBlocking) { - if (NS_WARN_IF(!mInputStream)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mInputStream); return mInputStream->IsNonBlocking(aNonBlocking); } NS_IMETHODIMP -nsBinaryInputStream::Close() -{ - if (NS_WARN_IF(!mInputStream)) - return NS_ERROR_UNEXPECTED; - return mInputStream->Close(); +nsBinaryInputStream::Close() +{ + NS_ENSURE_STATE(mInputStream); + return mInputStream->Close(); } NS_IMETHODIMP nsBinaryInputStream::SetInputStream(nsIInputStream *aInputStream) { - if (NS_WARN_IF(!aInputStream)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aInputStream); mInputStream = aInputStream; mBufferAccess = do_QueryInterface(aInputStream); return NS_OK; @@ -747,8 +729,7 @@ nsBinaryInputStream::ReadArrayBuffer(uint32_t aLength, const JS::Value& aBuffer, uint32_t bytesRead; nsresult rv = Read(reinterpret_cast(data), aLength, &bytesRead); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); if (bytesRead != aLength) { return NS_ERROR_FAILURE; } @@ -761,12 +742,10 @@ nsBinaryInputStream::ReadObject(bool aIsStrongRef, nsISupports* *aObject) nsCID cid; nsIID iid; nsresult rv = ReadID(&cid); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = ReadID(&iid); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); // HACK: Intercept old (pre-gecko6) nsIURI IID, and replace with // the updated IID, so that we're QI'ing to an actual interface. @@ -795,16 +774,13 @@ nsBinaryInputStream::ReadObject(bool aIsStrongRef, nsISupports* *aObject) // END HACK nsCOMPtr object = do_CreateInstance(cid, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr serializable = do_QueryInterface(object); - if (NS_WARN_IF(!serializable)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_TRUE(serializable, NS_ERROR_UNEXPECTED); rv = serializable->Read(this); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); return object->QueryInterface(iid, reinterpret_cast(aObject)); } @@ -813,21 +789,17 @@ NS_IMETHODIMP nsBinaryInputStream::ReadID(nsID *aResult) { nsresult rv = Read32(&aResult->m0); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = Read16(&aResult->m1); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = Read16(&aResult->m2); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); for (int i = 0; i < 8; ++i) { rv = Read8(&aResult->m3[i]); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } return NS_OK; diff --git a/xpcom/io/nsDirectoryService.cpp b/xpcom/io/nsDirectoryService.cpp index a1078d4dfb9..603c9c015ca 100644 --- a/xpcom/io/nsDirectoryService.cpp +++ b/xpcom/io/nsDirectoryService.cpp @@ -59,8 +59,7 @@ nsresult nsDirectoryService::GetCurrentProcessDirectory(nsIFile** aFile) //---------------------------------------------------------------------------------------- { - if (NS_WARN_IF(!aFile)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aFile); *aFile = nullptr; // Set the component registry location: @@ -226,10 +225,8 @@ nsDirectoryService::nsDirectoryService() nsresult nsDirectoryService::Create(nsISupports *outer, REFNSIID aIID, void **aResult) { - if (NS_WARN_IF(!aResult)) - return NS_ERROR_INVALID_ARG; - if (NS_WARN_IF(outer)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_ARG_POINTER(aResult); + NS_ENSURE_NO_AGGREGATION(outer); if (!gService) { @@ -287,8 +284,7 @@ NS_IMPL_ISUPPORTS4(nsDirectoryService, nsIProperties, nsIDirectoryService, nsIDi NS_IMETHODIMP nsDirectoryService::Undefine(const char* prop) { - if (NS_WARN_IF(!prop)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(prop); nsDependentCString key(prop); if (!mHashtable.Get(key, nullptr)) @@ -364,8 +360,7 @@ static bool FindProviderFile(nsIDirectoryServiceProvider* aElement, NS_IMETHODIMP nsDirectoryService::Get(const char* prop, const nsIID & uuid, void* *result) { - if (NS_WARN_IF(!prop)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(prop); nsDependentCString key(prop); @@ -414,8 +409,7 @@ nsDirectoryService::Get(const char* prop, const nsIID & uuid, void* *result) NS_IMETHODIMP nsDirectoryService::Set(const char* prop, nsISupports* value) { - if (NS_WARN_IF(!prop)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(prop); nsDependentCString key(prop); if (mHashtable.Get(key, nullptr) || !value) { @@ -437,8 +431,7 @@ nsDirectoryService::Set(const char* prop, nsISupports* value) NS_IMETHODIMP nsDirectoryService::Has(const char *prop, bool *_retval) { - if (NS_WARN_IF(!prop)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(prop); *_retval = false; nsCOMPtr value; @@ -917,8 +910,7 @@ nsDirectoryService::GetFile(const char *prop, bool *persistent, nsIFile **_retva NS_IMETHODIMP nsDirectoryService::GetFiles(const char *prop, nsISimpleEnumerator **_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); *_retval = nullptr; return NS_ERROR_FAILURE; diff --git a/xpcom/io/nsIOUtil.cpp b/xpcom/io/nsIOUtil.cpp index e4062793b0b..4363355de1a 100644 --- a/xpcom/io/nsIOUtil.cpp +++ b/xpcom/io/nsIOUtil.cpp @@ -1,4 +1,4 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -13,8 +13,7 @@ NS_IMPL_ISUPPORTS1(nsIOUtil, nsIIOUtil) NS_IMETHODIMP nsIOUtil::InputStreamIsBuffered(nsIInputStream* aStream, bool* _retval) { - if (NS_WARN_IF(!aStream)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aStream); *_retval = NS_InputStreamIsBuffered(aStream); return NS_OK; } @@ -22,8 +21,7 @@ nsIOUtil::InputStreamIsBuffered(nsIInputStream* aStream, bool* _retval) NS_IMETHODIMP nsIOUtil::OutputStreamIsBuffered(nsIOutputStream* aStream, bool* _retval) { - if (NS_WARN_IF(!aStream)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aStream); *_retval = NS_OutputStreamIsBuffered(aStream); return NS_OK; } diff --git a/xpcom/io/nsInputStreamTee.cpp b/xpcom/io/nsInputStreamTee.cpp index cebf04d20c7..4ac153dfe21 100644 --- a/xpcom/io/nsInputStreamTee.cpp +++ b/xpcom/io/nsInputStreamTee.cpp @@ -164,6 +164,7 @@ nsInputStreamTee::TeeSegment(const char *buf, uint32_t count) } nsRefPtr event = new nsInputStreamTeeWriteEvent(buf, count, mSink, this); + NS_ENSURE_TRUE(event, NS_ERROR_OUT_OF_MEMORY); LOG(("nsInputStreamTee::TeeSegment [%p] dispatching write %u bytes\n", this, count)); return mEventTarget->Dispatch(event, NS_DISPATCH_NORMAL); @@ -213,8 +214,7 @@ NS_IMPL_ISUPPORTS2(nsInputStreamTee, NS_IMETHODIMP nsInputStreamTee::Close() { - if (NS_WARN_IF(!mSource)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED); nsresult rv = mSource->Close(); mSource = 0; mSink = 0; @@ -224,16 +224,14 @@ nsInputStreamTee::Close() NS_IMETHODIMP nsInputStreamTee::Available(uint64_t *avail) { - if (NS_WARN_IF(!mSource)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED); return mSource->Available(avail); } NS_IMETHODIMP nsInputStreamTee::Read(char *buf, uint32_t count, uint32_t *bytesRead) { - if (NS_WARN_IF(!mSource)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED); nsresult rv = mSource->Read(buf, count, bytesRead); if (NS_FAILED(rv) || (*bytesRead == 0)) @@ -248,8 +246,7 @@ nsInputStreamTee::ReadSegments(nsWriteSegmentFun writer, uint32_t count, uint32_t *bytesRead) { - if (NS_WARN_IF(!mSource)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED); mWriter = writer; mClosure = closure; @@ -260,8 +257,7 @@ nsInputStreamTee::ReadSegments(nsWriteSegmentFun writer, NS_IMETHODIMP nsInputStreamTee::IsNonBlocking(bool *result) { - if (NS_WARN_IF(!mSource)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED); return mSource->IsNonBlocking(result); } diff --git a/xpcom/io/nsLocalFileCommon.cpp b/xpcom/io/nsLocalFileCommon.cpp index 75c53483877..5ec4bae22b3 100644 --- a/xpcom/io/nsLocalFileCommon.cpp +++ b/xpcom/io/nsLocalFileCommon.cpp @@ -33,8 +33,7 @@ void NS_ShutdownLocalFile() NS_IMETHODIMP nsLocalFile::InitWithFile(nsIFile *aFile) { - if (NS_WARN_IF(!aFile)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aFile); nsAutoCString path; aFile->GetNativePath(path); @@ -190,8 +189,7 @@ static int32_t SplitPath(PRUnichar *path, PRUnichar **nodeArray, int32_t arrayLe NS_IMETHODIMP nsLocalFile::GetRelativeDescriptor(nsIFile *fromFile, nsACString& _retval) { - if (NS_WARN_IF(!fromFile)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(fromFile); const int32_t kMaxNodesInPath = 32; // diff --git a/xpcom/io/nsLocalFileUnix.cpp b/xpcom/io/nsLocalFileUnix.cpp index c0127e80949..099bcea9028 100644 --- a/xpcom/io/nsLocalFileUnix.cpp +++ b/xpcom/io/nsLocalFileUnix.cpp @@ -243,10 +243,8 @@ nsLocalFile::nsLocalFileConstructor(nsISupports *outer, const nsIID &aIID, void **aInstancePtr) { - if (NS_WARN_IF(!aInstancePtr)) - return NS_ERROR_INVALID_ARG; - if (NS_WARN_IF(outer)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_ARG_POINTER(aInstancePtr); + NS_ENSURE_NO_AGGREGATION(outer); *aInstancePtr = nullptr; @@ -1007,8 +1005,7 @@ NS_IMETHODIMP nsLocalFile::GetLastModifiedTime(PRTime *aLastModTime) { CHECK_mPath(); - if (NS_WARN_IF(!aLastModTime)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aLastModTime); PRFileInfo64 info; if (PR_GetFileInfo64(mPath.get(), &info) != PR_SUCCESS) @@ -1046,8 +1043,7 @@ NS_IMETHODIMP nsLocalFile::GetLastModifiedTimeOfLink(PRTime *aLastModTimeOfLink) { CHECK_mPath(); - if (NS_WARN_IF(!aLastModTimeOfLink)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aLastModTimeOfLink); struct STAT sbuf; if (LSTAT(mPath.get(), &sbuf) == -1) @@ -1076,8 +1072,7 @@ nsLocalFile::SetLastModifiedTimeOfLink(PRTime aLastModTimeOfLink) NS_IMETHODIMP nsLocalFile::GetPermissions(uint32_t *aPermissions) { - if (NS_WARN_IF(!aPermissions)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aPermissions); ENSURE_STAT_CACHE(); *aPermissions = NORMALIZE_PERMS(mCachedStat.st_mode); return NS_OK; @@ -1087,8 +1082,7 @@ NS_IMETHODIMP nsLocalFile::GetPermissionsOfLink(uint32_t *aPermissionsOfLink) { CHECK_mPath(); - if (NS_WARN_IF(!aPermissionsOfLink)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aPermissionsOfLink); struct STAT sbuf; if (LSTAT(mPath.get(), &sbuf) == -1) @@ -1133,8 +1127,7 @@ nsLocalFile::SetPermissionsOfLink(uint32_t aPermissions) NS_IMETHODIMP nsLocalFile::GetFileSize(int64_t *aFileSize) { - if (NS_WARN_IF(!aFileSize)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aFileSize); *aFileSize = 0; ENSURE_STAT_CACHE(); @@ -1183,8 +1176,7 @@ NS_IMETHODIMP nsLocalFile::GetFileSizeOfLink(int64_t *aFileSize) { CHECK_mPath(); - if (NS_WARN_IF(!aFileSize)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aFileSize); struct STAT sbuf; if (LSTAT(mPath.get(), &sbuf) == -1) @@ -1249,8 +1241,7 @@ GetDeviceName(int deviceMajor, int deviceMinor, nsACString &deviceName) NS_IMETHODIMP nsLocalFile::GetDiskSpaceAvailable(int64_t *aDiskSpaceAvailable) { - if (NS_WARN_IF(!aDiskSpaceAvailable)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aDiskSpaceAvailable); // These systems have the operations necessary to check disk space. @@ -1335,8 +1326,7 @@ NS_IMETHODIMP nsLocalFile::GetParent(nsIFile **aParent) { CHECK_mPath(); - if (NS_WARN_IF(!aParent)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aParent); *aParent = nullptr; // if '/' we are at the top of the volume, return null @@ -1382,8 +1372,7 @@ NS_IMETHODIMP nsLocalFile::Exists(bool *_retval) { CHECK_mPath(); - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); *_retval = (access(mPath.get(), F_OK) == 0); return NS_OK; @@ -1394,8 +1383,7 @@ NS_IMETHODIMP nsLocalFile::IsWritable(bool *_retval) { CHECK_mPath(); - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); *_retval = (access(mPath.get(), W_OK) == 0); if (*_retval || errno == EACCES) @@ -1407,8 +1395,7 @@ NS_IMETHODIMP nsLocalFile::IsReadable(bool *_retval) { CHECK_mPath(); - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); *_retval = (access(mPath.get(), R_OK) == 0); if (*_retval || errno == EACCES) @@ -1420,8 +1407,7 @@ NS_IMETHODIMP nsLocalFile::IsExecutable(bool *_retval) { CHECK_mPath(); - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); // Check extension (bug 663899). On certain platforms, the file // extension may cause the OS to treat it as executable regardless of @@ -1510,8 +1496,7 @@ nsLocalFile::IsExecutable(bool *_retval) NS_IMETHODIMP nsLocalFile::IsDirectory(bool *_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); *_retval = false; ENSURE_STAT_CACHE(); *_retval = S_ISDIR(mCachedStat.st_mode); @@ -1521,8 +1506,7 @@ nsLocalFile::IsDirectory(bool *_retval) NS_IMETHODIMP nsLocalFile::IsFile(bool *_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); *_retval = false; ENSURE_STAT_CACHE(); *_retval = S_ISREG(mCachedStat.st_mode); @@ -1532,8 +1516,7 @@ nsLocalFile::IsFile(bool *_retval) NS_IMETHODIMP nsLocalFile::IsHidden(bool *_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); nsACString::const_iterator begin, end; LocateNativeLeafName(begin, end); *_retval = (*begin == '.'); @@ -1543,8 +1526,7 @@ nsLocalFile::IsHidden(bool *_retval) NS_IMETHODIMP nsLocalFile::IsSymlink(bool *_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); CHECK_mPath(); struct STAT symStat; @@ -1557,8 +1539,7 @@ nsLocalFile::IsSymlink(bool *_retval) NS_IMETHODIMP nsLocalFile::IsSpecial(bool *_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); ENSURE_STAT_CACHE(); *_retval = S_ISCHR(mCachedStat.st_mode) || S_ISBLK(mCachedStat.st_mode) || @@ -1573,10 +1554,8 @@ nsLocalFile::IsSpecial(bool *_retval) NS_IMETHODIMP nsLocalFile::Equals(nsIFile *inFile, bool *_retval) { - if (NS_WARN_IF(!inFile)) - return NS_ERROR_INVALID_ARG; - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(inFile); + NS_ENSURE_ARG_POINTER(_retval); *_retval = false; nsAutoCString inPath; @@ -1594,10 +1573,8 @@ NS_IMETHODIMP nsLocalFile::Contains(nsIFile *inFile, bool recur, bool *_retval) { CHECK_mPath(); - if (NS_WARN_IF(!inFile)) - return NS_ERROR_INVALID_ARG; - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(inFile); + NS_ENSURE_ARG_POINTER(_retval); nsAutoCString inPath; nsresult rv; @@ -1733,8 +1710,7 @@ NS_IMETHODIMP nsLocalFile::Load(PRLibrary **_retval) { CHECK_mPath(); - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); #ifdef NS_BUILD_REFCNT_LOGGING nsTraceRefcntImpl::SetActivityIsLegal(false); @@ -2145,8 +2121,7 @@ nsLocalFile::InitWithCFURL(CFURLRef aCFURL) NS_IMETHODIMP nsLocalFile::InitWithFSRef(const FSRef *aFSRef) { - if (NS_WARN_IF(!aFSRef)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aFSRef); CFURLRef newURLRef = ::CFURLCreateFromFSRef(kCFAllocatorDefault, aFSRef); if (newURLRef) { @@ -2176,8 +2151,7 @@ nsLocalFile::GetCFURL(CFURLRef *_retval) NS_IMETHODIMP nsLocalFile::GetFSRef(FSRef *_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); nsresult rv = NS_ERROR_FAILURE; @@ -2195,8 +2169,7 @@ nsLocalFile::GetFSRef(FSRef *_retval) NS_IMETHODIMP nsLocalFile::GetFSSpec(FSSpec *_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(_retval); FSRef fsRef; nsresult rv = GetFSRef(&fsRef); @@ -2211,8 +2184,7 @@ nsLocalFile::GetFSSpec(FSSpec *_retval) NS_IMETHODIMP nsLocalFile::GetFileSizeWithResFork(int64_t *aFileSizeWithResFork) { - if (NS_WARN_IF(!aFileSizeWithResFork)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aFileSizeWithResFork); FSRef fsRef; nsresult rv = GetFSRef(&fsRef); @@ -2371,8 +2343,7 @@ nsLocalFile::OpenDocWithApp(nsIFile *aAppToOpenWith, bool aLaunchInBackground) NS_IMETHODIMP nsLocalFile::IsPackage(bool *_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(_retval); *_retval = false; CFURLRef url; @@ -2443,8 +2414,7 @@ NS_IMETHODIMP nsLocalFile::GetBundleContentsLastModifiedTime(int64_t *aLastModTime) { CHECK_mPath(); - if (NS_WARN_IF(!aLastModTime)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aLastModTime); bool isPackage = false; nsresult rv = IsPackage(&isPackage); @@ -2470,8 +2440,7 @@ nsLocalFile::GetBundleContentsLastModifiedTime(int64_t *aLastModTime) NS_IMETHODIMP nsLocalFile::InitWithFile(nsIFile *aFile) { - if (NS_WARN_IF(!aFile)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aFile); nsAutoCString nativePath; nsresult rv = aFile->GetNativePath(nativePath); diff --git a/xpcom/io/nsLocalFileWin.cpp b/xpcom/io/nsLocalFileWin.cpp index 3d1e3e01ab0..c8cd7911f15 100644 --- a/xpcom/io/nsLocalFileWin.cpp +++ b/xpcom/io/nsLocalFileWin.cpp @@ -725,8 +725,7 @@ struct nsDir static nsresult OpenDir(const nsAFlatString &name, nsDir * *dir) { - if (NS_WARN_IF(!dir)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(dir); *dir = nullptr; if (name.Length() + 3 >= MAX_PATH) @@ -766,8 +765,7 @@ static nsresult ReadDir(nsDir *dir, PRDirFlags flags, nsString& name) { name.Truncate(); - if (NS_WARN_IF(!dir)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(dir); while (1) { BOOL rv; @@ -811,8 +809,7 @@ ReadDir(nsDir *dir, PRDirFlags flags, nsString& name) static nsresult CloseDir(nsDir *&d) { - if (NS_WARN_IF(!d)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(d); BOOL isOk = FindClose(d->handle); // PR_DELETE also nulls out the passed in pointer. @@ -965,10 +962,8 @@ nsLocalFile::nsLocalFile() nsresult nsLocalFile::nsLocalFileConstructor(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr) { - if (NS_WARN_IF(!aInstancePtr)) - return NS_ERROR_INVALID_ARG; - if (NS_WARN_IF(outer)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_ARG_POINTER(aInstancePtr); + NS_ENSURE_NO_AGGREGATION(outer); nsLocalFile* inst = new nsLocalFile(); if (inst == nullptr) @@ -1154,8 +1149,7 @@ nsLocalFile::Clone(nsIFile **file) NS_IMETHODIMP nsLocalFile::InitWithFile(nsIFile *aFile) { - if (NS_WARN_IF(!aFile)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aFile); nsAutoString path; aFile->GetPath(path); @@ -2078,8 +2072,7 @@ nsLocalFile::CopyMove(nsIFile *aParentDir, const nsAString &newName, bool follow return NS_ERROR_FAILURE; rv = file->MoveTo(target, EmptyString()); - if (NS_FAILED(rv)) - return rv; + NS_ENSURE_SUCCESS(rv,rv); } else { @@ -2087,8 +2080,7 @@ nsLocalFile::CopyMove(nsIFile *aParentDir, const nsAString &newName, bool follow rv = file->CopyToFollowingLinks(target, EmptyString()); else rv = file->CopyTo(target, EmptyString()); - if (NS_FAILED(rv)) - return rv; + NS_ENSURE_SUCCESS(rv,rv); } } } @@ -2101,8 +2093,7 @@ nsLocalFile::CopyMove(nsIFile *aParentDir, const nsAString &newName, bool follow if (move) { rv = Remove(false /* recursive */); - if (NS_FAILED(rv)) - return rv; + NS_ENSURE_SUCCESS(rv,rv); } } @@ -2267,8 +2258,7 @@ nsLocalFile::GetLastModifiedTime(PRTime *aLastModifiedTime) // Check we are correctly initialized. CHECK_mWorkingPath(); - if (NS_WARN_IF(!aLastModifiedTime)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aLastModifiedTime); // get the modified time of the target as determined by mFollowSymlinks // If true, then this will be for the target of the shortcut file, @@ -2291,8 +2281,7 @@ nsLocalFile::GetLastModifiedTimeOfLink(PRTime *aLastModifiedTime) // Check we are correctly initialized. CHECK_mWorkingPath(); - if (NS_WARN_IF(!aLastModifiedTime)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aLastModifiedTime); // The caller is assumed to have already called IsSymlink // and to have found that this file is a link. @@ -2392,8 +2381,7 @@ nsLocalFile::SetModDate(PRTime aLastModifiedTime, const PRUnichar *filePath) NS_IMETHODIMP nsLocalFile::GetPermissions(uint32_t *aPermissions) { - if (NS_WARN_IF(!aPermissions)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aPermissions); // get the permissions of the target as determined by mFollowSymlinks // If true, then this will be for the target of the shortcut file, @@ -2422,8 +2410,7 @@ nsLocalFile::GetPermissionsOfLink(uint32_t *aPermissions) // Check we are correctly initialized. CHECK_mWorkingPath(); - if (NS_WARN_IF(!aPermissions)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aPermissions); // The caller is assumed to have already called IsSymlink // and to have found that this file is a link. It is not @@ -2492,8 +2479,7 @@ nsLocalFile::SetPermissionsOfLink(uint32_t aPermissions) NS_IMETHODIMP nsLocalFile::GetFileSize(int64_t *aFileSize) { - if (NS_WARN_IF(!aFileSize)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aFileSize); nsresult rv = ResolveAndStat(); if (NS_FAILED(rv)) @@ -2510,8 +2496,7 @@ nsLocalFile::GetFileSizeOfLink(int64_t *aFileSize) // Check we are correctly initialized. CHECK_mWorkingPath(); - if (NS_WARN_IF(!aFileSize)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aFileSize); // The caller is assumed to have already called IsSymlink // and to have found that this file is a link. @@ -2566,8 +2551,7 @@ nsLocalFile::GetDiskSpaceAvailable(int64_t *aDiskSpaceAvailable) // Check we are correctly initialized. CHECK_mWorkingPath(); - if (NS_WARN_IF(!aDiskSpaceAvailable)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aDiskSpaceAvailable); ResolveAndStat(); @@ -2596,8 +2580,7 @@ nsLocalFile::GetParent(nsIFile * *aParent) // Check we are correctly initialized. CHECK_mWorkingPath(); - if (NS_WARN_IF(!aParent)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aParent); // A two-character path must be a drive such as C:, so it has no parent if (mWorkingPath.Length() == 2) { @@ -2641,8 +2624,7 @@ nsLocalFile::Exists(bool *_retval) // Check we are correctly initialized. CHECK_mWorkingPath(); - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(_retval); *_retval = false; MakeDirty(); @@ -2712,8 +2694,7 @@ nsLocalFile::IsReadable(bool *_retval) // Check we are correctly initialized. CHECK_mWorkingPath(); - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(_retval); *_retval = false; nsresult rv = ResolveAndStat(); @@ -2731,8 +2712,7 @@ nsLocalFile::IsExecutable(bool *_retval) // Check we are correctly initialized. CHECK_mWorkingPath(); - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(_retval); *_retval = false; nsresult rv; @@ -2887,8 +2867,7 @@ nsLocalFile::IsHidden(bool *_retval) nsresult nsLocalFile::HasFileAttribute(DWORD fileAttrib, bool *_retval) { - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(_retval); nsresult rv = Resolve(); if (NS_FAILED(rv)) { @@ -2910,8 +2889,7 @@ nsLocalFile::IsSymlink(bool *_retval) // Check we are correctly initialized. CHECK_mWorkingPath(); - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(_retval); // unless it is a valid shortcut path it's not a symlink if (!IsShortcutPath(mWorkingPath)) { @@ -2942,10 +2920,8 @@ nsLocalFile::IsSpecial(bool *_retval) NS_IMETHODIMP nsLocalFile::Equals(nsIFile *inFile, bool *_retval) { - if (NS_WARN_IF(!inFile)) - return NS_ERROR_INVALID_ARG; - if (NS_WARN_IF(!_retval)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(inFile); + NS_ENSURE_ARG(_retval); EnsureShortPath(); diff --git a/xpcom/io/nsMultiplexInputStream.cpp b/xpcom/io/nsMultiplexInputStream.cpp index 4d36fb90721..2373cfaa737 100644 --- a/xpcom/io/nsMultiplexInputStream.cpp +++ b/xpcom/io/nsMultiplexInputStream.cpp @@ -118,6 +118,7 @@ nsMultiplexInputStream::InsertStream(nsIInputStream *aStream, uint32_t aIndex) { NS_ASSERTION(SeekableStreamAtBeginning(aStream), "Inserted stream not at beginning."); bool result = mStreams.InsertElementAt(aIndex, aStream); + NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY); if (mCurrentStream > aIndex || (mCurrentStream == aIndex && mStartedReadingCurrent)) ++mCurrentStream; @@ -142,8 +143,7 @@ NS_IMETHODIMP nsMultiplexInputStream::GetStream(uint32_t aIndex, nsIInputStream **_retval) { *_retval = mStreams.SafeElementAt(aIndex, nullptr); - if (NS_WARN_IF(!*_retval)) - return NS_ERROR_NOT_AVAILABLE; + NS_ENSURE_TRUE(*_retval, NS_ERROR_NOT_AVAILABLE); NS_ADDREF(*_retval); return NS_OK; @@ -181,8 +181,7 @@ nsMultiplexInputStream::Available(uint64_t *_retval) for (uint32_t i = mCurrentStream; i < len; i++) { uint64_t streamAvail; rv = mStreams[i]->Available(&streamAvail); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); avail += streamAvail; } *_retval = avail; @@ -329,8 +328,7 @@ nsMultiplexInputStream::IsNonBlocking(bool *aNonBlocking) } for (uint32_t i = 0; i < len; ++i) { nsresult rv = mStreams[i]->IsNonBlocking(aNonBlocking); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); // If one is non-blocking the entire stream becomes non-blocking // (except that we don't implement nsIAsyncInputStream, so there's // not much for the caller to do if Read returns "would block") @@ -369,8 +367,7 @@ nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset) if (i < oldCurrentStream || (i == oldCurrentStream && oldStartedReadingCurrent)) { rv = stream->Seek(NS_SEEK_SET, 0); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); continue; } else { @@ -386,15 +383,13 @@ nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset) } else { rv = stream->Tell(&streamPos); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } // See if we need to seek current stream forward or backward if (remaining < streamPos) { rv = stream->Seek(NS_SEEK_SET, remaining); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); mCurrentStream = i; mStartedReadingCurrent = remaining != 0; @@ -410,14 +405,12 @@ nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset) else { uint64_t avail; rv = mStreams[i]->Available(&avail); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); int64_t newPos = XPCOM_MIN(remaining, streamPos + (int64_t)avail); rv = stream->Seek(NS_SEEK_SET, newPos); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); mCurrentStream = i; mStartedReadingCurrent = true; @@ -443,14 +436,12 @@ nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset) uint64_t avail; rv = mStreams[i]->Available(&avail); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); int64_t seek = XPCOM_MIN((int64_t)avail, remaining); rv = stream->Seek(NS_SEEK_CUR, seek); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); mCurrentStream = i; mStartedReadingCurrent = true; @@ -469,14 +460,12 @@ nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset) int64_t pos; rv = stream->Tell(&pos); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); int64_t seek = XPCOM_MIN(pos, remaining); rv = stream->Seek(NS_SEEK_CUR, -seek); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); mCurrentStream = i; mStartedReadingCurrent = seek != -pos; @@ -506,8 +495,7 @@ nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset) if (remaining == 0) { if (i >= oldCurrentStream) { rv = stream->Seek(NS_SEEK_END, 0); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } else { break; @@ -521,8 +509,7 @@ nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset) } else { uint64_t avail; rv = mStreams[i]->Available(&avail); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); streamPos = avail; } @@ -530,8 +517,7 @@ nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset) // See if we have enough data in the current stream. if (DeprecatedAbs(remaining) < streamPos) { rv = stream->Seek(NS_SEEK_END, remaining); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); mCurrentStream = i; mStartedReadingCurrent = true; @@ -545,14 +531,12 @@ nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset) } else { int64_t avail; rv = stream->Tell(&avail); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); int64_t newPos = streamPos + XPCOM_MIN(avail, DeprecatedAbs(remaining)); rv = stream->Seek(NS_SEEK_END, -newPos); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); mCurrentStream = i; mStartedReadingCurrent = true; @@ -586,13 +570,11 @@ nsMultiplexInputStream::Tell(int64_t *_retval) last = mStartedReadingCurrent ? mCurrentStream+1 : mCurrentStream; for (i = 0; i < last; ++i) { nsCOMPtr stream = do_QueryInterface(mStreams[i]); - if (NS_WARN_IF(!stream)) - return NS_ERROR_NO_INTERFACE; + NS_ENSURE_TRUE(stream, NS_ERROR_NO_INTERFACE); int64_t pos; rv = stream->Tell(&pos); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); ret64 += pos; } *_retval = ret64; diff --git a/xpcom/io/nsPipe3.cpp b/xpcom/io/nsPipe3.cpp index 4c988cf083b..eb63ef92432 100644 --- a/xpcom/io/nsPipe3.cpp +++ b/xpcom/io/nsPipe3.cpp @@ -356,8 +356,7 @@ nsPipe::GetInputStream(nsIAsyncInputStream **aInputStream) NS_IMETHODIMP nsPipe::GetOutputStream(nsIAsyncOutputStream **aOutputStream) { - if (NS_WARN_IF(!mInited)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mInited, NS_ERROR_NOT_INITIALIZED); NS_ADDREF(*aOutputStream = &mOutput); return NS_OK; } diff --git a/xpcom/io/nsStorageStream.cpp b/xpcom/io/nsStorageStream.cpp index 5c745819b5b..1bf82156757 100644 --- a/xpcom/io/nsStorageStream.cpp +++ b/xpcom/io/nsStorageStream.cpp @@ -88,10 +88,8 @@ NS_IMETHODIMP nsStorageStream::GetOutputStream(int32_t aStartingOffset, nsIOutputStream * *aOutputStream) { - if (NS_WARN_IF(!aOutputStream)) - return NS_ERROR_INVALID_ARG; - if (NS_WARN_IF(!mSegmentedBuffer)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_ARG(aOutputStream); + NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED); if (mWriteInProgress) return NS_ERROR_NOT_AVAILABLE; @@ -118,8 +116,7 @@ nsStorageStream::GetOutputStream(int32_t aStartingOffset, NS_IMETHODIMP nsStorageStream::Close() { - if (NS_WARN_IF(!mSegmentedBuffer)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED); mWriteInProgress = false; @@ -148,15 +145,15 @@ nsStorageStream::Flush() NS_IMETHODIMP nsStorageStream::Write(const char *aBuffer, uint32_t aCount, uint32_t *aNumWritten) { - if (NS_WARN_IF(!aNumWritten) || NS_WARN_IF(!aBuffer)) - return NS_ERROR_INVALID_ARG; - if (NS_WARN_IF(!mSegmentedBuffer)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED); const char* readCursor; uint32_t count, availableInSegment, remaining; nsresult rv = NS_OK; + NS_ENSURE_ARG_POINTER(aNumWritten); + NS_ENSURE_ARG(aBuffer); + LOG(("nsStorageStream [%p] Write mWriteCursor=%x mSegmentEnd=%x aCount=%d\n", this, mWriteCursor, mSegmentEnd, aCount)); @@ -226,6 +223,7 @@ nsStorageStream::IsNonBlocking(bool *aNonBlocking) NS_IMETHODIMP nsStorageStream::GetLength(uint32_t *aLength) { + NS_ENSURE_ARG(aLength); *aLength = mLogicalLength; return NS_OK; } @@ -234,8 +232,7 @@ nsStorageStream::GetLength(uint32_t *aLength) NS_IMETHODIMP nsStorageStream::SetLength(uint32_t aLength) { - if (NS_WARN_IF(!mSegmentedBuffer)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED); if (mWriteInProgress) return NS_ERROR_NOT_AVAILABLE; @@ -260,6 +257,8 @@ nsStorageStream::SetLength(uint32_t aLength) NS_IMETHODIMP nsStorageStream::GetWriteInProgress(bool *aWriteInProgress) { + NS_ENSURE_ARG(aWriteInProgress); + *aWriteInProgress = mWriteInProgress; return NS_OK; } @@ -267,8 +266,7 @@ nsStorageStream::GetWriteInProgress(bool *aWriteInProgress) NS_METHOD nsStorageStream::Seek(int32_t aPosition) { - if (NS_WARN_IF(!mSegmentedBuffer)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED); // An argument of -1 means "seek to end of stream" if (aPosition == -1) @@ -360,8 +358,7 @@ NS_IMPL_ISUPPORTS2(nsStorageInputStream, NS_IMETHODIMP nsStorageStream::NewInputStream(int32_t aStartingOffset, nsIInputStream* *aInputStream) { - if (NS_WARN_IF(!mSegmentedBuffer)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED); nsStorageInputStream *inputStream = new nsStorageInputStream(this, mSegmentSize); if (!inputStream) @@ -527,6 +524,8 @@ nsStorageInputStream::Seek(uint32_t aPosition) nsresult NS_NewStorageStream(uint32_t segmentSize, uint32_t maxSize, nsIStorageStream **result) { + NS_ENSURE_ARG(result); + nsStorageStream* storageStream = new nsStorageStream(); if (!storageStream) return NS_ERROR_OUT_OF_MEMORY; diff --git a/xpcom/io/nsStringStream.cpp b/xpcom/io/nsStringStream.cpp index f6bfd18d9f7..f3281781192 100644 --- a/xpcom/io/nsStringStream.cpp +++ b/xpcom/io/nsStringStream.cpp @@ -112,8 +112,7 @@ nsStringInputStream::GetData(nsACString &data) // The stream doesn't have any data when it is closed. We could fake it // and return an empty string here, but it seems better to keep this return // value consistent with the behavior of the other 'getter' methods. - if (NS_WARN_IF(Closed())) - return NS_BASE_STREAM_CLOSED; + NS_ENSURE_TRUE(!Closed(), NS_BASE_STREAM_CLOSED); data.Assign(mData); return NS_OK; @@ -141,8 +140,7 @@ nsStringInputStream::ToString(char **result) NS_IMETHODIMP nsStringInputStream::SetData(const char *data, int32_t dataLen) { - if (NS_WARN_IF(!data)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(data); mData.Assign(data, dataLen); mOffset = 0; return NS_OK; @@ -151,8 +149,7 @@ nsStringInputStream::SetData(const char *data, int32_t dataLen) NS_IMETHODIMP nsStringInputStream::AdoptData(char *data, int32_t dataLen) { - if (NS_WARN_IF(!data)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(data); mData.Adopt(data, dataLen); mOffset = 0; return NS_OK; @@ -161,8 +158,7 @@ nsStringInputStream::AdoptData(char *data, int32_t dataLen) NS_IMETHODIMP nsStringInputStream::ShareData(const char *data, int32_t dataLen) { - if (NS_WARN_IF(!data)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(data); if (dataLen < 0) dataLen = strlen(data); @@ -266,8 +262,8 @@ nsStringInputStream::Seek(int32_t whence, int64_t offset) return NS_ERROR_INVALID_ARG; } - if (NS_WARN_IF(newPos < 0) || NS_WARN_IF(newPos > Length())) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(newPos >= 0); + NS_ENSURE_ARG(newPos <= Length()); mOffset = (uint32_t)newPos; return NS_OK; @@ -390,8 +386,7 @@ nsStringInputStreamConstructor(nsISupports *outer, REFNSIID iid, void **result) { *result = nullptr; - if (NS_WARN_IF(outer)) - return NS_ERROR_NO_AGGREGATION; + NS_ENSURE_TRUE(!outer, NS_ERROR_NO_AGGREGATION); nsStringInputStream *inst = new nsStringInputStream(); if (!inst) diff --git a/xpcom/reflect/xptcall/src/xptcall.cpp b/xpcom/reflect/xptcall/src/xptcall.cpp index a33b1e70051..e46ddd559d3 100644 --- a/xpcom/reflect/xptcall/src/xptcall.cpp +++ b/xpcom/reflect/xptcall/src/xptcall.cpp @@ -40,13 +40,11 @@ EXPORT_XPCOM_API(nsresult) NS_GetXPTCallStub(REFNSIID aIID, nsIXPTCProxy* aOuter, nsISomeInterface* *aResult) { - if (NS_WARN_IF(!aOuter) || NS_WARN_IF(!aResult)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG(aOuter && aResult); XPTInterfaceInfoManager *iim = XPTInterfaceInfoManager::GetSingleton(); - if (NS_WARN_IF(!iim)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(iim, NS_ERROR_NOT_INITIALIZED); xptiInterfaceEntry *iie = iim->GetInterfaceEntryForIID(&aIID); if (!iie || !iie->EnsureResolved() || iie->GetBuiltinClassFlag()) diff --git a/xpcom/threads/LazyIdleThread.cpp b/xpcom/threads/LazyIdleThread.cpp index 71bf2ea5371..0cf14ad0acd 100644 --- a/xpcom/threads/LazyIdleThread.cpp +++ b/xpcom/threads/LazyIdleThread.cpp @@ -147,26 +147,21 @@ LazyIdleThread::EnsureThread() if (mShutdownMethod == AutomaticShutdown && NS_IsMainThread()) { nsCOMPtr obs = do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = obs->AddObserver(this, "xpcom-shutdown-threads", false); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } mIdleTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv); - if (NS_WARN_IF(!mIdleTimer)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_TRUE(mIdleTimer, NS_ERROR_FAILURE); nsCOMPtr runnable = NS_NewRunnableMethod(this, &LazyIdleThread::InitThread); - if (NS_WARN_IF(!runnable)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_TRUE(runnable, NS_ERROR_FAILURE); rv = NS_NewThread(getter_AddRefs(mThread), runnable); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); return NS_OK; } @@ -273,14 +268,12 @@ LazyIdleThread::ShutdownThread() nsCOMPtr runnable = NS_NewRunnableMethod(this, &LazyIdleThread::CleanupThread); - if (NS_WARN_IF(!runnable)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_TRUE(runnable, NS_ERROR_FAILURE); PreDispatch(); rv = mThread->Dispatch(runnable, NS_DISPATCH_NORMAL); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); // Put the temporary queue in place before calling Shutdown(). mQueuedRunnables = &queuedRunnables; @@ -306,8 +299,7 @@ LazyIdleThread::ShutdownThread() if (mIdleTimer) { rv = mIdleTimer->Cancel(); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); mIdleTimer = nullptr; } @@ -384,8 +376,7 @@ LazyIdleThread::Dispatch(nsIRunnable* aEvent, ASSERT_OWNING_THREAD(); // LazyIdleThread can't always support synchronous dispatch currently. - if (NS_WARN_IF(aFlags != NS_DISPATCH_NORMAL)) - return NS_ERROR_NOT_IMPLEMENTED; + NS_ENSURE_TRUE(aFlags == NS_DISPATCH_NORMAL, NS_ERROR_NOT_IMPLEMENTED); // If our thread is shutting down then we can't actually dispatch right now. // Queue this runnable for later. @@ -395,8 +386,7 @@ LazyIdleThread::Dispatch(nsIRunnable* aEvent, } nsresult rv = EnsureThread(); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); PreDispatch(); @@ -437,8 +427,7 @@ LazyIdleThread::Shutdown() mIdleObserver = nullptr; - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); return NS_OK; } @@ -478,8 +467,7 @@ LazyIdleThread::Notify(nsITimer* aTimer) } nsresult rv = ShutdownThread(); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); return NS_OK; } @@ -525,12 +513,10 @@ LazyIdleThread::AfterProcessNextEvent(nsIThreadInternal* /* aThread */, if (shouldNotifyIdle) { nsCOMPtr runnable = NS_NewRunnableMethod(this, &LazyIdleThread::ScheduleTimer); - if (NS_WARN_IF(!runnable)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_TRUE(runnable, NS_ERROR_FAILURE); nsresult rv = mOwningThread->Dispatch(runnable, NS_DISPATCH_NORMAL); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); } return NS_OK; diff --git a/xpcom/threads/nsEnvironment.cpp b/xpcom/threads/nsEnvironment.cpp index 53b20755673..f5c0656b4ed 100644 --- a/xpcom/threads/nsEnvironment.cpp +++ b/xpcom/threads/nsEnvironment.cpp @@ -48,8 +48,7 @@ nsEnvironment::Exists(const nsAString& aName, bool *aOutValue) { nsAutoCString nativeName; nsresult rv = NS_CopyUnicodeToNative(aName, nativeName); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); nsAutoCString nativeVal; #if defined(XP_UNIX) @@ -79,8 +78,7 @@ nsEnvironment::Get(const nsAString& aName, nsAString& aOutValue) { nsAutoCString nativeName; nsresult rv = NS_CopyUnicodeToNative(aName, nativeName); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); nsAutoCString nativeVal; const char *value = PR_GetEnv(nativeName.get()); @@ -124,12 +122,10 @@ nsEnvironment::Set(const nsAString& aName, const nsAString& aValue) nsAutoCString nativeVal; nsresult rv = NS_CopyUnicodeToNative(aName, nativeName); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); rv = NS_CopyUnicodeToNative(aValue, nativeVal); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); MutexAutoLock lock(mLock); diff --git a/xpcom/threads/nsMemoryPressure.cpp b/xpcom/threads/nsMemoryPressure.cpp index 094d4c06ab0..0e1b556054e 100644 --- a/xpcom/threads/nsMemoryPressure.cpp +++ b/xpcom/threads/nsMemoryPressure.cpp @@ -49,5 +49,6 @@ NS_DispatchMemoryPressure(MemoryPressureState state) { NS_DispatchEventualMemoryPressure(state); nsCOMPtr event = new nsRunnable; + NS_ENSURE_TRUE(event, NS_ERROR_OUT_OF_MEMORY); return NS_DispatchToMainThread(event); } diff --git a/xpcom/threads/nsProcessCommon.cpp b/xpcom/threads/nsProcessCommon.cpp index d2d660bafb6..8a6345d11d3 100644 --- a/xpcom/threads/nsProcessCommon.cpp +++ b/xpcom/threads/nsProcessCommon.cpp @@ -88,8 +88,7 @@ nsProcess::Init(nsIFile* executable) if (mExecutable) return NS_ERROR_ALREADY_INITIALIZED; - if (NS_WARN_IF(!executable)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(executable); bool isFile; //First make sure the file exists @@ -409,10 +408,8 @@ nsresult nsProcess::RunProcess(bool blocking, char **my_argv, nsIObserver* observer, bool holdWeak, bool argsUTF8) { - if (NS_WARN_IF(!mExecutable)) - return NS_ERROR_NOT_INITIALIZED; - if (NS_WARN_IF(mThread)) - return NS_ERROR_ALREADY_INITIALIZED; + NS_ENSURE_TRUE(mExecutable, NS_ERROR_NOT_INITIALIZED); + NS_ENSURE_FALSE(mThread, NS_ERROR_ALREADY_INITIALIZED); if (observer) { if (holdWeak) { diff --git a/xpcom/threads/nsThread.cpp b/xpcom/threads/nsThread.cpp index 9cc4e2528e8..94e8a04da13 100644 --- a/xpcom/threads/nsThread.cpp +++ b/xpcom/threads/nsThread.cpp @@ -152,9 +152,9 @@ NS_IMPL_CI_INTERFACE_GETTER4(nsThread, nsIThread, nsIThreadInternal, class nsThreadStartupEvent : public nsRunnable { public: - nsThreadStartupEvent() - : mMon("nsThreadStartupEvent.mMon") - , mInitialized(false) { + // Create a new thread startup object. + static nsThreadStartupEvent *Create() { + return new nsThreadStartupEvent(); } // This method does not return until the thread startup object is in the @@ -180,6 +180,11 @@ private: return NS_OK; } + nsThreadStartupEvent() + : mMon("nsThreadStartupEvent.mMon") + , mInitialized(false) { + } + ReentrantMonitor mMon; bool mInitialized; }; @@ -305,7 +310,8 @@ nsresult nsThread::Init() { // spawn thread and wait until it is fully setup - nsRefPtr startup = new nsThreadStartupEvent(); + nsRefPtr startup = nsThreadStartupEvent::Create(); + NS_ENSURE_TRUE(startup, NS_ERROR_OUT_OF_MEMORY); NS_ADDREF_THIS(); @@ -371,8 +377,7 @@ nsThread::Dispatch(nsIRunnable *event, uint32_t flags) { LOG(("THRD(%p) Dispatch [%p %x]\n", this, event, flags)); - if (NS_WARN_IF(!event)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(event); if (gXPCOMThreadsShutDown && MAIN_THREAD != mIsMainThread) { return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; @@ -380,8 +385,7 @@ nsThread::Dispatch(nsIRunnable *event, uint32_t flags) if (flags & DISPATCH_SYNC) { nsThread *thread = nsThreadManager::get()->GetCurrentThread(); - if (NS_WARN_IF(!thread)) - return NS_ERROR_NOT_AVAILABLE; + NS_ENSURE_STATE(thread); // XXX we should be able to do something better here... we should // be able to monitor the slot occupied by this event and use @@ -433,8 +437,7 @@ nsThread::Shutdown() if (!mThread) return NS_OK; - if (NS_WARN_IF(mThread == PR_GetCurrentThread())) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(mThread != PR_GetCurrentThread()); // Prevent multiple calls to this method { @@ -487,8 +490,7 @@ nsThread::Shutdown() NS_IMETHODIMP nsThread::HasPendingEvents(bool *result) { - if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) - return NS_ERROR_NOT_SAME_THREAD; + NS_ENSURE_STATE(PR_GetCurrentThread() == mThread); *result = mEvents.GetEvent(false, nullptr); return NS_OK; @@ -546,8 +548,7 @@ nsThread::ProcessNextEvent(bool mayWait, bool *result) { LOG(("THRD(%p) ProcessNextEvent [%u %u]\n", this, mayWait, mRunningEvent)); - if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) - return NS_ERROR_NOT_SAME_THREAD; + NS_ENSURE_STATE(PR_GetCurrentThread() == mThread); if (MAIN_THREAD == mIsMainThread && mayWait && !ShuttingDown()) HangMonitor::Suspend(); @@ -643,8 +644,7 @@ nsThread::GetPriority(int32_t *priority) NS_IMETHODIMP nsThread::SetPriority(int32_t priority) { - if (NS_WARN_IF(!mThread)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_STATE(mThread); // NSPR defines the following four thread priorities: // PR_PRIORITY_LOW @@ -690,8 +690,7 @@ nsThread::GetObserver(nsIThreadObserver **obs) NS_IMETHODIMP nsThread::SetObserver(nsIThreadObserver *obs) { - if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) - return NS_ERROR_NOT_SAME_THREAD; + NS_ENSURE_STATE(PR_GetCurrentThread() == mThread); MutexAutoLock lock(mLock); mObserver = obs; @@ -701,8 +700,8 @@ nsThread::SetObserver(nsIThreadObserver *obs) NS_IMETHODIMP nsThread::GetRecursionDepth(uint32_t *depth) { - if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) - return NS_ERROR_NOT_SAME_THREAD; + NS_ENSURE_ARG_POINTER(depth); + NS_ENSURE_STATE(PR_GetCurrentThread() == mThread); *depth = mRunningEvent; return NS_OK; @@ -711,10 +710,8 @@ nsThread::GetRecursionDepth(uint32_t *depth) NS_IMETHODIMP nsThread::AddObserver(nsIThreadObserver *observer) { - if (NS_WARN_IF(!observer)) - return NS_ERROR_INVALID_ARG; - if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) - return NS_ERROR_NOT_SAME_THREAD; + NS_ENSURE_ARG_POINTER(observer); + NS_ENSURE_STATE(PR_GetCurrentThread() == mThread); NS_WARN_IF_FALSE(!mEventObservers.Contains(observer), "Adding an observer twice!"); @@ -730,8 +727,7 @@ nsThread::AddObserver(nsIThreadObserver *observer) NS_IMETHODIMP nsThread::RemoveObserver(nsIThreadObserver *observer) { - if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) - return NS_ERROR_NOT_SAME_THREAD; + NS_ENSURE_STATE(PR_GetCurrentThread() == mThread); if (observer && !mEventObservers.RemoveElement(observer)) { NS_WARNING("Removing an observer that was never added!"); diff --git a/xpcom/threads/nsThreadManager.cpp b/xpcom/threads/nsThreadManager.cpp index cf41dcdb49a..52d1364f695 100644 --- a/xpcom/threads/nsThreadManager.cpp +++ b/xpcom/threads/nsThreadManager.cpp @@ -217,8 +217,7 @@ nsThreadManager::NewThread(uint32_t creationFlags, nsIThread **result) { // No new threads during Shutdown - if (NS_WARN_IF(!mInitialized)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED); nsThread *thr = new nsThread(nsThread::NOT_MAIN_THREAD, stackSize); if (!thr) @@ -243,10 +242,8 @@ NS_IMETHODIMP nsThreadManager::GetThreadFromPRThread(PRThread *thread, nsIThread **result) { // Keep this functioning during Shutdown - if (NS_WARN_IF(!mMainThread)) - return NS_ERROR_NOT_INITIALIZED; - if (NS_WARN_IF(!thread)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_TRUE(mMainThread, NS_ERROR_NOT_INITIALIZED); + NS_ENSURE_ARG_POINTER(thread); nsRefPtr temp; { @@ -262,8 +259,7 @@ NS_IMETHODIMP nsThreadManager::GetMainThread(nsIThread **result) { // Keep this functioning during Shutdown - if (NS_WARN_IF(!mMainThread)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mMainThread, NS_ERROR_NOT_INITIALIZED); NS_ADDREF(*result = mMainThread); return NS_OK; } @@ -272,8 +268,7 @@ NS_IMETHODIMP nsThreadManager::GetCurrentThread(nsIThread **result) { // Keep this functioning during Shutdown - if (NS_WARN_IF(!mMainThread)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(mMainThread, NS_ERROR_NOT_INITIALIZED); *result = GetCurrentThread(); if (!*result) return NS_ERROR_OUT_OF_MEMORY; diff --git a/xpcom/threads/nsThreadPool.cpp b/xpcom/threads/nsThreadPool.cpp index 6e3236db7a7..2ccc4b0142f 100644 --- a/xpcom/threads/nsThreadPool.cpp +++ b/xpcom/threads/nsThreadPool.cpp @@ -92,8 +92,7 @@ nsThreadPool::PutEvent(nsIRunnable *event) nsThreadManager::get()->NewThread(0, nsIThreadManager::DEFAULT_STACK_SIZE, getter_AddRefs(thread)); - if (NS_WARN_IF(!thread)) - return NS_ERROR_UNEXPECTED; + NS_ENSURE_STATE(thread); bool killThread = false; { @@ -226,14 +225,12 @@ nsThreadPool::Dispatch(nsIRunnable *event, uint32_t flags) { LOG(("THRD-P(%p) dispatch [%p %x]\n", this, event, flags)); - if (NS_WARN_IF(mShutdown)) - return NS_ERROR_NOT_AVAILABLE; + NS_ENSURE_STATE(!mShutdown); if (flags & DISPATCH_SYNC) { nsCOMPtr thread; nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thread)); - if (NS_WARN_IF(!thread)) - return NS_ERROR_NOT_AVAILABLE; + NS_ENSURE_STATE(thread); nsRefPtr wrapper = new nsThreadSyncDispatch(thread, event); diff --git a/xpcom/threads/nsTimerImpl.cpp b/xpcom/threads/nsTimerImpl.cpp index 3aa9e606e24..bb59fc98bf1 100644 --- a/xpcom/threads/nsTimerImpl.cpp +++ b/xpcom/threads/nsTimerImpl.cpp @@ -317,16 +317,14 @@ nsresult nsTimerImpl::InitCommon(uint32_t aType, uint32_t aDelay) { nsresult rv; - if (NS_WARN_IF(!gThread)) - return NS_ERROR_NOT_INITIALIZED; + NS_ENSURE_TRUE(gThread, NS_ERROR_NOT_INITIALIZED); if (!mEventTarget) { NS_ERROR("mEventTarget is NULL"); return NS_ERROR_NOT_INITIALIZED; } rv = gThread->Init(); - if (NS_WARN_IF(NS_FAILED(rv))) - return rv; + NS_ENSURE_SUCCESS(rv, rv); /** * In case of re-Init, both with and without a preceding Cancel, clear the @@ -359,8 +357,7 @@ NS_IMETHODIMP nsTimerImpl::InitWithFuncCallback(nsTimerCallbackFunc aFunc, uint32_t aDelay, uint32_t aType) { - if (NS_WARN_IF(!aFunc)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aFunc); ReleaseCallback(); mCallbackType = CALLBACK_TYPE_FUNC; @@ -374,8 +371,7 @@ NS_IMETHODIMP nsTimerImpl::InitWithCallback(nsITimerCallback *aCallback, uint32_t aDelay, uint32_t aType) { - if (NS_WARN_IF(!aCallback)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aCallback); ReleaseCallback(); mCallbackType = CALLBACK_TYPE_INTERFACE; @@ -389,8 +385,7 @@ NS_IMETHODIMP nsTimerImpl::Init(nsIObserver *aObserver, uint32_t aDelay, uint32_t aType) { - if (NS_WARN_IF(!aObserver)) - return NS_ERROR_INVALID_ARG; + NS_ENSURE_ARG_POINTER(aObserver); ReleaseCallback(); mCallbackType = CALLBACK_TYPE_OBSERVER; @@ -486,8 +481,8 @@ NS_IMETHODIMP nsTimerImpl::GetTarget(nsIEventTarget** aTarget) NS_IMETHODIMP nsTimerImpl::SetTarget(nsIEventTarget* aTarget) { - if (NS_WARN_IF(mCallbackType != CALLBACK_TYPE_UNKNOWN)) - return NS_ERROR_ALREADY_INITIALIZED; + NS_ENSURE_TRUE(mCallbackType == CALLBACK_TYPE_UNKNOWN, + NS_ERROR_ALREADY_INITIALIZED); if (aTarget) mEventTarget = aTarget;