Skip to content

Commit

Permalink
assert: fix infinite loop
Browse files Browse the repository at this point in the history
In rare cirumstances it is possible to get a identical error diff.
In such a case the advances diffing runs into a infinite loop.
This fixes it by properly checking for extra entries.

Backport-PR-URL: #19230
PR-URL: #18611
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
BridgeAR authored and MylesBorins committed Mar 20, 2018
1 parent 968b867 commit 9abbb6b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ function createErrDiff(actual, expected, operator) {
}
actualLines.pop();
expectedLines.pop();
if (actualLines.length === 0 || expectedLines.length === 0)
break;
a = actualLines[actualLines.length - 1];
b = expectedLines[expectedLines.length - 1];
}
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

const common = require('../common');
const assert = require('assert');
const { inspect } = require('util');
const a = assert;

function makeBlock(f) {
Expand Down Expand Up @@ -890,6 +891,17 @@ common.expectsError(
() => assert.deepEqual(Array(12).fill(1), Array(12).fill(2)),
{ message });

const obj1 = {};
const obj2 = { loop: 'forever' };
obj2[inspect.custom] = () => '{}';
// No infinite loop and no custom inspect.
assert.throws(() => assert.deepEqual(obj1, obj2), {
message: `${start}\n` +
`${actExp}\n` +
'\n' +
' {}'
});

// notDeepEqual tests
message = 'Identical input passed to notDeepStrictEqual:\n[\n 1\n]';
assert.throws(
Expand Down

0 comments on commit 9abbb6b

Please sign in to comment.