Bug 857765 - Add try/catch to content-script window.onerror call. r=abr
authorJan-Ivar Bruaroey <jib@mozilla.com>
Wed, 17 Apr 2013 19:03:39 -0400
changeset 129222 9dd64ef21eff04910856d1d287b74700d8363369
parent 129221 43987922a6b34b8ba4ab3c361a2a5f2bdd2c5e2f
child 129223 a520eb3b34ae63cde7a4ad99c3d9ed66257a5ff2
push id24562
push userryanvm@gmail.com
push dateFri, 19 Apr 2013 01:24:04 +0000
treeherdermozilla-central@f8d27fe5d7c0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersabr
bugs857765
milestone23.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 857765 - Add try/catch to content-script window.onerror call. r=abr
dom/media/PeerConnection.js
dom/media/tests/mochitest/test_peerConnection_throwInCallbacks.html
--- a/dom/media/PeerConnection.js
+++ b/dom/media/PeerConnection.js
@@ -802,28 +802,42 @@ PeerConnectionObserver.prototype = {
         //    do the translation by extracting file and line from XPCOM stack:
         //
         // e.location ~= "JS frame :: file://.js :: RTCPCCb::onCallback :: line 1"
 
         let stack = e.location.toString().split(" :: ");
         let file = stack[1];
         let line = parseInt(stack[3].split(" ")[1]);
 
-        let scriptError = Cc["@mozilla.org/scripterror;1"].
-            createInstance(Ci.nsIScriptError);
+        let scriptErrorClass = Cc["@mozilla.org/scripterror;1"];
+        let scriptError = scriptErrorClass.createInstance(Ci.nsIScriptError);
         scriptError.initWithWindowID(msg, file, null, line, 0,
                                      Ci.nsIScriptError.exceptionFlag,
                                      "content javascript",
                                      this._dompc._winID);
-        Cc["@mozilla.org/consoleservice;1"].
-            getService(Ci.nsIConsoleService).logMessage(scriptError);
+        let console = Cc["@mozilla.org/consoleservice;1"].
+            getService(Ci.nsIConsoleService);
+        console.logMessage(scriptError);
 
-        // Call onerror directly if present (necessary for testing)
-        if (typeof this._dompc._win.onerror === "function") {
-          this._dompc._win.onerror(msg, file, line);
+        // Safely call onerror directly if present (necessary for testing)
+        try {
+          if (typeof this._dompc._win.onerror === "function") {
+            this._dompc._win.onerror(msg, file, line);
+          }
+        } catch(e) {
+          // If onerror itself throws, service it.
+          try {
+            let scriptError = scriptErrorClass.createInstance(Ci.nsIScriptError);
+            scriptError.initWithWindowID(e.message, e.fileName, null,
+                                         e.lineNumber, 0,
+                                         Ci.nsIScriptError.exceptionFlag,
+                                         "content javascript",
+                                         this._dompc._winID);
+            console.logMessage(scriptError);
+          } catch(e) {}
         }
       }
     }
   },
 
   onCreateOfferSuccess: function(offer) {
     this.callCB(this._dompc._onCreateOfferSuccess,
                 { type: "offer", sdp: offer,
--- a/dom/media/tests/mochitest/test_peerConnection_throwInCallbacks.html
+++ b/dom/media/tests/mochitest/test_peerConnection_throwInCallbacks.html
@@ -20,16 +20,17 @@
     if (errorMsg.indexOf("Expected") == -1) {
       getFail(new Error)(errorMsg);
     }
     error_count += 1;
     info("onerror " + error_count + ": " + errorMsg);
     if (error_count == 7) {
       finish();
     }
+    throw new Error("window.onerror may throw");
     return false;
   }
 
   let pc0, pc1, pc2;
 
   runTest(function () {
     error_count = 0;
 
@@ -62,17 +63,17 @@
         });
       }, getFail(new Error));
       throw new Error("Expected");
     });
   }, 1);
 
   function finish() {
     window.onerror = oldOnError;
-    ok(error_count == 7, "Seven expected errors verified.");
+    is(error_count, 7, "Seven expected errors verified.");
     SimpleTest.finish();
   }
 
   function getFail(where) {
     return function (err) {
       window.onerror = onOldError;
       unexpectedCallbackAndFinish(where)(err);
     };