Bug 1364056 - don't try to use `doc` reference when it might be dead, r?evanxd draft
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Mon, 15 May 2017 22:01:40 +0100
changeset 578074 57d32fa11c940223323f777d6545430b8f5f9f31
parent 577554 e66dedabe582ba7b394aee4f89ed70fe389b3c46
child 628678 9079c3b575dd564eb1ba50166ce535fdbbf4c2a7
push id58881
push usergijskruitbosch@gmail.com
push dateMon, 15 May 2017 21:02:18 +0000
reviewersevanxd
bugs1364056
milestone55.0a1
Bug 1364056 - don't try to use `doc` reference when it might be dead, r?evanxd MozReview-Commit-ID: Lt7aOU8hPSV
toolkit/components/reader/ReaderMode.jsm
--- a/toolkit/components/reader/ReaderMode.jsm
+++ b/toolkit/components/reader/ReaderMode.jsm
@@ -440,16 +440,20 @@ this.ReaderMode = {
       let numTags = doc.getElementsByTagName("*").length;
       if (numTags > this.parseNodeLimit) {
         this.log("Aborting parse for " + doc.baseURIObject.spec + "; " + numTags + " elements found");
         histogram.add(PARSE_ERROR_TOO_MANY_ELEMENTS);
         return null;
       }
     }
 
+    // Fetch this here before we send `doc` off to the worker thread, as later on the
+    // document might be nuked but we will still want the URI.
+    let {documentURI} = doc;
+
     let uriParam = {
       spec: doc.baseURIObject.spec,
       host: doc.baseURIObject.host,
       prePath: doc.baseURIObject.prePath,
       scheme: doc.baseURIObject.scheme,
       pathBase: Services.io.newURI(".", null, doc.baseURIObject).spec
     };
 
@@ -460,26 +464,30 @@ this.ReaderMode = {
     let article = null;
     try {
       article = await ReaderWorker.post("parseDocument", [uriParam, serializedDoc]);
     } catch (e) {
       Cu.reportError("Error in ReaderWorker: " + e);
       histogram.add(PARSE_ERROR_WORKER);
     }
 
+    // Explicitly null out doc to make it clear it might not be available from this
+    // point on.
+    doc = null;
+
     if (!article) {
       this.log("Worker did not return an article");
       histogram.add(PARSE_ERROR_NO_ARTICLE);
       return null;
     }
 
     // Readability returns a URI object based on the baseURI, but we only care
     // about the original document's URL from now on. This also avoids spoofing
     // attempts where the baseURI doesn't match the domain of the documentURI
-    article.url = doc.documentURI;
+    article.url = documentURI;
     delete article.uri;
 
     let flags = Ci.nsIDocumentEncoder.OutputSelectionOnly | Ci.nsIDocumentEncoder.OutputAbsoluteLinks;
     article.title = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils)
                                                     .convertToPlainText(article.title, flags, 0);
     if (gIsFirefoxDesktop) {
       await this._assignLanguage(article);
       this._maybeAssignTextDirection(article);