Skip to content

Commit

Permalink
Avoid an infinite loop in wrapAnsiString (#1864)
Browse files Browse the repository at this point in the history
* Avoid an infinite loop in `wrapAnsiString`

If a terminal's width is reported by node as `0`, this would get caught in an infinite loop, and crash v8 trying to allocate an ever-expanding array.

This works around this by immediately returning the supplied string if the width supplied is `0`, and adds a test for such a case.

fixes #1863

* Update utils-test.js
  • Loading branch information
ticky authored and cpojer committed Oct 5, 2016
1 parent a712f39 commit 2e36e25
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ exports[`trimAndFormatPath() trims dirname 1`] = `"...234567890/123

exports[`trimAndFormatPath() trims dirname and basename 1`] = `"...1234.js"`;

exports[`wrapAnsiString() returns the string unaltered if given a terminal width of zero 1`] = `"This string shouldn\'t cause you any trouble"`;

exports[`wrapAnsiString() returns the string unaltered if given a terminal width of zero 2`] = `"This string shouldn\'t cause you any trouble"`;

exports[`wrapAnsiString() wraps a long string containing ansi chars 1`] = `
"abcde red-
bold 12344
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-cli/src/reporters/__tests__/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ describe('wrapAnsiString()', () => {
expect(wrapAnsiString(string, 10)).toMatchSnapshot();
expect(stripAnsi(wrapAnsiString(string, 10))).toMatchSnapshot();
});

it('returns the string unaltered if given a terminal width of zero', () => {
const string = `This string shouldn't cause you any trouble`;
expect(wrapAnsiString(string, 0)).toMatchSnapshot();
expect(stripAnsi(wrapAnsiString(string, 0))).toMatchSnapshot();
});
});

describe('trimAndFormatPath()', () => {
Expand Down
20 changes: 14 additions & 6 deletions packages/jest-cli/src/reporters/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,14 @@ const renderTime = (
return time;
};

// wrap a string that contains ANSI escape sequences. ANSI escape sequences
// do not add to the string length.
const wrapAnsiString = (string: string, width: number) => {
// word-wrap a string that contains ANSI escape sequences.
// ANSI escape sequences do not add to the string length.
const wrapAnsiString = (string: string, terminalWidth: number) => {
if (terminalWidth === 0) {
// if the terminal width is zero, don't bother word-wrapping
return string;
}

const ANSI_REGEXP = /[\u001b\u009b]\[\d{1,2}m/g;
const tokens = [];
let lastIndex = 0;
Expand All @@ -218,11 +223,14 @@ const wrapAnsiString = (string: string, width: number) => {
return tokens.reduce(
(lines, [kind, token]) => {
if (kind === 'string') {
if (lastLineLength + token.length > width) {
if (lastLineLength + token.length > terminalWidth) {

while (token.length) {
const chunk = token.slice(0, width - lastLineLength);
const remaining = token.slice(width - lastLineLength, token.length);
const chunk = token.slice(0, terminalWidth - lastLineLength);
const remaining = token.slice(
terminalWidth - lastLineLength,
token.length,
);
lines[lines.length - 1] += chunk;
lastLineLength += chunk.length;
token = remaining;
Expand Down

0 comments on commit 2e36e25

Please sign in to comment.