Skip to content

Commit

Permalink
fix: crash on objects with keys shadowing hasOwnProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant committed Jan 26, 2024
1 parent d8c5dd1 commit c2c27f5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/collection-compare/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function compareObjects(value1, value2) {
for (var i = 0; i < len; i++) {
var key1 = keys1[i];

if (!(value2.hasOwnProperty(key1) && compare(value1[key1], value2[key1]))) {
if (!({}.hasOwnProperty.call(value2, key1) && compare(value1[key1], value2[key1]))) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/collection-compare/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function compareObjects(value1, value2) {
for (var i = 0; i < len; i++) {
var key1 = keys1[i];

if (!(value2.hasOwnProperty(key1) && compare(value1[key1], value2[key1]))) {
if (!({}.hasOwnProperty.call(value2, key1) && compare(value1[key1], value2[key1]))) {
return false;
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/collection-compare/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,14 @@ test('primitives compared with primitive wrappers return false', function(t) {
t.notOk(compare(value2, value1));
t.end();
});

test('objects with keys shadowing hasOwnProperty do not crash', function(t) {
t.plan(2);
var value1 = {hasOwnProperty: 1};
var value2 = {hasOwnProperty: 1};
t.ok(compare(value1, value2));
var value3 = {hasOwnProperty: 1};
var value4 = {hasOwnProperty: 2};
t.notOk(compare(value3, value4));
t.end();
});

0 comments on commit c2c27f5

Please sign in to comment.