Bug 1619953 - P7 - mozJSSubScriptLoader::ReadScript() now attempts reading from input stream even if content length is not already known r=baku,#dom-workers-and-storage draft
authorssengupta <ssengupta@mozilla.com>
Wed, 01 Jul 2020 13:08:02 +0000
changeset 3016304 f51a8dd1a978674ab2295d05b30aa581ae816c89
parent 3016303 384f5bed95f041609ee98fed4cb23610c2b30711
child 3016305 f8091aa42ea4413508f70a4b0d4d9b48026c2944
push id562770
push userreviewbot
push dateWed, 01 Jul 2020 13:08:29 +0000
treeherdertry@e04720d62235 [default view] [failures only]
reviewersbaku
bugs1619953
milestone80.0a1
Bug 1619953 - P7 - mozJSSubScriptLoader::ReadScript() now attempts reading from input stream even if content length is not already known r=baku,#dom-workers-and-storage Summary: If reading succeeds, it is checked that the length is known from that point on (this check is debug only). Differential Revision: https://phabricator.services.mozilla.com/D81718 Depends on D81452 Test Plan: Reviewers: baku, #dom-workers-and-storage Subscribers: Bug #: 1619953 Differential Diff: PHID-DIFF-xk3lqbpteg56vskik7ok
js/xpconnect/loader/mozJSSubScriptLoader.cpp
--- a/js/xpconnect/loader/mozJSSubScriptLoader.cpp
+++ b/js/xpconnect/loader/mozJSSubScriptLoader.cpp
@@ -271,30 +271,45 @@ bool mozJSSubScriptLoader::ReadScript(JS
   if (NS_FAILED(rv)) {
     ReportError(cx, LOAD_ERROR_NOSTREAM, uri);
     return false;
   }
 
   int64_t len = -1;
 
   rv = chan->GetContentLength(&len);
-  if (NS_FAILED(rv) || len == -1) {
+  if (NS_FAILED(rv)) {
     ReportError(cx, LOAD_ERROR_NOCONTENT, uri);
     return false;
   }
 
   if (len > INT32_MAX) {
     ReportError(cx, LOAD_ERROR_CONTENTTOOBIG, uri);
     return false;
   }
 
   nsCString buf;
   rv = NS_ReadInputStreamToString(instream, buf, len);
   NS_ENSURE_SUCCESS(rv, false);
 
+  if (len < 0) {
+    len = buf.Length();
+  }
+
+#ifdef DEBUG
+  int64_t currentLength = -1;
+  // if getting content length succeeded above, it should not fail now
+  MOZ_ASSERT(chan->GetContentLength(&currentLength) == NS_OK);
+  // if content length was not known when GetContentLength() was called before,
+  // 'len' would be set to -1 until NS_ReadInputStreamToString() set its correct
+  // value. Every subsequent call to GetContentLength() should return the same
+  // length as that value.
+  MOZ_ASSERT(currentLength == len);
+#endif
+
   Maybe<JSAutoRealm> ar;
 
   // Note that when using the ScriptPreloader cache with loadSubScript, there
   // will be a side-effect of keeping the global that the script was compiled
   // for alive. See note above in EvalScript().
   //
   // This will compile the script in XPConnect compilation scope. When the
   // script is evaluated, it will be cloned into the target scope to be