Bug 1534652 - Add try catch whenever querying mozparams. r=adw
authorMichael Kaply <mozilla@kaply.com>
Tue, 02 Apr 2019 21:14:09 +0000
changeset 467704 46fbb035f33543550347c89b0d24882ccd2198cc
parent 467703 f7b70caea4a5361ab87459abacc65f34206a01f8
child 467705 9f64986f5b368cb3480c09e4e68f8864230f2135
push id35810
push useraciure@mozilla.com
push dateThu, 04 Apr 2019 04:33:36 +0000
treeherdermozilla-central@b72c02e34261 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw
bugs1534652
milestone68.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 1534652 - Add try catch whenever querying mozparams. r=adw Differential Revision: https://phabricator.services.mozilla.com/D23384
toolkit/components/search/SearchService.jsm
--- a/toolkit/components/search/SearchService.jsm
+++ b/toolkit/components/search/SearchService.jsm
@@ -794,24 +794,26 @@ function sanitizeName(aName) {
   if (name.length < minLength)
     name = Math.random().toString(36).replace(/^.*\./, "");
 
   // Force max length.
   return name.substring(0, maxLength);
 }
 
 /**
- * Retrieve a pref from the search param branch.
+ * Retrieve a pref from the search param branch. Returns null if the
+ * preference is not found.
  *
  * @param prefName
  *        The name of the pref.
  **/
 function getMozParamPref(prefName) {
   let branch = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF + "param.");
-  return encodeURIComponent(branch.getCharPref(prefName));
+  let prefValue = branch.getCharPref(prefName, null);
+  return prefValue ? encodeURIComponent(prefValue) : null;
 }
 
 /**
  * Notifies watchers of SEARCH_ENGINE_TOPIC about changes to an engine or to
  * the state of the search service.
  *
  * @param aEngine
  *        The nsISearchEngine object to which the change applies.
@@ -1073,17 +1075,19 @@ EngineURL.prototype = {
 
     this.rels = aJson.rels;
 
     for (let i = 0; i < aJson.params.length; ++i) {
       let param = aJson.params[i];
       if (param.mozparam) {
         if (param.condition == "pref") {
           let value = getMozParamPref(param.pref);
-          this.addParam(param.name, value);
+          if (value) {
+            this.addParam(param.name, value);
+          }
         }
         this._addMozParam(param);
       } else {
         this.addParam(param.name, param.value, param.purpose || undefined);
       }
     }
   },
 
@@ -1703,17 +1707,19 @@ Engine.prototype = {
 
     if (aParams.mozParams) {
       for (let p of aParams.mozParams) {
         if ((p.condition || p.purpose) && !this._isDefault) {
           continue;
         }
         if (p.condition == "pref") {
           let value = getMozParamPref(p.pref);
-          url.addParam(p.name, value);
+          if (value) {
+            url.addParam(p.name, value);
+          }
           url._addMozParam(p);
         } else {
           url.addParam(p.name, p.value, p.purpose || undefined);
         }
       }
     }
 
     this._urls.push(url);
@@ -1836,23 +1842,23 @@ Engine.prototype = {
           case "purpose":
             url.addParam(param.getAttribute("name"),
                          param.getAttribute("value"),
                          param.getAttribute("purpose"));
             // _addMozParam is not needed here since it can be serialized fine without. _addMozParam
             // also requires a unique "name" which is not normally the case when @purpose is used.
             break;
           case "pref":
-            try {
-              value = getMozParamPref(param.getAttribute("pref"), value);
+            value = getMozParamPref(param.getAttribute("pref"), value);
+            if (value) {
               url.addParam(param.getAttribute("name"), value);
-              url._addMozParam({"pref": param.getAttribute("pref"),
-                                "name": param.getAttribute("name"),
-                                "condition": "pref"});
-            } catch (e) { }
+            }
+            url._addMozParam({"pref": param.getAttribute("pref"),
+                              "name": param.getAttribute("name"),
+                              "condition": "pref"});
             break;
           default:
             let engineLoc = this._location;
             let paramName = param.getAttribute("name");
             LOG("_parseURL: MozParam (" + paramName + ") has an unknown condition: " +
               condition + ". Found parsing engine: " + engineLoc);
           break;
         }