Bug 1333990: Follow-up: Use safer conversion functions when creating error message JS strings. draft
authorKris Maglione <maglione.k@gmail.com>
Fri, 17 Mar 2017 14:51:17 -0700
changeset 500884 6cb19ccf457e2d58347caa69f02af8a2b87de688
parent 500870 87195e1dfa56d7b34fed2432e2e4c96da2ac2905
child 500885 45e0fda3c15fc4fdc30c93d3d858819c640a2fc3
push id49830
push usermaglione.k@gmail.com
push dateFri, 17 Mar 2017 21:53:14 +0000
bugs1333990
milestone55.0a1
Bug 1333990: Follow-up: Use safer conversion functions when creating error message JS strings. MozReview-Commit-ID: FimoWFIgUxL
js/xpconnect/loader/ChromeScriptLoader.cpp
js/xpconnect/loader/mozJSSubScriptLoader.cpp
--- a/js/xpconnect/loader/ChromeScriptLoader.cpp
+++ b/js/xpconnect/loader/ChromeScriptLoader.cpp
@@ -190,22 +190,25 @@ AsyncScriptCompiler::Reject(JSContext* a
         JS_ClearPendingException(aCx);
     }
     mPromise->MaybeReject(aCx, value);
 }
 
 void
 AsyncScriptCompiler::Reject(JSContext* aCx, const char* aMsg)
 {
-    nsAutoCString msg(aMsg);
-    msg.Append(": ");
-    msg.Append(mURL);
+    nsAutoString msg;
+    msg.AppendASCII(aMsg);
+    msg.AppendLiteral(": ");
+    msg.Append(NS_ConvertUTF8toUTF16(mURL));
 
-    RootedValue exn(aCx, StringValue(JS_NewStringCopyZ(aCx, msg.get())));
-    JS_SetPendingException(aCx, exn);
+    RootedValue exn(aCx);
+    if (xpc::StringToJsval(aCx, msg, &exn)) {
+        JS_SetPendingException(aCx, exn);
+    }
 
     Reject(aCx);
 }
 
 NS_IMETHODIMP
 AsyncScriptCompiler::OnIncrementalData(nsIIncrementalStreamLoader* aLoader,
                                        nsISupports* aContext,
                                        uint32_t aDataLength,
--- a/js/xpconnect/loader/mozJSSubScriptLoader.cpp
+++ b/js/xpconnect/loader/mozJSSubScriptLoader.cpp
@@ -87,39 +87,43 @@ mozJSSubScriptLoader::mozJSSubScriptLoad
 mozJSSubScriptLoader::~mozJSSubScriptLoader()
 {
     /* empty */
 }
 
 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::StringToJsval(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;
     }
 
     nsAutoCString spec;
     nsresult rv = uri->GetSpec(spec);
     if (NS_FAILED(rv))
         spec.Assign("(unknown)");
 
     nsAutoCString msg(origMsg);
     msg.Append(": ");
     msg.Append(spec);
-    ReportError(cx, msg.get());
+    ReportError(cx, msg);
 }
 
 bool
 PrepareScript(nsIURI* uri,
               JSContext* cx,
               RootedObject& targetObj,
               const char* uriStr,
               const nsAString& charset,
@@ -615,31 +619,31 @@ mozJSSubScriptLoader::DoLoadSubScriptWit
     JSAutoCompartment ac(cx, targetObj);
 
     // Suppress caching if we're compiling as content.
     StartupCache* cache = (principal == mSystemPrincipal)
                           ? StartupCache::GetSingleton()
                           : nullptr;
     nsCOMPtr<nsIIOService> serv = do_GetService(NS_IOSERVICE_CONTRACTID);
     if (!serv) {
-        ReportError(cx, LOAD_ERROR_NOSERVICE);
+        ReportError(cx, NS_LITERAL_CSTRING(LOAD_ERROR_NOSERVICE));
         return NS_OK;
     }
 
     // Make sure to explicitly create the URI, since we'll need the
     // 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;
     }
 
     rv = uri->GetScheme(scheme);
     if (NS_FAILED(rv)) {
         ReportError(cx, LOAD_ERROR_NOSCHEME, uri);
         return NS_OK;
     }