Skip to content

Commit f1c22ea

Browse files
BridgeARrvagg
authored andcommitted
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: #22243 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: George Adams <george.adams@uk.ibm.com>
1 parent 2d1c185 commit f1c22ea

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/internal/util/comparisons.js

+3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ function strictDeepEqual(val1, val2, memos) {
120120
const val1Value = val1.valueOf();
121121
// Note: Boxed string keys are going to be compared again by Object.keys
122122
if (val1Value !== val1) {
123+
if (typeof val2.valueOf !== 'function') {
124+
return false;
125+
}
123126
if (!innerDeepEqual(val1Value, val2.valueOf(), true))
124127
return false;
125128
// Fast path for boxed primitives

test/parallel/test-assert-deep.js

+7
Original file line numberDiff line numberDiff line change
@@ -931,3 +931,10 @@ assert.throws(() => assert.deepStrictEqual(new Boolean(true), {}),
931931
);
932932
util.inspect.defaultOptions = tmp;
933933
}
934+
935+
// Basic valueOf check.
936+
{
937+
const a = new String(1);
938+
a.valueOf = undefined;
939+
assertNotDeepOrStrict(a, new String(1));
940+
}

0 commit comments

Comments
 (0)