Skip to content

Commit 656a5d0

Browse files
committed
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. PR-URL: nodejs#18611 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 1fc373b commit 656a5d0

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
@@ -184,6 +184,8 @@ function createErrDiff(actual, expected, operator) {
184184
}
185185
actualLines.pop();
186186
expectedLines.pop();
187+
if (actualLines.length === 0 || expectedLines.length === 0)
188+
break;
187189
a = actualLines[actualLines.length - 1];
188190
b = expectedLines[expectedLines.length - 1];
189191
}

test/parallel/test-assert.js

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const { EOL } = require('os');
3131
const EventEmitter = require('events');
3232
const { errorCache } = require('internal/errors');
3333
const { writeFileSync, unlinkSync } = require('fs');
34+
const { inspect } = require('util');
3435
const a = assert;
3536

3637
assert.ok(a.AssertionError.prototype instanceof Error,
@@ -565,6 +566,17 @@ common.expectsError(
565566
() => assert.deepEqual(Array(12).fill(1), Array(12).fill(2)),
566567
{ message });
567568

569+
const obj1 = {};
570+
const obj2 = { loop: 'forever' };
571+
obj2[inspect.custom] = () => '{}';
572+
// No infinite loop and no custom inspect.
573+
assert.throws(() => assert.deepEqual(obj1, obj2), {
574+
message: `${start}\n` +
575+
`${actExp}\n` +
576+
'\n' +
577+
' {}'
578+
});
579+
568580
// notDeepEqual tests
569581
message = 'Identical input passed to notDeepStrictEqual:\n[\n 1\n]';
570582
assert.throws(

0 commit comments

Comments
 (0)