9
9
headerLineRe = re .compile (r"^:?-+:?$" )
10
10
enclosingPipesRe = re .compile (r"^\||\|$" )
11
11
12
+ # Limit the amount of empty autocompleted cells in a table,
13
+ # see https://github.com/markdown-it/markdown-it/issues/1000,
14
+ # Both pulldown-cmark and commonmark-hs limit the number of cells this way to ~200k.
15
+ # We set it to 65k, which can expand user input by a factor of x370
16
+ # (256x256 square is 1.8kB expanded into 650kB).
17
+ MAX_AUTOCOMPLETED_CELLS = 0x10000
18
+
12
19
13
20
def getLine (state : StateBlock , line : int ) -> str :
14
21
pos = state .bMarks [line ] + state .tShift [line ]
@@ -172,6 +179,7 @@ def table(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool
172
179
token = state .push ("tr_close" , "tr" , - 1 )
173
180
token = state .push ("thead_close" , "thead" , - 1 )
174
181
182
+ autocompleted_cells = 0
175
183
nextLine = startLine + 2
176
184
while nextLine < endLine :
177
185
if state .sCount [nextLine ] < state .blkIndent :
@@ -196,6 +204,12 @@ def table(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool
196
204
if columns and columns [- 1 ] == "" :
197
205
columns .pop ()
198
206
207
+ # note: autocomplete count can be negative if user specifies more columns than header,
208
+ # but that does not affect intended use (which is limiting expansion)
209
+ autocompleted_cells += columnCount - len (columns )
210
+ if autocompleted_cells > MAX_AUTOCOMPLETED_CELLS :
211
+ break
212
+
199
213
if nextLine == startLine + 2 :
200
214
token = state .push ("tbody_open" , "tbody" , 1 )
201
215
token .map = tbodyLines = [startLine + 2 , 0 ]
0 commit comments