From 213d513b990fa871ce0d731489a60c256d743480 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 1 Feb 2019 13:48:43 -0800 Subject: [PATCH] [Fix]` `utils.merge`: avoid a crash with a null target and a truthy non-array source --- lib/utils.js | 2 +- test/utils.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index e0ebba2d..32a83c75 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -30,7 +30,7 @@ exports.merge = function (target, source, options) { if (typeof source !== 'object') { if (Array.isArray(target)) { target.push(source); - } else if (typeof target === 'object') { + } else if (target && typeof target === 'object') { if ((options && (options.plainObjects || options.allowPrototypes)) || !has.call(Object.prototype, source)) { target[source] = true; } diff --git a/test/utils.js b/test/utils.js index 999f860d..67ef9369 100644 --- a/test/utils.js +++ b/test/utils.js @@ -4,6 +4,8 @@ var test = require('tape'); var utils = require('../lib/utils'); test('merge()', function (t) { + t.deepEqual(utils.merge(null, true), [null, true], 'merges true into null'); + t.deepEqual(utils.merge({ a: 'b' }, { a: 'c' }), { a: ['b', 'c'] }, 'merges two objects with the same key'); var oneMerged = utils.merge({ foo: 'bar' }, { foo: { first: '123' } });