Bug 935203: Don't leak ScriptSources if we error out before a ScriptSourceObject can take ownership of them. r=djvj
authorJim Blandy <jimb@mozilla.com>
Mon, 24 Feb 2014 15:31:47 -0800
changeset 170641 75147b26aef99ed7a89d1213ea632097ce4f0a2f
parent 170640 8c594f255751b360f2b372f2190b21a119e3db88
child 170642 9d1d368fdc10e65e8e151843a7804bb2056172bf
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersdjvj
bugs935203
milestone30.0a1
Bug 935203: Don't leak ScriptSources if we error out before a ScriptSourceObject can take ownership of them. r=djvj
js/src/frontend/BytecodeCompiler.cpp
js/src/jsscript.cpp
js/src/vm/GlobalObject.cpp
--- a/js/src/frontend/BytecodeCompiler.cpp
+++ b/js/src/frontend/BytecodeCompiler.cpp
@@ -165,16 +165,17 @@ frontend::MaybeCallSourceHandler(JSConte
 }
 
 ScriptSourceObject *
 frontend::CreateScriptSourceObject(ExclusiveContext *cx, const ReadOnlyCompileOptions &options)
 {
     ScriptSource *ss = cx->new_<ScriptSource>(options.originPrincipals());
     if (!ss)
         return nullptr;
+    ScriptSourceHolder ssHolder(ss);
 
     if (options.hasIntroductionInfo) {
         const char *filename = options.filename() ? options.filename() : "<unknown>";
         JS_ASSERT(options.introductionType != nullptr);
 
         if (!ss->setIntroducedFilename(cx, filename, options.introductionLineno,
                                        options.introductionType, options.introducerFilename()))
             return nullptr;
--- a/js/src/jsscript.cpp
+++ b/js/src/jsscript.cpp
@@ -673,16 +673,18 @@ js::XDRScript(XDRState<mode> *xdr, Handl
         options.setVersion(version_)
                .setNoScriptRval(!!(scriptBits & (1 << NoScriptRval)))
                .setSelfHostingMode(!!(scriptBits & (1 << SelfHosted)));
         RootedScriptSource sourceObject(cx);
         if (scriptBits & (1 << OwnSource)) {
             ScriptSource *ss = cx->new_<ScriptSource>(xdr->originPrincipals());
             if (!ss)
                 return false;
+            ScriptSourceHolder ssHolder(ss);
+
             /*
              * We use this CompileOptions only to initialize the
              * ScriptSourceObject. Most CompileOptions fields aren't used by
              * ScriptSourceObject, and those that are (element; elementAttributeName)
              * aren't preserved by XDR. So this can be simple.
              */
             CompileOptions options(cx);
             sourceObject = ScriptSourceObject::create(cx, ss, options);
--- a/js/src/vm/GlobalObject.cpp
+++ b/js/src/vm/GlobalObject.cpp
@@ -248,16 +248,17 @@ GlobalObject::initFunctionAndObjectClass
         if (!source)
             return nullptr;
         ScriptSource *ss =
             cx->new_<ScriptSource>(/* originPrincipals = */ (JSPrincipals*)nullptr);
         if (!ss) {
             js_free(source);
             return nullptr;
         }
+        ScriptSourceHolder ssHolder(ss);
         ss->setSource(source, sourceLen);
         CompileOptions options(cx);
         options.setNoScriptRval(true)
                .setVersion(JSVERSION_DEFAULT);
         RootedScriptSource sourceObject(cx, ScriptSourceObject::create(cx, ss, options));
         if (!sourceObject)
             return nullptr;