From f1c22eaa56bddffcd2f5d3d1e4a174e9fd558812 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 3 Aug 2018 20:46:40 +0200 Subject: [PATCH] util,assert: fix boxed primitives bug Currently the comparison could throw an error in case a boxed primitive has no valueOf function on one side of the assert call. PR-URL: https://github.com/nodejs/node/pull/22243 Reviewed-By: Luigi Pinca Reviewed-By: Trivikram Kamat Reviewed-By: George Adams --- lib/internal/util/comparisons.js | 3 +++ test/parallel/test-assert-deep.js | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index 5b29ccf561c71b..0e58ea2cb4e024 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -120,6 +120,9 @@ function strictDeepEqual(val1, val2, memos) { const val1Value = val1.valueOf(); // Note: Boxed string keys are going to be compared again by Object.keys if (val1Value !== val1) { + if (typeof val2.valueOf !== 'function') { + return false; + } if (!innerDeepEqual(val1Value, val2.valueOf(), true)) return false; // Fast path for boxed primitives diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index 431106c99be2b6..326bbfd1e76ddf 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -931,3 +931,10 @@ assert.throws(() => assert.deepStrictEqual(new Boolean(true), {}), ); util.inspect.defaultOptions = tmp; } + +// Basic valueOf check. +{ + const a = new String(1); + a.valueOf = undefined; + assertNotDeepOrStrict(a, new String(1)); +}