Skip to content

Commit 9e0855d

Browse files
Fix GHSL-2023-031: prevent quadratic performance by not allowing very deeply nested lists.
1 parent c32ef78 commit 9e0855d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/blocks.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
#define CODE_INDENT 4
2828
#define TAB_STOP 4
2929

30+
/**
31+
* Very deeply nested lists can cause quadratic performance issues.
32+
* This constant is used in open_new_blocks() to limit the nesting
33+
* depth. It is unlikely that a non-contrived markdown document will
34+
* be nested this deeply.
35+
*/
36+
#define MAX_LIST_DEPTH 100
37+
3038
#ifndef MIN
3139
#define MIN(x, y) ((x < y) ? x : y)
3240
#endif
@@ -1119,10 +1127,11 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container,
11191127
bool has_content;
11201128
int save_offset;
11211129
int save_column;
1130+
size_t depth = 0;
11221131

11231132
while (cont_type != CMARK_NODE_CODE_BLOCK &&
11241133
cont_type != CMARK_NODE_HTML_BLOCK) {
1125-
1134+
depth++;
11261135
S_find_first_nonspace(parser, input);
11271136
indented = parser->indent >= CODE_INDENT;
11281137

@@ -1224,6 +1233,7 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container,
12241233
(*container)->internal_offset = matched;
12251234
} else if ((!indented || cont_type == CMARK_NODE_LIST) &&
12261235
parser->indent < 4 &&
1236+
depth < MAX_LIST_DEPTH &&
12271237
(matched = parse_list_marker(
12281238
parser->mem, input, parser->first_nonspace,
12291239
(*container)->type == CMARK_NODE_PARAGRAPH, &data))) {

0 commit comments

Comments
 (0)