Skip to content

Commit 9abbb6b

Browse files
BridgeARMylesBorins
authored andcommitted
assert: fix infinite loop
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]>
1 parent 968b867 commit 9abbb6b

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/internal/errors.js

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ function createErrDiff(actual, expected, operator) {
110110
}
111111
actualLines.pop();
112112
expectedLines.pop();
113+
if (actualLines.length === 0 || expectedLines.length === 0)
114+
break;
113115
a = actualLines[actualLines.length - 1];
114116
b = expectedLines[expectedLines.length - 1];
115117
}

test/parallel/test-assert.js

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
const common = require('../common');
2727
const assert = require('assert');
28+
const { inspect } = require('util');
2829
const a = assert;
2930

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

894+
const obj1 = {};
895+
const obj2 = { loop: 'forever' };
896+
obj2[inspect.custom] = () => '{}';
897+
// No infinite loop and no custom inspect.
898+
assert.throws(() => assert.deepEqual(obj1, obj2), {
899+
message: `${start}\n` +
900+
`${actExp}\n` +
901+
'\n' +
902+
' {}'
903+
});
904+
893905
// notDeepEqual tests
894906
message = 'Identical input passed to notDeepStrictEqual:\n[\n 1\n]';
895907
assert.throws(

0 commit comments

Comments
 (0)