mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
assert: fix TypeError on deepStrictEqual with null Map key or Set member
deepStrictEqual() and util.isDeepStrictEqual() threw "Cannot read properties of null (reading 'constructor')" instead of comparing when a Map key or Set member was null/undefined (or another primitive) and lined up against object-only keys/members in the other collection with an equal count. The primitive/null handling was gated behind an optimization that is skipped when the counts match, letting such keys reach objectComparisonStart, which dereferences `.constructor`. Resolve primitive and null keys/members directly in every case. Signed-off-by: semx <7532921+semx@users.noreply.github.com> PR-URL: https://github.com/nodejs/node/pull/64449 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
d03e7313cf
commit
2bf082453b
2 files changed
+24
-7
No files matched your search
@@ -683,16 +683,18 @@ function setObjectEquiv(array, a, b, mode, memo) {
|
||||
const comparator = mode !== kLoose ? objectComparisonStart : innerDeepEqual;
|
||||
const extraChecks = mode === kLoose || array.length !== a.size;
|
||||
for (const val1 of a) {
|
||||
if (extraChecks) {
|
||||
if (typeof val1 === 'object') {
|
||||
if (b.has(val1)) {
|
||||
continue;
|
||||
}
|
||||
} else if (b.has(val1)) {
|
||||
// Primitive and null members can only match by identity, and must never
|
||||
// reach objectComparisonStart (which throws on `val.constructor` for
|
||||
// null/undefined). Resolve them directly for every such member.
|
||||
if (typeof val1 !== 'object' || val1 === null) {
|
||||
if (b.has(val1)) {
|
||||
continue;
|
||||
} else if (mode !== kLoose) {
|
||||
}
|
||||
if (mode !== kLoose) {
|
||||
return false;
|
||||
}
|
||||
} else if (extraChecks && b.has(val1)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let innerStart = start;
|
||||
|
||||
@@ -278,6 +278,10 @@ test('es6 Maps and Sets', () => {
|
||||
assertDeepAndStrictEqual(new Set([[1, 2], [3, 4]]), new Set([[3, 4], [1, 2]]));
|
||||
assertNotDeepOrStrict(new Set([{ a: 0 }]), new Set([{ a: 1 }]));
|
||||
assertNotDeepOrStrict(new Set([Symbol()]), new Set([Symbol()]));
|
||||
// A null/primitive member lined up against object-only members in the other
|
||||
// set must report inequality, not throw on `member.constructor`.
|
||||
assertNotDeepOrStrict(new Set([null, {}, {}]), new Set([{}, {}, {}]));
|
||||
assertNotDeepOrStrict(new Set([undefined, {}, {}]), new Set([{}, {}, {}]));
|
||||
|
||||
{
|
||||
const a = [ 1, 2 ];
|
||||
@@ -298,6 +302,17 @@ test('es6 Maps and Sets', () => {
|
||||
new Map([[[1], 1], [{}, 2]]),
|
||||
new Map([[[1], 2], [{}, 1]])
|
||||
);
|
||||
// A null/primitive key that lines up with object-only keys in the other map
|
||||
// must report inequality, not throw on `key.constructor`. Refs: object keys
|
||||
// of `b` equal in count to `a.size` used to skip the primitive-key handling.
|
||||
assertNotDeepOrStrict(
|
||||
new Map([[null, 1], [{}, 2]]),
|
||||
new Map([[{}, 9], [{}, 9]])
|
||||
);
|
||||
assertNotDeepOrStrict(
|
||||
new Map([[undefined, 1], [{}, 2]]),
|
||||
new Map([[{}, 9], [{}, 9]])
|
||||
);
|
||||
|
||||
assertNotDeepOrStrict(new Set([1]), [1]);
|
||||
assertNotDeepOrStrict(new Set(), []);
|
||||
|
||||
Reference in new issue
Block a user