From 52fc48b3e9d92e62cfa717fb1569def10bdd19c3 Mon Sep 17 00:00:00 2001 From: Kris Shannon Date: Mon, 15 Feb 2016 03:58:29 +1100 Subject: [PATCH] Protect against cache lookup returning null If the cache file does not exist _cacheGetJson will not pass any value to the callback. In the getImage function the callback passed to _cacheGetJson assumes if there was no error value then the second argument must be an array. Add some logic to guard against null values before iterating over the array. --- lib/tritonapi.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/tritonapi.js b/lib/tritonapi.js index 3d25301..bd7cea7 100644 --- a/lib/tritonapi.js +++ b/lib/tritonapi.js @@ -354,10 +354,12 @@ TritonApi.prototype.getImage = function getImage(opts, cb) { next(err); return; } - for (var i = 0; i < images.length; i++) { - if (images[i].id === opts.name) { - img = images[i]; - break; + if (images) { + for (var i = 0; i < images.length; i++) { + if (images[i].id === opts.name) { + img = images[i]; + break; + } } } next();