From b3d00ca35bf15a2f0147cdcfd039d54b82f94545 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Fri, 17 Mar 2017 16:53:04 -0700 Subject: [PATCH] Bug 1333990: Follow-up: Use safer conversion functions when creating error message JS strings. r=bz MozReview-Commit-ID: FimoWFIgUxL --HG-- extra : rebase_source : 7b9193ac1c12dc96b269ee3dea167edc4051669f --- js/xpconnect/loader/ChromeScriptLoader.cpp | 13 ++++++++----- js/xpconnect/loader/mozJSSubScriptLoader.cpp | 20 ++++++++++++-------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/js/xpconnect/loader/ChromeScriptLoader.cpp b/js/xpconnect/loader/ChromeScriptLoader.cpp index 5bc4648c3a6e..553e042d4f01 100644 --- a/js/xpconnect/loader/ChromeScriptLoader.cpp +++ b/js/xpconnect/loader/ChromeScriptLoader.cpp @@ -195,12 +195,15 @@ AsyncScriptCompiler::Reject(JSContext* aCx) void AsyncScriptCompiler::Reject(JSContext* aCx, const char* aMsg) { - nsAutoCString msg(aMsg); - msg.Append(": "); - msg.Append(mURL); + nsAutoString msg; + msg.AppendASCII(aMsg); + msg.AppendLiteral(": "); + AppendUTF8toUTF16(mURL, msg); - RootedValue exn(aCx, StringValue(JS_NewStringCopyZ(aCx, msg.get()))); - JS_SetPendingException(aCx, exn); + RootedValue exn(aCx); + if (xpc::NonVoidStringToJsval(aCx, msg, &exn)) { + JS_SetPendingException(aCx, exn); + } Reject(aCx); } diff --git a/js/xpconnect/loader/mozJSSubScriptLoader.cpp b/js/xpconnect/loader/mozJSSubScriptLoader.cpp index e022818b0596..574f80ca61db 100644 --- a/js/xpconnect/loader/mozJSSubScriptLoader.cpp +++ b/js/xpconnect/loader/mozJSSubScriptLoader.cpp @@ -92,17 +92,21 @@ mozJSSubScriptLoader::~mozJSSubScriptLoader() NS_IMPL_ISUPPORTS(mozJSSubScriptLoader, mozIJSSubScriptLoader) static void -ReportError(JSContext* cx, const char* msg) +ReportError(JSContext* cx, const nsACString& msg) { - RootedValue exn(cx, JS::StringValue(JS_NewStringCopyZ(cx, msg))); - JS_SetPendingException(cx, exn); + NS_ConvertUTF8toUTF16 ucMsg(msg); + + RootedValue exn(cx); + if (xpc::NonVoidStringToJsval(cx, ucMsg, &exn)) { + JS_SetPendingException(cx, exn); + } } static void ReportError(JSContext* cx, const char* origMsg, nsIURI* uri) { if (!uri) { - ReportError(cx, origMsg); + ReportError(cx, nsDependentCString(origMsg)); return; } @@ -114,7 +118,7 @@ ReportError(JSContext* cx, const char* origMsg, nsIURI* uri) nsAutoCString msg(origMsg); msg.Append(": "); msg.Append(spec); - ReportError(cx, msg.get()); + ReportError(cx, msg); } bool @@ -620,7 +624,7 @@ mozJSSubScriptLoader::DoLoadSubScriptWithOptions(const nsAString& url, : nullptr; nsCOMPtr serv = do_GetService(NS_IOSERVICE_CONTRACTID); if (!serv) { - ReportError(cx, LOAD_ERROR_NOSERVICE); + ReportError(cx, NS_LITERAL_CSTRING(LOAD_ERROR_NOSERVICE)); return NS_OK; } @@ -628,13 +632,13 @@ mozJSSubScriptLoader::DoLoadSubScriptWithOptions(const nsAString& url, // canonicalized spec. rv = NS_NewURI(getter_AddRefs(uri), NS_LossyConvertUTF16toASCII(url).get(), nullptr, serv); if (NS_FAILED(rv)) { - ReportError(cx, LOAD_ERROR_NOURI); + ReportError(cx, NS_LITERAL_CSTRING(LOAD_ERROR_NOURI)); return NS_OK; } rv = uri->GetSpec(uriStr); if (NS_FAILED(rv)) { - ReportError(cx, LOAD_ERROR_NOSPEC); + ReportError(cx, NS_LITERAL_CSTRING(LOAD_ERROR_NOSPEC)); return NS_OK; }