From 9abbb6b857d4f85a291342a9ae088105fca85782 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 7 Feb 2018 02:15:23 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/19230 PR-URL: https://github.com/nodejs/node/pull/18611 Reviewed-By: Joyee Cheung Reviewed-By: Matteo Collina Reviewed-By: Anna Henningsen --- lib/internal/errors.js | 2 ++ test/parallel/test-assert.js | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 13f6dd38cee01d..927e121ef3baee 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -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]; } diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 3f92e1035de1e3..e3088360e3324b 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -25,6 +25,7 @@ const common = require('../common'); const assert = require('assert'); +const { inspect } = require('util'); const a = assert; function makeBlock(f) { @@ -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(