Skip to content

Commit 58a52e8

Browse files
authored
fix: fix infinite loop for indented code blank line (#3947)
* fix: fix infinite loop for indented code blank line * set redos tests to not silent * simplify test * fix test
1 parent 98b3824 commit 58a52e8

4 files changed

Lines changed: 35 additions & 15 deletions

File tree

src/Lexer.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,15 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
110110
src = src.replace(other.tabCharGlobal, ' ').replace(other.spaceLine, '');
111111
}
112112

113+
let srcLength = Infinity;
113114
while (src) {
115+
if (src.length < srcLength) {
116+
srcLength = src.length;
117+
} else {
118+
this.infiniteLoopError(src.charCodeAt(0));
119+
break;
120+
}
121+
114122
let token: Tokens.Generic | undefined;
115123

116124
if (this.options.extensions?.block?.some((extTokenizer) => {
@@ -275,13 +283,8 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
275283
}
276284

277285
if (src) {
278-
const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
279-
if (this.options.silent) {
280-
console.error(errMsg);
281-
break;
282-
} else {
283-
throw new Error(errMsg);
284-
}
286+
this.infiniteLoopError(src.charCodeAt(0));
287+
break;
285288
}
286289
}
287290

@@ -334,7 +337,15 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
334337

335338
let keepPrevChar = false;
336339
let prevChar = '';
340+
let srcLength = Infinity;
337341
while (src) {
342+
if (src.length < srcLength) {
343+
srcLength = src.length;
344+
} else {
345+
this.infiniteLoopError(src.charCodeAt(0));
346+
break;
347+
}
348+
338349
if (!keepPrevChar) {
339350
prevChar = '';
340351
}
@@ -464,16 +475,20 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
464475
}
465476

466477
if (src) {
467-
const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
468-
if (this.options.silent) {
469-
console.error(errMsg);
470-
break;
471-
} else {
472-
throw new Error(errMsg);
473-
}
478+
this.infiniteLoopError(src.charCodeAt(0));
479+
break;
474480
}
475481
}
476482

477483
return tokens;
478484
}
485+
486+
private infiniteLoopError(byte: number) {
487+
const errMsg = 'Infinite loop on byte: ' + byte;
488+
if (this.options.silent) {
489+
console.error(errMsg);
490+
} else {
491+
throw new Error(errMsg);
492+
}
493+
}
479494
}

src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export function rtrim(str: string, c: string, invert?: boolean) {
112112
export function trimTrailingBlankLines(str: string) {
113113
const lines = str.split('\n');
114114
let end = lines.length - 1;
115-
while (end >= 0 && !lines[end].trim()) {
115+
while (end >= 0 && other.blankLine.test(lines[end])) {
116116
end--;
117117
}
118118
if (lines.length - end <= 2) {

test/run-spec-tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ runTests({
4747
runTests({
4848
tests: redosTests,
4949
parse,
50+
defaultMarkedOptions: { silent: false },
5051
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
markdown: '\t\v\n',
3+
html: '<pre><code>\v</code></pre>\n',
4+
};

0 commit comments

Comments
 (0)