Skip to content

Commit

Permalink
util,assert: fix boxed primitives bug
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: George Adams <[email protected]>
  • Loading branch information
BridgeAR authored and rvagg committed Aug 15, 2018
1 parent 2d1c185 commit f1c22ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

0 comments on commit f1c22ea

Please sign in to comment.