diff --git a/xpcom/ds/nsSupportsPrimitives.cpp b/xpcom/ds/nsSupportsPrimitives.cpp index 76c1fb44414..a3901a44c5d 100644 --- a/xpcom/ds/nsSupportsPrimitives.cpp +++ b/xpcom/ds/nsSupportsPrimitives.cpp @@ -6,8 +6,24 @@ #include "nsSupportsPrimitives.h" #include "nsMemory.h" +#include "mozilla/Assertions.h" #include "mozilla/IntegerPrintfMacros.h" #include "mozilla/Snprintf.h" +#include + +template +static char* +DataToString(const char* aFormat, T aData) +{ + static const int size = 32; + char buf[size]; + + int len = snprintf_literal(buf, aFormat, aData); + MOZ_ASSERT(len >= 0); + + return static_cast(nsMemory::Clone(buf, std::min(len + 1, size) * + sizeof(char))); +} /***************************************************************************/ @@ -23,7 +39,6 @@ nsSupportsIDImpl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_ID; - return NS_OK; } @@ -31,11 +46,13 @@ NS_IMETHODIMP nsSupportsIDImpl::GetData(nsID** aData) { NS_ASSERTION(aData, "Bad pointer"); + if (mData) { - *aData = (nsID*)nsMemory::Clone(mData, sizeof(nsID)); - return *aData ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aData = static_cast(nsMemory::Clone(mData, sizeof(nsID))); + } else { + *aData = nullptr; } - *aData = nullptr; + return NS_OK; } @@ -45,28 +62,29 @@ nsSupportsIDImpl::SetData(const nsID* aData) if (mData) { free(mData); } + if (aData) { - mData = (nsID*)nsMemory::Clone(aData, sizeof(nsID)); + mData = static_cast(nsMemory::Clone(aData, sizeof(nsID))); } else { mData = nullptr; } + return NS_OK; } NS_IMETHODIMP nsSupportsIDImpl::ToString(char** aResult) { - char* result; NS_ASSERTION(aResult, "Bad pointer"); + if (mData) { - result = mData->ToString(); + *aResult = mData->ToString(); } else { static const char nullStr[] = "null"; - result = (char*)nsMemory::Clone(nullStr, sizeof(nullStr)); + *aResult = static_cast(nsMemory::Clone(nullStr, sizeof(nullStr))); } - *aResult = result; - return result ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + return NS_OK; } /***************************************************************************** @@ -80,7 +98,6 @@ NS_IMETHODIMP nsSupportsCStringImpl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); - *aType = TYPE_CSTRING; return NS_OK; } @@ -96,7 +113,6 @@ NS_IMETHODIMP nsSupportsCStringImpl::ToString(char** aResult) { *aResult = ToNewCString(mData); - if (!*aResult) { return NS_ERROR_OUT_OF_MEMORY; } @@ -111,6 +127,7 @@ nsSupportsCStringImpl::SetData(const nsACString& aData) if (!ok) { return NS_ERROR_OUT_OF_MEMORY; } + return NS_OK; } @@ -125,7 +142,6 @@ NS_IMETHODIMP nsSupportsStringImpl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); - *aType = TYPE_STRING; return NS_OK; } @@ -141,7 +157,6 @@ NS_IMETHODIMP nsSupportsStringImpl::ToString(char16_t** aResult) { *aResult = ToNewUnicode(mData); - if (!*aResult) { return NS_ERROR_OUT_OF_MEMORY; } @@ -156,6 +171,7 @@ nsSupportsStringImpl::SetData(const nsAString& aData) if (!ok) { return NS_ERROR_OUT_OF_MEMORY; } + return NS_OK; } @@ -174,7 +190,6 @@ nsSupportsPRBoolImpl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_PRBOOL; - return NS_OK; } @@ -198,8 +213,9 @@ nsSupportsPRBoolImpl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); const char* str = mData ? "true" : "false"; - *aResult = (char*)nsMemory::Clone(str, (strlen(str) + 1) * sizeof(char)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = static_cast(nsMemory::Clone(str, (strlen(str) + 1) * + sizeof(char))); + return NS_OK; } /***************************************************************************/ @@ -217,7 +233,6 @@ nsSupportsPRUint8Impl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_PRUINT8; - return NS_OK; } @@ -240,11 +255,8 @@ NS_IMETHODIMP nsSupportsPRUint8Impl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); - char buf[8]; - snprintf_literal(buf, "%u", (unsigned int)mData); - - *aResult = (char*)nsMemory::Clone(buf, (strlen(buf) + 1) * sizeof(char)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = DataToString("%u", static_cast(mData)); + return NS_OK; } /***************************************************************************/ @@ -262,7 +274,6 @@ nsSupportsPRUint16Impl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_PRUINT16; - return NS_OK; } @@ -285,11 +296,8 @@ NS_IMETHODIMP nsSupportsPRUint16Impl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); - char buf[8]; - snprintf_literal(buf, "%u", (unsigned int)mData); - - *aResult = (char*)nsMemory::Clone(buf, (strlen(buf) + 1) * sizeof(char)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = DataToString("%u", static_cast(mData)); + return NS_OK; } /***************************************************************************/ @@ -307,7 +315,6 @@ nsSupportsPRUint32Impl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_PRUINT32; - return NS_OK; } @@ -330,11 +337,8 @@ NS_IMETHODIMP nsSupportsPRUint32Impl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); - char buf[16]; - snprintf_literal(buf, "%u", mData); - - *aResult = (char*)nsMemory::Clone(buf, (strlen(buf) + 1) * sizeof(char)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = DataToString("%u", mData); + return NS_OK; } /***************************************************************************/ @@ -352,7 +356,6 @@ nsSupportsPRUint64Impl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_PRUINT64; - return NS_OK; } @@ -375,11 +378,8 @@ NS_IMETHODIMP nsSupportsPRUint64Impl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); - char buf[32]; - snprintf_literal(buf, "%llu", mData); - - *aResult = (char*)nsMemory::Clone(buf, (strlen(buf) + 1) * sizeof(char)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = DataToString("%llu", mData); + return NS_OK; } /***************************************************************************/ @@ -397,7 +397,6 @@ nsSupportsPRTimeImpl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_PRTIME; - return NS_OK; } @@ -420,11 +419,8 @@ NS_IMETHODIMP nsSupportsPRTimeImpl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); - char buf[32]; - snprintf_literal(buf, "%" PRIu64, mData); - - *aResult = (char*)nsMemory::Clone(buf, (strlen(buf) + 1) * sizeof(char)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = DataToString("%" PRIu64, mData); + return NS_OK; } /***************************************************************************/ @@ -442,7 +438,6 @@ nsSupportsCharImpl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_CHAR; - return NS_OK; } @@ -465,14 +460,11 @@ NS_IMETHODIMP nsSupportsCharImpl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); + *aResult = static_cast(moz_xmalloc(2 * sizeof(char))); + *aResult[0] = mData; + *aResult[1] = '\0'; - char* result = (char*)moz_xmalloc(2 * sizeof(char)); - if (result) { - result[0] = mData; - result[1] = '\0'; - } - *aResult = result; - return result ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + return NS_OK; } /***************************************************************************/ @@ -490,7 +482,6 @@ nsSupportsPRInt16Impl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_PRINT16; - return NS_OK; } @@ -513,11 +504,8 @@ NS_IMETHODIMP nsSupportsPRInt16Impl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); - char buf[8]; - snprintf_literal(buf, "%d", (int)mData); - - *aResult = (char*)nsMemory::Clone(buf, (strlen(buf) + 1) * sizeof(char)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = DataToString("%d", static_cast(mData)); + return NS_OK; } /***************************************************************************/ @@ -535,7 +523,6 @@ nsSupportsPRInt32Impl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_PRINT32; - return NS_OK; } @@ -558,11 +545,8 @@ NS_IMETHODIMP nsSupportsPRInt32Impl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); - char buf[16]; - snprintf_literal(buf, "%d", mData); - - *aResult = (char*)nsMemory::Clone(buf, (strlen(buf) + 1) * sizeof(char)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = DataToString("%d", mData); + return NS_OK; } /***************************************************************************/ @@ -580,7 +564,6 @@ nsSupportsPRInt64Impl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_PRINT64; - return NS_OK; } @@ -603,11 +586,8 @@ NS_IMETHODIMP nsSupportsPRInt64Impl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); - char buf[32]; - snprintf_literal(buf, "%" PRId64, mData); - - *aResult = (char*)nsMemory::Clone(buf, (strlen(buf) + 1) * sizeof(char)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = DataToString("%" PRId64, mData); + return NS_OK; } /***************************************************************************/ @@ -625,7 +605,6 @@ nsSupportsFloatImpl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_FLOAT; - return NS_OK; } @@ -648,11 +627,8 @@ NS_IMETHODIMP nsSupportsFloatImpl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); - char buf[32]; - snprintf_literal(buf, "%f", (double)mData); - - *aResult = (char*)nsMemory::Clone(buf, (strlen(buf) + 1) * sizeof(char)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = DataToString("%f", static_cast(mData)); + return NS_OK; } /***************************************************************************/ @@ -670,7 +646,6 @@ nsSupportsDoubleImpl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_DOUBLE; - return NS_OK; } @@ -693,11 +668,8 @@ NS_IMETHODIMP nsSupportsDoubleImpl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); - char buf[32]; - snprintf_literal(buf, "%f", mData); - - *aResult = (char*)nsMemory::Clone(buf, (strlen(buf) + 1) * sizeof(char)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = DataToString("%f", mData); + return NS_OK; } /***************************************************************************/ @@ -716,7 +688,6 @@ nsSupportsVoidImpl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_VOID; - return NS_OK; } @@ -739,11 +710,9 @@ NS_IMETHODIMP nsSupportsVoidImpl::ToString(char** aResult) { NS_ASSERTION(aResult, "Bad pointer"); - static const char str[] = "[raw data]"; - char* result = (char*)nsMemory::Clone(str, sizeof(str)); - *aResult = result; - return result ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = static_cast(nsMemory::Clone(str, sizeof(str))); + return NS_OK; } /***************************************************************************/ @@ -770,7 +739,6 @@ nsSupportsInterfacePointerImpl::GetType(uint16_t* aType) { NS_ASSERTION(aType, "Bad pointer"); *aType = TYPE_INTERFACE_POINTER; - return NS_OK; } @@ -778,10 +746,8 @@ NS_IMETHODIMP nsSupportsInterfacePointerImpl::GetData(nsISupports** aData) { NS_ASSERTION(aData, "Bad pointer"); - *aData = mData; NS_IF_ADDREF(*aData); - return NS_OK; } @@ -789,7 +755,6 @@ NS_IMETHODIMP nsSupportsInterfacePointerImpl::SetData(nsISupports* aData) { mData = aData; - return NS_OK; } @@ -799,10 +764,11 @@ nsSupportsInterfacePointerImpl::GetDataIID(nsID** aIID) NS_ASSERTION(aIID, "Bad pointer"); if (mIID) { - *aIID = (nsID*)nsMemory::Clone(mIID, sizeof(nsID)); - return *aIID ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aIID = static_cast(nsMemory::Clone(mIID, sizeof(nsID))); + } else { + *aIID = nullptr; } - *aIID = nullptr; + return NS_OK; } @@ -812,8 +778,9 @@ nsSupportsInterfacePointerImpl::SetDataIID(const nsID* aIID) if (mIID) { free(mIID); } + if (aIID) { - mIID = (nsID*)nsMemory::Clone(aIID, sizeof(nsID)); + mIID = static_cast(nsMemory::Clone(aIID, sizeof(nsID))); } else { mIID = nullptr; } @@ -827,11 +794,10 @@ nsSupportsInterfacePointerImpl::ToString(char** aResult) NS_ASSERTION(aResult, "Bad pointer"); static const char str[] = "[interface pointer]"; - // jband sez: think about asking nsIInterfaceInfoManager whether // the interface has a known human-readable name - *aResult = (char*)nsMemory::Clone(str, sizeof(str)); - return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + *aResult = static_cast(nsMemory::Clone(str, sizeof(str))); + return NS_OK; } /***************************************************************************/