Skip to content

Commit dfc65e2

Browse files
authored
Merge pull request #1144 from mathjax/issue1142
Add a template substitution count to avoid infinite loop (#1142)
2 parents 9b6e85a + 543fc09 commit dfc65e2

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

ts/input/tex.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export class TeX<N, T, D> extends AbstractInputJax<N, T, D> {
7272
digits: /^(?:[0-9]+(?:\{,\}[0-9]{3})*(?:\.[0-9]*)?|\.[0-9]+)/,
7373
// Maximum size of TeX string to process.
7474
maxBuffer: 5 * 1024,
75+
// Maximum number of array template substitutions (avoids infinite loop from @{\\} for example)
76+
maxTemplateSubtitutions: 10000,
7577
// math-style to use for Latin and Greek letters
7678
mathStyle: 'TeX', // one of TeX, ISO, French, or upright
7779
formatError: (jax: TeX<any, any, any>, err: TexError) =>

ts/input/tex/base/BaseItems.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,11 @@ export class ArrayItem extends BaseItem {
10171017
table: '',
10181018
};
10191019

1020+
/**
1021+
* Substitution count for template substitutions (to avoid infinite loops)
1022+
*/
1023+
public templateSubs: number = 0;
1024+
10201025
/**
10211026
* The TeX parser that created this item
10221027
*/
@@ -1208,6 +1213,18 @@ export class ArrayItem extends BaseItem {
12081213
if (ralign) {
12091214
entry = '\\text{' + entry.trim() + '}';
12101215
}
1216+
if (start || end || ralign) {
1217+
if (
1218+
++this.templateSubs >
1219+
parser.configuration.options.maxTemplateSubtitutions
1220+
) {
1221+
throw new TexError(
1222+
'MaxTemplateSubs',
1223+
'Maximum template substitutions exceeded; ' +
1224+
'is there an invalid use of \\\\ in the template?'
1225+
);
1226+
}
1227+
}
12111228
}
12121229
//
12131230
// Add any \hline or \hfill macros

0 commit comments

Comments
 (0)