Bug 1462958 - Avoid infinite recursion if resourceIds is empty. r?mossop draft
authorZibi Braniecki <zbraniecki@mozilla.com>
Sat, 19 May 2018 20:14:00 -0700
changeset 798282 2cc77ccb2ce9027d82b438c24bf66a2eabb821ef
parent 798281 d65ecc05c8f822855f92402d7ac6f238c31c1f4a
push id110692
push userbmo:gandalf@aviary.pl
push dateTue, 22 May 2018 14:55:11 +0000
reviewersmossop
bugs1462958
milestone62.0a1
Bug 1462958 - Avoid infinite recursion if resourceIds is empty. r?mossop `generateContextsForLocale` didn't have a condition for when `resolvedLength` equals `resourcesLength` which triggeres an infinite recursion when `resourceIds` is empty. MozReview-Commit-ID: Csy1ROTfbE2
intl/l10n/L10nRegistry.jsm
intl/l10n/test/test_l10nregistry.js
--- a/intl/l10n/L10nRegistry.jsm
+++ b/intl/l10n/L10nRegistry.jsm
@@ -206,17 +206,17 @@ async function* generateContextsForLocal
 
     // If the number of resolved sources equals the number of resources,
     // create the right context and return it if it loads.
     if (resolvedLength + 1 === resourcesLength) {
       const ctx = await generateContext(locale, order, resourceIds);
       if (ctx !== null) {
         yield ctx;
       }
-    } else {
+    } else if (resolvedLength < resourcesLength) {
       // otherwise recursively load another generator that walks over the
       // partially resolved list of sources.
       yield * generateContextsForLocale(locale, sourcesOrder, resourceIds, order);
     }
   }
 }
 
 const MSG_CONTEXT_OPTIONS = {
--- a/intl/l10n/test/test_l10nregistry.js
+++ b/intl/l10n/test/test_l10nregistry.js
@@ -19,16 +19,53 @@ L10nRegistry.load = async function(url) 
 add_task(function test_methods_presence() {
   equal(typeof L10nRegistry.generateContexts, "function");
   equal(typeof L10nRegistry.getAvailableLocales, "function");
   equal(typeof L10nRegistry.registerSource, "function");
   equal(typeof L10nRegistry.updateSource, "function");
 });
 
 /**
+ * Test that passing empty resourceIds list works.
+ */
+add_task(async function test_empty_resourceids() {
+  fs = {};
+
+  const source = new FileSource("test", ["en-US"], "/localization/{locale}");
+  L10nRegistry.registerSource(source);
+
+  const ctxs = L10nRegistry.generateContexts(["en-US"], []);
+
+  const done = (await ctxs.next()).done;
+
+  equal(done, true);
+
+  // cleanup
+  L10nRegistry.sources.clear();
+  L10nRegistry.ctxCache.clear();
+});
+
+/**
+ * Test that passing empty sources list works.
+ */
+add_task(async function test_empty_sources() {
+  fs = {};
+
+  const ctxs = L10nRegistry.generateContexts(["en-US"], []);
+
+  const done = (await ctxs.next()).done;
+
+  equal(done, true);
+
+  // cleanup
+  L10nRegistry.sources.clear();
+  L10nRegistry.ctxCache.clear();
+});
+
+/**
  * This test tests generation of a proper context for a single
  * source scenario
  */
 add_task(async function test_methods_calling() {
   fs = {
     "/localization/en-US/browser/menu.ftl": "key = Value",
   };