Skip to content

Commit c7ca199

Browse files
BridgeARGeorge Adams
authored and
George Adams
committed
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 0aae34f commit c7ca199

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
@@ -890,3 +890,10 @@ assert.deepStrictEqual(obj1, obj2);
890890
);
891891
util.inspect.defaultOptions = tmp;
892892
}
893+
894+
// Basic valueOf check.
895+
{
896+
const a = new String(1);
897+
a.valueOf = undefined;
898+
assertNotDeepOrStrict(a, new String(1));
899+
}

0 commit comments

Comments
 (0)