diff --git a/content/base/src/nsHostObjectProtocolHandler.cpp b/content/base/src/nsHostObjectProtocolHandler.cpp index 7f6413149fc..d9aab865042 100644 --- a/content/base/src/nsHostObjectProtocolHandler.cpp +++ b/content/base/src/nsHostObjectProtocolHandler.cpp @@ -94,16 +94,14 @@ class BlobURLsReporter MOZ_FINAL : public nsIMemoryReporter } for (uint32_t i = 0; i < maxFrames && frame; ++i) { - nsAutoCString fileNameEscaped; - char* fileName = nullptr; + nsCString fileName; int32_t lineNumber = 0; - frame->GetFilename(&fileName); + frame->GetFilename(fileName); frame->GetLineNumber(&lineNumber); - if (fileName != nullptr && fileName[0] != '\0') { + if (!fileName.IsEmpty()) { stack += "js("; - fileNameEscaped = fileName; if (!origin.IsEmpty()) { // Make the file name root-relative for conciseness if possible. const char* originData; @@ -111,14 +109,14 @@ class BlobURLsReporter MOZ_FINAL : public nsIMemoryReporter originLen = origin.GetData(&originData); // If fileName starts with origin + "/", cut up to that "/". - if (strlen(fileName) >= originLen + 1 && - memcmp(fileName, originData, originLen) == 0 && + if (fileName.Length() >= originLen + 1 && + memcmp(fileName.get(), originData, originLen) == 0 && fileName[originLen] == '/') { - fileNameEscaped.Cut(0, originLen); + fileName.Cut(0, originLen); } } - fileNameEscaped.ReplaceChar('/', '\\'); - stack += fileNameEscaped; + fileName.ReplaceChar('/', '\\'); + stack += fileName; if (lineNumber > 0) { stack += ", line="; stack.AppendInt(lineNumber); diff --git a/dom/base/DOMError.cpp b/dom/base/DOMError.cpp index 8da1b9ab791..2399075dac0 100644 --- a/dom/base/DOMError.cpp +++ b/dom/base/DOMError.cpp @@ -29,11 +29,11 @@ DOMError::DOMError(nsPIDOMWindow* aWindow) DOMError::DOMError(nsPIDOMWindow* aWindow, nsresult aValue) : mWindow(aWindow) { - const char *name, *message; - NS_GetNameAndMessageForDOMNSResult(aValue, &name, &message); + nsCString name, message; + NS_GetNameAndMessageForDOMNSResult(aValue, name, message); - mName = NS_ConvertASCIItoUTF16(name); - mMessage = NS_ConvertASCIItoUTF16(message); + CopyUTF8toUTF16(name, mName); + CopyUTF8toUTF16(message, mMessage); SetIsDOMBinding(); } diff --git a/dom/base/DOMException.cpp b/dom/base/DOMException.cpp index b35bd6c41af..af5b6ed7b7d 100644 --- a/dom/base/DOMException.cpp +++ b/dom/base/DOMException.cpp @@ -89,17 +89,19 @@ static const struct ResultStruct static void NSResultToNameAndMessage(nsresult aNSResult, - const char** aName, - const char** aMessage, + nsCString& aName, + nsCString& aMessage, uint16_t* aCode) { - *aName = nullptr; - *aMessage = nullptr; + aName.Truncate(); + aMessage.Truncate(); *aCode = 0; for (uint32_t idx = 0; idx < ArrayLength(sDOMErrorMsgMap); idx++) { if (aNSResult == sDOMErrorMsgMap[idx].mNSResult) { - *aName = sDOMErrorMsgMap[idx].mName; - *aMessage = sDOMErrorMsgMap[idx].mMessage; + aName.Rebind(sDOMErrorMsgMap[idx].mName, + strlen(sDOMErrorMsgMap[idx].mName)); + aMessage.Rebind(sDOMErrorMsgMap[idx].mMessage, + strlen(sDOMErrorMsgMap[idx].mMessage)); *aCode = sDOMErrorMsgMap[idx].mCode; return; } @@ -111,17 +113,17 @@ NSResultToNameAndMessage(nsresult aNSResult, } nsresult -NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, const char** aName, - const char** aMessage, uint16_t* aCode) +NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, nsACString& aName, + nsACString& aMessage, uint16_t* aCode) { - const char* name = nullptr; - const char* message = nullptr; + nsCString name; + nsCString message; uint16_t code = 0; - NSResultToNameAndMessage(aNSResult, &name, &message, &code); + NSResultToNameAndMessage(aNSResult, name, message, &code); - if (name && message) { - *aName = name; - *aMessage = message; + if (!name.IsEmpty() && !message.IsEmpty()) { + aName = name; + aMessage = message; if (aCode) { *aCode = code; } @@ -170,19 +172,13 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CI_INTERFACE_GETTER1(Exception, nsIXPCException) -Exception::Exception(const char *aMessage, +Exception::Exception(const nsACString& aMessage, nsresult aResult, - const char *aName, + const nsACString& aName, nsIStackFrame *aLocation, nsISupports *aData) -: mMessage(nullptr), - mResult(NS_OK), - mName(nullptr), - mLocation(nullptr), - mData(nullptr), - mFilename(nullptr), +: mResult(NS_OK), mLineNumber(0), - mInner(nullptr), mInitialized(false), mHoldingJSVal(false) { @@ -235,10 +231,7 @@ Exception::Exception(const char *aMessage, } Exception::Exception() - : mMessage(nullptr), - mResult(NS_OK), - mName(nullptr), - mFilename(nullptr), + : mResult(NS_OK), mLineNumber(-1), mInitialized(false), mHoldingJSVal(false) @@ -247,19 +240,6 @@ Exception::Exception() Exception::~Exception() { - if (mMessage) { - nsMemory::Free(mMessage); - mMessage = nullptr; - } - if (mName) { - nsMemory::Free(mName); - mName = nullptr; - } - if (mFilename) { - nsMemory::Free(mFilename); - mFilename = nullptr; - } - if (mHoldingJSVal) { MOZ_ASSERT(NS_IsMainThread()); @@ -296,20 +276,13 @@ Exception::StowJSVal(JS::Value& aVp) } } -/* readonly attribute string message; */ +/* readonly attribute AUTF8String message; */ NS_IMETHODIMP -Exception::GetMessageMoz(char** aMessage) +Exception::GetMessageMoz(nsACString& aMessage) { - NS_ENSURE_ARG_POINTER(aMessage); NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED); - if (mMessage) { - *aMessage = - (char*) nsMemory::Clone(mMessage, sizeof(char)*(strlen(mMessage)+1)); - } else { - *aMessage = nullptr; - } - + aMessage.Assign(mMessage); return NS_OK; } @@ -324,30 +297,31 @@ Exception::GetResult(nsresult* aResult) return NS_OK; } -/* readonly attribute string name; */ +/* readonly attribute AUTF8String name; */ NS_IMETHODIMP -Exception::GetName(char** aName) +Exception::GetName(nsACString& aName) { - NS_ENSURE_ARG_POINTER(aName); NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED); - const char* name = mName; - if (!name) { - nsXPCException::NameAndFormatForNSResult(mResult, &name, nullptr); - } - - if (name) { - *aName = (char*) nsMemory::Clone(name, sizeof(char)*(strlen(name)+1)); + if (!mName.IsEmpty()) { + aName.Assign(mName); } else { - *aName = nullptr; + aName.Truncate(); + + const char* name = nullptr; + nsXPCException::NameAndFormatForNSResult(mResult, &name, nullptr); + + if (name) { + aName.Assign(name); + } } return NS_OK; } -/* readonly attribute string filename; */ +/* readonly attribute AUTF8String filename; */ NS_IMETHODIMP -Exception::GetFilename(char** aFilename) +Exception::GetFilename(nsACString& aFilename) { NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED); @@ -355,7 +329,8 @@ Exception::GetFilename(char** aFilename) return mLocation->GetFilename(aFilename); } - XPC_STRING_GETTER_BODY(aFilename, mFilename); + aFilename.Assign(mFilename); + return NS_OK; } /* readonly attribute uint32_t lineNumber; */ @@ -423,11 +398,10 @@ Exception::GetInner(nsIException** aException) return NS_OK; } -/* string toString (); */ +/* AUTF8String toString (); */ NS_IMETHODIMP -Exception::ToString(char **_retval) +Exception::ToString(nsACString& _retval) { - NS_ENSURE_ARG_POINTER(_retval); NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED); static const char defaultMsg[] = ""; @@ -435,18 +409,21 @@ Exception::ToString(char **_retval) static const char format[] = "[Exception... \"%s\" nsresult: \"0x%x (%s)\" location: \"%s\" data: %s]"; - char* indicatedLocation = nullptr; + nsCString location; if (mLocation) { // we need to free this if it does not fail - nsresult rv = mLocation->ToString(&indicatedLocation); + nsresult rv = mLocation->ToString(location); NS_ENSURE_SUCCESS(rv, rv); } - const char* msg = mMessage ? mMessage : nullptr; - const char* location = indicatedLocation ? - indicatedLocation : defaultLocation; - const char* resultName = mName; + if (location.IsEmpty()) { + location.Assign(defaultLocation); + } + + const char* msg = mMessage.IsEmpty() ? nullptr : mMessage.get(); + + const char* resultName = mName.IsEmpty() ? nullptr: mName.get(); if (!resultName && !nsXPCException::NameAndFormatForNSResult(mResult, &resultName, (!msg) ? &msg : nullptr)) { @@ -457,36 +434,24 @@ Exception::ToString(char **_retval) } const char* data = mData ? "yes" : "no"; - char* temp = JS_smprintf(format, msg, mResult, resultName, location, data); - if (indicatedLocation) { - nsMemory::Free(indicatedLocation); - } - - char* final = nullptr; - if (temp) { - final = (char*) nsMemory::Clone(temp, sizeof(char)*(strlen(temp)+1)); - JS_smprintf_free(temp); - } - - *_retval = final; - return final ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + _retval.Truncate(); + _retval.AppendPrintf(format, msg, mResult, resultName, + location.get(), data); + return NS_OK; } -/* void initialize (in string aMessage, in nsresult aResult, in string aName, in nsIStackFrame aLocation, in nsISupports aData, in nsIException aInner); */ +/* void initialize (in AUTF8String aMessage, in nsresult aResult, + * in AUTF8String aName, in nsIStackFrame aLocation, + * in nsISupports aData, in nsIException aInner); */ NS_IMETHODIMP -Exception::Initialize(const char *aMessage, nsresult aResult, const char *aName, nsIStackFrame *aLocation, nsISupports *aData, nsIException *aInner) +Exception::Initialize(const nsACString& aMessage, nsresult aResult, + const nsACString& aName, nsIStackFrame *aLocation, + nsISupports *aData, nsIException *aInner) { NS_ENSURE_FALSE(mInitialized, NS_ERROR_ALREADY_INITIALIZED); - if (aMessage) { - mMessage = - (char*) nsMemory::Clone(aMessage, sizeof(char)*(strlen(aMessage)+1)); - } - - if (aName) { - mName = (char*) nsMemory::Clone(aName, sizeof(char)*(strlen(aName)+1)); - } - + mMessage = aMessage; + mName = aName; mResult = aResult; if (aLocation) { @@ -516,14 +481,13 @@ Exception::WrapObject(JSContext* cx, JS::Handle scope) void Exception::GetMessageMoz(nsString& retval) { - char* str = nullptr; + nsCString str; #ifdef DEBUG DebugOnly rv = #endif - GetMessageMoz(&str); + GetMessageMoz(str); MOZ_ASSERT(NS_SUCCEEDED(rv)); CopyUTF8toUTF16(str, retval); - nsMemory::Free(str); } uint32_t @@ -535,27 +499,25 @@ Exception::Result() const void Exception::GetName(nsString& retval) { - char* str = nullptr; + nsCString str; #ifdef DEBUG DebugOnly rv = #endif - GetName(&str); + GetName(str); MOZ_ASSERT(NS_SUCCEEDED(rv)); CopyUTF8toUTF16(str, retval); - nsMemory::Free(str); } void Exception::GetFilename(nsString& retval) { - char* str = nullptr; + nsCString str; #ifdef DEBUG DebugOnly rv = #endif - GetFilename(&str); + GetFilename(str); MOZ_ASSERT(NS_SUCCEEDED(rv)); CopyUTF8toUTF16(str, retval); - nsMemory::Free(str); } uint32_t @@ -602,14 +564,13 @@ Exception::GetData() const void Exception::Stringify(nsString& retval) { - char* str = nullptr; + nsCString str; #ifdef DEBUG DebugOnly rv = #endif - ToString(&str); + ToString(str); MOZ_ASSERT(NS_SUCCEEDED(rv)); CopyUTF8toUTF16(str, retval); - nsMemory::Free(str); } NS_IMPL_ADDREF_INHERITED(DOMException, Exception) @@ -618,9 +579,9 @@ NS_INTERFACE_MAP_BEGIN(DOMException) NS_INTERFACE_MAP_ENTRY(nsIDOMDOMException) NS_INTERFACE_MAP_END_INHERITING(Exception) -DOMException::DOMException(nsresult aRv, const char* aMessage, - const char* aName, uint16_t aCode) - : Exception(nullptr, aRv, nullptr, nullptr, nullptr), +DOMException::DOMException(nsresult aRv, const nsACString& aMessage, + const nsACString& aName, uint16_t aCode) + : Exception(EmptyCString(), aRv, EmptyCString(), nullptr, nullptr), mName(aName), mMessage(aMessage), mCode(aCode) @@ -647,9 +608,9 @@ DOMException::GetCode(uint16_t* aCode) } NS_IMETHODIMP -DOMException::ToString(char **aReturn) +DOMException::ToString(nsACString& aReturn) { - *aReturn = nullptr; + aReturn.Truncate(); static const char defaultMsg[] = ""; static const char defaultLocation[] = ""; @@ -660,9 +621,8 @@ DOMException::ToString(char **aReturn) nsAutoCString location; if (mInner) { - nsXPIDLCString filename; - - mInner->GetFilename(getter_Copies(filename)); + nsCString filename; + mInner->GetFilename(filename); if (!filename.IsEmpty()) { uint32_t line_nr = 0; @@ -681,13 +641,13 @@ DOMException::ToString(char **aReturn) location = defaultLocation; } - const char* msg = mMessage ? mMessage : defaultMsg; - const char* resultName = mName ? mName : defaultName; + const char* msg = !mMessage.IsEmpty() ? mMessage.get() : defaultMsg; + const char* resultName = !mName.IsEmpty() ? mName.get() : defaultName; - *aReturn = PR_smprintf(format, msg, mCode, mResult, resultName, - location.get()); + aReturn.AppendPrintf(format, msg, mCode, mResult, resultName, + location.get()); - return *aReturn ? NS_OK : NS_ERROR_OUT_OF_MEMORY; + return NS_OK; } void @@ -711,10 +671,10 @@ DOMException::WrapObject(JSContext* aCx, JS::Handle aScope) /* static */already_AddRefed DOMException::Create(nsresult aRv) { - const char* name; - const char* message; + nsCString name; + nsCString message; uint16_t code; - NSResultToNameAndMessage(aRv, &name, &message, &code); + NSResultToNameAndMessage(aRv, name, message, &code); nsRefPtr inst = new DOMException(aRv, message, name, code); return inst.forget(); diff --git a/dom/base/DOMException.h b/dom/base/DOMException.h index cdf5ebeaeac..58744ac771f 100644 --- a/dom/base/DOMException.h +++ b/dom/base/DOMException.h @@ -21,13 +21,14 @@ #include "nsIDOMDOMException.h" #include "nsWrapperCache.h" #include "xpcexception.h" +#include "nsString.h" class nsIStackFrame; class nsString; nsresult -NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, const char** aName, - const char** aMessage, +NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, nsACString& aName, + nsACString& aMessage, uint16_t* aCode = nullptr); namespace mozilla { @@ -85,21 +86,21 @@ public: // XPCOM factory ctor. Exception(); - Exception(const char *aMessage, + Exception(const nsACString& aMessage, nsresult aResult, - const char *aName, + const nsACString& aName, nsIStackFrame *aLocation, nsISupports *aData); protected: virtual ~Exception(); - char* mMessage; + nsCString mMessage; nsresult mResult; - char* mName; + nsCString mName; nsCOMPtr mLocation; nsCOMPtr mData; - char* mFilename; + nsCString mFilename; int mLineNumber; nsCOMPtr mInner; bool mInitialized; @@ -117,14 +118,14 @@ class DOMException : public Exception, public nsIDOMDOMException { public: - DOMException(nsresult aRv, const char* aMessage, - const char* aName, uint16_t aCode); + DOMException(nsresult aRv, const nsACString& aMessage, + const nsACString& aName, uint16_t aCode); NS_DECL_ISUPPORTS_INHERITED NS_DECL_NSIDOMDOMEXCEPTION // nsIException overrides - NS_IMETHOD ToString(char **aReturn) MOZ_OVERRIDE; + NS_IMETHOD ToString(nsACString& aReturn) MOZ_OVERRIDE; // nsWrapperCache overrides virtual JSObject* WrapObject(JSContext* aCx, JS::Handle aScope) @@ -145,9 +146,8 @@ protected: virtual ~DOMException() {} - // Intentionally shadow the nsXPCException version. - const char* mName; - const char* mMessage; + nsCString mName; + nsCString mMessage; uint16_t mCode; }; diff --git a/dom/bindings/Exceptions.cpp b/dom/bindings/Exceptions.cpp index 7c169dcb3a8..d4083d50e1c 100644 --- a/dom/bindings/Exceptions.cpp +++ b/dom/bindings/Exceptions.cpp @@ -147,7 +147,8 @@ Throw(JSContext* aCx, nsresult aRv, const char* aMessage) // If not, use the default. if (!finalException) { - finalException = new Exception(aMessage, aRv, nullptr, nullptr, nullptr); + finalException = new Exception(nsCString(aMessage), aRv, + EmptyCString(), nullptr, nullptr); } MOZ_ASSERT(finalException); @@ -292,16 +293,14 @@ private: return mLanguage == nsIProgrammingLanguage::JAVASCRIPT; } - const char* GetFilename(); - const char* GetFunname(); int32_t GetLineno(); nsRefPtr mStackDescription; nsCOMPtr mCaller; // Cached values - char* mFilename; - char* mFunname; + nsCString mFilename; + nsCString mFunname; int32_t mLineno; uint32_t mLanguage; @@ -315,9 +314,7 @@ private: JSStackFrame::JSStackFrame(StackDescriptionOwner* aStackDescription, size_t aIndex) - : mFilename(nullptr), - mFunname(nullptr), - mLineno(0) + : mLineno(0) { if (aStackDescription && aIndex < aStackDescription->NumFrames()) { mStackDescription = aStackDescription; @@ -340,12 +337,6 @@ JSStackFrame::JSStackFrame(StackDescriptionOwner* aStackDescription, JSStackFrame::~JSStackFrame() { - if (mFilename) { - nsMemory::Free(mFilename); - } - if (mFunname) { - nsMemory::Free(mFunname); - } } NS_IMPL_CYCLE_COLLECTION_2(JSStackFrame, mStackDescription, mCaller) @@ -366,22 +357,22 @@ NS_IMETHODIMP JSStackFrame::GetLanguage(uint32_t* aLanguage) } /* readonly attribute string languageName; */ -NS_IMETHODIMP JSStackFrame::GetLanguageName(char** aLanguageName) +NS_IMETHODIMP JSStackFrame::GetLanguageName(nsACString& aLanguageName) { static const char js[] = "JavaScript"; static const char cpp[] = "C++"; if (IsJSFrame()) { - *aLanguageName = (char*) nsMemory::Clone(js, sizeof(js)); + aLanguageName.AssignASCII(js); } else { - *aLanguageName = (char*) nsMemory::Clone(cpp, sizeof(cpp)); + aLanguageName.AssignASCII(cpp); } return NS_OK; } -const char* -JSStackFrame::GetFilename() +/* readonly attribute string filename; */ +NS_IMETHODIMP JSStackFrame::GetFilename(nsACString& aFilename) { if (!mFilenameInitialized) { JS::FrameDescription& desc = mStackDescription->FrameAt(mIndex); @@ -393,34 +384,22 @@ JSStackFrame::GetFilename() JSAutoCompartment ac(cx, desc.script()); const char* filename = JS_GetScriptFilename(cx, desc.script()); if (filename) { - mFilename = - (char*)nsMemory::Clone(filename, sizeof(char)*(strlen(filename)+1)); + mFilename.Assign(filename); } } mFilenameInitialized = true; } - return mFilename; -} - -/* readonly attribute string filename; */ -NS_IMETHODIMP JSStackFrame::GetFilename(char** aFilename) -{ - NS_ENSURE_ARG_POINTER(aFilename); - - const char* filename = GetFilename(); - if (filename) { - *aFilename = (char*) nsMemory::Clone(filename, - sizeof(char)*(strlen(filename)+1)); + if (mFilename.IsEmpty()) { + aFilename.SetIsVoid(true); } else { - *aFilename = nullptr; + aFilename.Assign(mFilename); } - return NS_OK; } -const char* -JSStackFrame::GetFunname() +/* readonly attribute string name; */ +NS_IMETHODIMP JSStackFrame::GetName(nsACString& aFunction) { if (!mFunnameInitialized) { JS::FrameDescription& desc = mStackDescription->FrameAt(mIndex); @@ -433,33 +412,19 @@ JSStackFrame::GetFunname() if (funid) { size_t length = JS_GetStringEncodingLength(cx, funid); if (length != size_t(-1)) { - mFunname = static_cast(nsMemory::Alloc(length + 1)); - if (mFunname) { - JS_EncodeStringToBuffer(cx, funid, mFunname, length); - mFunname[length] = '\0'; - } + mFunname.SetLength(uint32_t(length)); + JS_EncodeStringToBuffer(cx, funid, mFunname.BeginWriting(), length); } } } mFunnameInitialized = true; } - return mFunname; -} - -/* readonly attribute string name; */ -NS_IMETHODIMP JSStackFrame::GetName(char** aFunction) -{ - NS_ENSURE_ARG_POINTER(aFunction); - - const char* funname = GetFunname(); - if (funname) { - *aFunction = (char*) nsMemory::Clone(funname, - sizeof(char)*(strlen(funname)+1)); + if (mFunname.IsEmpty()) { + aFunction.SetIsVoid(true); } else { - *aFunction = nullptr; + aFunction.Assign(mFunname); } - return NS_OK; } @@ -482,10 +447,10 @@ NS_IMETHODIMP JSStackFrame::GetLineNumber(int32_t* aLineNumber) return NS_OK; } -/* readonly attribute string sourceLine; */ -NS_IMETHODIMP JSStackFrame::GetSourceLine(char** aSourceLine) +/* readonly attribute AUTF8String sourceLine; */ +NS_IMETHODIMP JSStackFrame::GetSourceLine(nsACString& aSourceLine) { - *aSourceLine = nullptr; + aSourceLine.Truncate(); return NS_OK; } @@ -500,26 +465,31 @@ NS_IMETHODIMP JSStackFrame::GetCaller(nsIStackFrame** aCaller) return NS_OK; } -/* string toString (); */ -NS_IMETHODIMP JSStackFrame::ToString(char** _retval) +/* AUTF8String toString (); */ +NS_IMETHODIMP JSStackFrame::ToString(nsACString& _retval) { + _retval.Truncate(); + const char* frametype = IsJSFrame() ? "JS" : "native"; - const char* filename = GetFilename(); - if (!filename) { - filename = ""; + + nsCString filename; + nsresult rv = GetFilename(filename); + NS_ENSURE_SUCCESS(rv, rv); + + if (filename.IsEmpty()) { + filename.AssignASCII(""); } - const char* funname = GetFunname(); - if (!funname) { - funname = ""; + + nsCString funname; + rv = GetName(funname); + NS_ENSURE_SUCCESS(rv, rv); + + if (funname.IsEmpty()) { + funname.AssignASCII(""); } static const char format[] = "%s frame :: %s :: %s :: line %d"; - int len = sizeof(char)* - (strlen(frametype) + strlen(filename) + strlen(funname)) + - sizeof(format) + 3 * sizeof(mLineno); - - char* buf = (char*) nsMemory::Alloc(len); - JS_snprintf(buf, len, format, frametype, filename, funname, GetLineno()); - *_retval = buf; + _retval.AppendPrintf(format, frametype, filename.get(), + funname.get(), GetLineno()); return NS_OK; } @@ -550,17 +520,8 @@ JSStackFrame::CreateStackFrameLocation(uint32_t aLanguage, self->mLanguage = aLanguage; self->mLineno = aLineNumber; - - if (aFilename) { - self->mFilename = - (char*)nsMemory::Clone(aFilename, sizeof(char)*(strlen(aFilename)+1)); - } - - if (aFunctionName) { - self->mFunname = - (char*)nsMemory::Clone(aFunctionName, - sizeof(char)*(strlen(aFunctionName)+1)); - } + self->mFilename = aFilename; + self->mFunname = aFunctionName; self->mCaller = aCaller; diff --git a/js/xpconnect/idl/xpccomponents.idl b/js/xpconnect/idl/xpccomponents.idl index 6f39ceed02e..0488db37cf8 100644 --- a/js/xpconnect/idl/xpccomponents.idl +++ b/js/xpconnect/idl/xpccomponents.idl @@ -153,7 +153,7 @@ interface nsIXPCComponents_Utils : nsISupports [implicit_jscontext,optional_argc] jsval evalInSandbox(in AString source, in jsval sandbox, [optional] in jsval version, - [optional] in jsval filename, + [optional] in AUTF8String filename, [optional] in long lineNo); /* diff --git a/js/xpconnect/idl/xpcexception.idl b/js/xpconnect/idl/xpcexception.idl index 79db29aced9..026766a4c88 100644 --- a/js/xpconnect/idl/xpcexception.idl +++ b/js/xpconnect/idl/xpcexception.idl @@ -12,9 +12,9 @@ interface nsIXPCException : nsIException { // inherits methods from nsIException - void initialize(in string aMessage, + void initialize(in AUTF8String aMessage, in nsresult aResult, - in string aName, + in AUTF8String aName, in nsIStackFrame aLocation, in nsISupports aData, in nsIException aInner); diff --git a/js/xpconnect/src/Sandbox.cpp b/js/xpconnect/src/Sandbox.cpp index 9b7a7fbb966..7404415cbd3 100644 --- a/js/xpconnect/src/Sandbox.cpp +++ b/js/xpconnect/src/Sandbox.cpp @@ -1515,7 +1515,7 @@ AssembleSandboxMemoryReporterName(JSContext *cx, nsCString &sandboxName) if (frame) { nsCString location; int32_t lineNumber = 0; - frame->GetFilename(getter_Copies(location)); + frame->GetFilename(location); frame->GetLineNumber(&lineNumber); sandboxName.AppendLiteral(" (from: "); @@ -1634,7 +1634,7 @@ ContextHolder::~ContextHolder() nsresult xpc::EvalInSandbox(JSContext *cx, HandleObject sandboxArg, const nsAString& source, - const char *filename, int32_t lineNo, + const nsACString& filename, int32_t lineNo, JSVersion jsVersion, bool returnStringOnly, MutableHandleValue rval) { JS_AbortIfWrongThread(JS_GetRuntime(cx)); @@ -1653,10 +1653,11 @@ xpc::EvalInSandbox(JSContext *cx, HandleObject sandboxArg, const nsAString& sour NS_ENSURE_TRUE(prin, NS_ERROR_FAILURE); nsAutoCString filenameBuf; - if (!filename) { + if (!filename.IsVoid()) { + filenameBuf.Assign(filename); + } else { // Default to the spec of the principal. nsJSPrincipals::get(prin)->GetScriptLocation(filenameBuf); - filename = filenameBuf.get(); lineNo = 1; } @@ -1680,7 +1681,7 @@ xpc::EvalInSandbox(JSContext *cx, HandleObject sandboxArg, const nsAString& sour JS::CompileOptions options(sandcx); options.setPrincipals(nsJSPrincipals::get(prin)) - .setFileAndLine(filename, lineNo); + .setFileAndLine(filenameBuf.get(), lineNo); if (jsVersion != JSVERSION_DEFAULT) options.setVersion(jsVersion); JS::RootedObject rootedSandbox(sandcx, sandbox); diff --git a/js/xpconnect/src/XPCComponents.cpp b/js/xpconnect/src/XPCComponents.cpp index efb6a50f548..c0148208c90 100644 --- a/js/xpconnect/src/XPCComponents.cpp +++ b/js/xpconnect/src/XPCComponents.cpp @@ -1861,8 +1861,10 @@ nsXPCComponents_Exception::CallOrConstruct(nsIXPConnectWrappedNative *wrapper, if (!parser.parse(args)) return ThrowAndFail(NS_ERROR_XPC_BAD_CONVERT_JS, cx, _retval); - nsCOMPtr e = new Exception(parser.eMsg, parser.eResult, - nullptr, parser.eStack, + nsCOMPtr e = new Exception(nsCString(parser.eMsg), + parser.eResult, + EmptyCString(), + parser.eStack, parser.eData); nsCOMPtr holder; @@ -2582,10 +2584,10 @@ nsXPCComponents_Utils::ReportError(HandleValue error, JSContext *cx) nsXPConnect *xpc = nsXPConnect::XPConnect(); xpc->GetCurrentJSStack(getter_AddRefs(frame)); - nsXPIDLCString fileName; + nsCString fileName; int32_t lineNo = 0; if (frame) { - frame->GetFilename(getter_Copies(fileName)); + frame->GetFilename(fileName); frame->GetLineNumber(&lineNo); } @@ -2595,8 +2597,8 @@ nsXPCComponents_Utils::ReportError(HandleValue error, JSContext *cx) nsresult rv = scripterr->InitWithWindowID( nsDependentString(static_cast(msgchars)), - NS_ConvertUTF8toUTF16(fileName), - EmptyString(), lineNo, 0, 0, "XPConnect JavaScript", innerWindowID); + NS_ConvertUTF8toUTF16(fileName), EmptyString(), lineNo, 0, 0, + "XPConnect JavaScript", innerWindowID); NS_ENSURE_SUCCESS(rv, NS_OK); console->LogMessage(scripterr); @@ -2608,7 +2610,7 @@ NS_IMETHODIMP nsXPCComponents_Utils::EvalInSandbox(const nsAString& source, HandleValue sandboxVal, HandleValue version, - HandleValue filenameVal, + const nsACString& filenameArg, int32_t lineNumber, JSContext *cx, uint8_t optionalArgc, @@ -2642,17 +2644,10 @@ nsXPCComponents_Utils::EvalInSandbox(const nsAString& source, } // Optional fourth and fifth arguments: filename and line number. - nsXPIDLCString filename; int32_t lineNo = (optionalArgc >= 3) ? lineNumber : 1; - if (optionalArgc >= 2) { - JSString *filenameStr = ToString(cx, filenameVal); - if (!filenameStr) - return NS_ERROR_INVALID_ARG; - - JSAutoByteString filenameBytes; - if (!filenameBytes.encodeLatin1(cx, filenameStr)) - return NS_ERROR_INVALID_ARG; - filename = filenameBytes.ptr(); + nsCString filename; + if (!filenameArg.IsVoid()) { + filename.Assign(filenameArg); } else { // Get the current source info from xpc. nsresult rv; @@ -2662,12 +2657,12 @@ nsXPCComponents_Utils::EvalInSandbox(const nsAString& source, nsCOMPtr frame; xpc->GetCurrentJSStack(getter_AddRefs(frame)); if (frame) { - frame->GetFilename(getter_Copies(filename)); + frame->GetFilename(filename); frame->GetLineNumber(&lineNo); } } - return xpc::EvalInSandbox(cx, sandbox, source, filename.get(), lineNo, + return xpc::EvalInSandbox(cx, sandbox, source, filename, lineNo, jsVersion, false, retval); } diff --git a/js/xpconnect/src/XPCConvert.cpp b/js/xpconnect/src/XPCConvert.cpp index c8b6c294e30..ea97c37e4f5 100644 --- a/js/xpconnect/src/XPCConvert.cpp +++ b/js/xpconnect/src/XPCConvert.cpp @@ -1070,7 +1070,6 @@ XPCConvert::ConstructException(nsresult rv, const char* message, static const char format[] = "\'%s\' when calling method: [%s::%s]"; const char * msg = message; - char* sz = nullptr; nsXPIDLString xmsg; nsAutoCString sxmsg; @@ -1084,19 +1083,18 @@ XPCConvert::ConstructException(nsresult rv, const char* message, if (!msg) if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &msg) || ! msg) msg = ""; - if (ifaceName && methodName) - msg = sz = JS_smprintf(format, msg, ifaceName, methodName); - nsRefPtr e = new Exception(msg, rv, nullptr, nullptr, data); + nsCString msgStr(msg); + if (ifaceName && methodName) + msgStr.AppendPrintf(format, msg, ifaceName, methodName); + + nsRefPtr e = new Exception(msgStr, rv, EmptyCString(), nullptr, data); if (cx && jsExceptionPtr) { e->StowJSVal(*jsExceptionPtr); } e.forget(exceptn); - - if (sz) - JS_smprintf_free(sz); return NS_OK; } diff --git a/js/xpconnect/src/XPCWrappedJSClass.cpp b/js/xpconnect/src/XPCWrappedJSClass.cpp index 2cd7150cd3f..a336fb2244a 100644 --- a/js/xpconnect/src/XPCWrappedJSClass.cpp +++ b/js/xpconnect/src/XPCWrappedJSClass.cpp @@ -899,11 +899,11 @@ nsXPCWrappedJSClass::CheckForException(XPCCallContext & ccx, fputs(line, stdout); fputs(preamble, stdout); - char* text; - if (NS_SUCCEEDED(xpc_exception->ToString(&text)) && text) { - fputs(text, stdout); + nsCString text; + if (NS_SUCCEEDED(xpc_exception->ToString(text)) && + !text.IsEmpty()) { + fputs(text.get(), stdout); fputs("\n", stdout); - nsMemory::Free(text); } else fputs(cant_get_text, stdout); fputs(line, stdout); @@ -926,17 +926,13 @@ nsXPCWrappedJSClass::CheckForException(XPCCallContext & ccx, // try to cook one up. scriptError = do_CreateInstance(XPC_SCRIPT_ERROR_CONTRACTID); if (nullptr != scriptError) { - char* exn_string; - rv = xpc_exception->ToString(&exn_string); + nsCString newMessage; + rv = xpc_exception->ToString(newMessage); if (NS_SUCCEEDED(rv)) { - // use toString on the exception as the message - NS_ConvertASCIItoUTF16 newMessage(exn_string); - nsMemory::Free((void *) exn_string); - // try to get filename, lineno from the first // stack frame location. int32_t lineNumber = 0; - nsXPIDLCString sourceName; + nsCString sourceName; nsCOMPtr location; xpc_exception-> @@ -946,11 +942,11 @@ nsXPCWrappedJSClass::CheckForException(XPCCallContext & ccx, location->GetLineNumber(&lineNumber); // get a filename. - rv = location->GetFilename(getter_Copies(sourceName)); + rv = location->GetFilename(sourceName); } - rv = scriptError->InitWithWindowID(newMessage, - NS_ConvertASCIItoUTF16(sourceName), + rv = scriptError->InitWithWindowID(NS_ConvertUTF8toUTF16(newMessage), + NS_ConvertUTF8toUTF16(sourceName), EmptyString(), lineNumber, 0, 0, "XPConnect JavaScript", diff --git a/js/xpconnect/src/nsXPConnect.cpp b/js/xpconnect/src/nsXPConnect.cpp index ad911f19a3f..19137da2caa 100644 --- a/js/xpconnect/src/nsXPConnect.cpp +++ b/js/xpconnect/src/nsXPConnect.cpp @@ -898,9 +898,14 @@ nsXPConnect::EvalInSandboxObject(const nsAString& source, const char *filename, return NS_ERROR_INVALID_ARG; RootedObject sandbox(cx, sandboxArg); - return EvalInSandbox(cx, sandbox, source, filename ? filename : - "x-bogus://XPConnect/Sandbox", 1, JSVERSION_DEFAULT, - returnStringOnly, rval); + nsCString filenameStr; + if (filename) { + filenameStr.Assign(filename); + } else { + filenameStr = NS_LITERAL_CSTRING("x-bogus://XPConnect/Sandbox"); + } + return EvalInSandbox(cx, sandbox, source, filenameStr, 1, + JSVERSION_DEFAULT, returnStringOnly, rval); } /* nsIXPConnectJSObjectHolder getWrappedNativePrototype (in JSContextPtr aJSContext, in JSObjectPtr aScope, in nsIClassInfo aClassInfo); */ diff --git a/js/xpconnect/src/xpcprivate.h b/js/xpconnect/src/xpcprivate.h index 4021f49a75c..fc70554855d 100644 --- a/js/xpconnect/src/xpcprivate.h +++ b/js/xpconnect/src/xpcprivate.h @@ -3527,7 +3527,7 @@ CreateSandboxObject(JSContext *cx, JS::MutableHandleValue vp, nsISupports *prinO // result, and cx->exception will be empty. nsresult EvalInSandbox(JSContext *cx, JS::HandleObject sandbox, const nsAString& source, - const char *filename, int32_t lineNo, + const nsACString& filename, int32_t lineNo, JSVersion jsVersion, bool returnStringOnly, JS::MutableHandleValue rval); diff --git a/xpcom/base/nsIException.idl b/xpcom/base/nsIException.idl index 77b760f4713..ef7a808cb07 100644 --- a/xpcom/base/nsIException.idl +++ b/xpcom/base/nsIException.idl @@ -10,35 +10,31 @@ #include "nsISupports.idl" -// XXX - most "string"s in this file should probably move to Unicode -// so may as well use AStrings... - - [scriptable, uuid(91d82105-7c62-4f8b-9779-154277c0ee90)] interface nsIStackFrame : nsISupports { // see nsIProgrammingLanguage for list of language consts readonly attribute uint32_t language; - readonly attribute string languageName; - readonly attribute string filename; - readonly attribute string name; + readonly attribute AUTF8String languageName; + readonly attribute AUTF8String filename; + readonly attribute AUTF8String name; // Valid line numbers begin at '1'. '0' indicates unknown. readonly attribute int32_t lineNumber; - readonly attribute string sourceLine; + readonly attribute AUTF8String sourceLine; readonly attribute nsIStackFrame caller; - string toString(); + AUTF8String toString(); }; [scriptable, uuid(F3A8D3B4-C424-4edc-8BF6-8974C983BA78)] interface nsIException : nsISupports { // A custom message set by the thrower. - [binaryname(MessageMoz)] readonly attribute string message; + [binaryname(MessageMoz)] readonly attribute AUTF8String message; // The nsresult associated with this exception. readonly attribute nsresult result; // The name of the error code (ie, a string repr of |result|) - readonly attribute string name; + readonly attribute AUTF8String name; // Filename location. This is the location that caused the // error, which may or may not be a source file location. @@ -48,7 +44,7 @@ interface nsIException : nsISupports // etc. // null indicates "no data" - readonly attribute string filename; + readonly attribute AUTF8String filename; // Valid line numbers begin at '1'. '0' indicates unknown. readonly attribute uint32_t lineNumber; // Valid column numbers begin at 0. @@ -64,5 +60,5 @@ interface nsIException : nsISupports readonly attribute nsISupports data; // A generic formatter - make it suitable to print, etc. - string toString(); + AUTF8String toString(); };