Bug 1333990: Follow-up: Use safer conversion functions when creating error message JS strings.
MozReview-Commit-ID: FimoWFIgUxL
--- 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;
}