99headerLineRe = re .compile (r"^:?-+:?$" )
1010enclosingPipesRe = re .compile (r"^\||\|$" )
1111
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+
1219
1320def getLine (state : StateBlock , line : int ) -> str :
1421 pos = state .bMarks [line ] + state .tShift [line ]
@@ -172,6 +179,7 @@ def table(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool
172179 token = state .push ("tr_close" , "tr" , - 1 )
173180 token = state .push ("thead_close" , "thead" , - 1 )
174181
182+ autocompleted_cells = 0
175183 nextLine = startLine + 2
176184 while nextLine < endLine :
177185 if state .sCount [nextLine ] < state .blkIndent :
@@ -196,6 +204,12 @@ def table(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool
196204 if columns and columns [- 1 ] == "" :
197205 columns .pop ()
198206
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+
199213 if nextLine == startLine + 2 :
200214 token = state .push ("tbody_open" , "tbody" , 1 )
201215 token .map = tbodyLines = [startLine + 2 , 0 ]
0 commit comments