Bug 1265030 - Show symbolic nsresult name in NS_ENSURE_SUCCESS warning. r=mccr8

Through their use of Smprintf, the existing warning message formatting
is resistant to OOM errors.  So I figured that we should probably use
something that doesn't infallibly allocate like GetErrorName does.

Differential Revision: https://phabricator.services.mozilla.com/D70771

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Cameron McCormack 2020-04-13 23:39:19 +00:00
parent 094d451696
commit 4887043b29
3 changed files with 22 additions and 10 deletions

View File

@ -14,6 +14,8 @@
namespace mozilla {
const char* GetStaticErrorName(nsresult rv) { return GetErrorNameInternal(rv); }
void GetErrorName(nsresult rv, nsACString& name) {
if (const char* errorName = GetErrorNameInternal(rv)) {
name.AssignASCII(errorName);

View File

@ -19,6 +19,11 @@ namespace mozilla {
// "NS_ERROR_GENERATE_FAILURE(<module>, <code>)".
void GetErrorName(nsresult rv, nsACString& name);
// Same as GetErrorName, except that only nsresult values with statically
// known symbolic names are handled. For all other values, nullptr is
// returned.
const char* GetStaticErrorName(nsresult rv);
} // namespace mozilla
#endif // mozilla_ErrorNames_h

View File

@ -17,6 +17,7 @@
#include <stdarg.h>
#ifdef DEBUG
# include "mozilla/ErrorNames.h"
# include "mozilla/IntegerPrintfMacros.h"
# include "mozilla/Printf.h"
#endif
@ -247,18 +248,22 @@ inline void MOZ_PretendNoReturn() MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS {}
#if defined(DEBUG) && !defined(XPCOM_GLUE_AVOID_NSPR)
# define NS_ENSURE_SUCCESS_BODY(res, ret) \
mozilla::SmprintfPointer msg = mozilla::Smprintf( \
"NS_ENSURE_SUCCESS(%s, %s) failed with " \
"result 0x%" PRIX32, \
#res, #ret, static_cast<uint32_t>(__rv)); \
# define NS_ENSURE_SUCCESS_BODY(res, ret) \
const char* name = mozilla::GetStaticErrorName(__rv); \
mozilla::SmprintfPointer msg = mozilla::Smprintf( \
"NS_ENSURE_SUCCESS(%s, %s) failed with " \
"result 0x%" PRIX32 "%s%s%s", \
#res, #ret, static_cast<uint32_t>(__rv), name ? " (" : "", \
name ? name : "", name ? ")" : ""); \
NS_WARNING(msg.get());
# define NS_ENSURE_SUCCESS_BODY_VOID(res) \
mozilla::SmprintfPointer msg = mozilla::Smprintf( \
"NS_ENSURE_SUCCESS_VOID(%s) failed with " \
"result 0x%" PRIX32, \
#res, static_cast<uint32_t>(__rv)); \
# define NS_ENSURE_SUCCESS_BODY_VOID(res) \
const char* name = mozilla::GetStaticErrorName(__rv); \
mozilla::SmprintfPointer msg = mozilla::Smprintf( \
"NS_ENSURE_SUCCESS_VOID(%s) failed with " \
"result 0x%" PRIX32 "%s%s%s", \
#res, static_cast<uint32_t>(__rv), name ? " (" : "", name ? name : "", \
name ? ")" : ""); \
NS_WARNING(msg.get());
#else