Skip to content

Commit

Permalink
fix: just-compare crash on some objects
Browse files Browse the repository at this point in the history
- objects with keys shadowing `hasOwnProperty`
- objects with no prototypes
  • Loading branch information
Sec-ant committed Jan 26, 2024
1 parent d8c5dd1 commit 6821948
Show file tree
Hide file tree
Showing 3 changed files with 23 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
21 changes: 21 additions & 0 deletions test/collection-compare/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,24 @@ 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: 2};
t.notOk(compare(value1, value3));
t.end();
});

test('objects with no prototypes do not crash', function(t) {
t.plan(2);
var value1 = {foo: 'bar'};
var value2 = Object.create(null);
value2.foo = 'bar';
t.ok(compare(value1, value2));
var value3 = {foo: 'baz'};
t.notOk(compare(value3, value2));
t.end();
});

0 comments on commit 6821948

Please sign in to comment.