Skip to content

Commit 1971f30

Browse files
Merge pull request #323 from kevinbackhouse/list-depth-limit
Revert bad fix for GHSA-66g8-4hjf-77xh
2 parents 97fdfb1 + 1eda738 commit 1971f30

File tree

6 files changed

+16
-170
lines changed

6 files changed

+16
-170
lines changed

extensions/table.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,12 @@ static cmark_node *try_opening_table_header(cmark_syntax_extension *self,
311311
}
312312
}
313313

314-
assert(cmark_node_get_type(parent_container) == CMARK_NODE_PARAGRAPH);
315314
if (!cmark_node_set_type(parent_container, CMARK_NODE_TABLE)) {
316315
free_table_row(parser->mem, header_row);
317316
free_table_row(parser->mem, marker_row);
318317
return parent_container;
319318
}
320319

321-
// Update the node counts after parent_container changed type.
322-
assert(parent_container->next == NULL);
323-
decr_open_block_count(parser, CMARK_NODE_PARAGRAPH);
324-
incr_open_block_count(parser, CMARK_NODE_TABLE);
325-
326320
if (header_row->paragraph_offset) {
327321
try_inserting_table_header_paragraph(parser, parent_container, (unsigned char *)parent_string,
328322
header_row->paragraph_offset);

src/blocks.c

Lines changed: 12 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,6 @@ static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer,
7070
static void S_process_line(cmark_parser *parser, const unsigned char *buffer,
7171
bufsize_t bytes);
7272

73-
static void subtract_open_block_counts(cmark_parser *parser, cmark_node *node) {
74-
do {
75-
decr_open_block_count(parser, S_type(node));
76-
node->flags &= ~CMARK_NODE__OPEN_BLOCK;
77-
node = node->last_child;
78-
} while (node);
79-
}
80-
81-
static void add_open_block_counts(cmark_parser *parser, cmark_node *node) {
82-
do {
83-
incr_open_block_count(parser, S_type(node));
84-
node->flags |= CMARK_NODE__OPEN_BLOCK;
85-
node = node->last_child;
86-
} while (node);
87-
}
88-
8973
static cmark_node *make_block(cmark_mem *mem, cmark_node_type tag,
9074
int start_line, int start_column) {
9175
cmark_node *e;
@@ -145,7 +129,6 @@ static void cmark_parser_reset(cmark_parser *parser) {
145129
parser->refmap = cmark_reference_map_new(parser->mem);
146130
parser->root = document;
147131
parser->current = document;
148-
add_open_block_counts(parser, document);
149132

150133
parser->syntax_extensions = saved_exts;
151134
parser->inline_syntax_extensions = saved_inline_exts;
@@ -259,18 +242,15 @@ static void remove_trailing_blank_lines(cmark_strbuf *ln) {
259242
// Check to see if a node ends with a blank line, descending
260243
// if needed into lists and sublists.
261244
static bool S_ends_with_blank_line(cmark_node *node) {
262-
while (true) {
263-
if (S_last_line_checked(node)) {
264-
return(S_last_line_blank(node));
265-
} else if ((S_type(node) == CMARK_NODE_LIST ||
266-
S_type(node) == CMARK_NODE_ITEM) && node->last_child) {
267-
S_set_last_line_checked(node);
268-
node = node->last_child;
269-
continue;
270-
} else {
271-
S_set_last_line_checked(node);
272-
return (S_last_line_blank(node));
273-
}
245+
if (S_last_line_checked(node)) {
246+
return(S_last_line_blank(node));
247+
} else if ((S_type(node) == CMARK_NODE_LIST ||
248+
S_type(node) == CMARK_NODE_ITEM) && node->last_child) {
249+
S_set_last_line_checked(node);
250+
return(S_ends_with_blank_line(node->last_child));
251+
} else {
252+
S_set_last_line_checked(node);
253+
return (S_last_line_blank(node));
274254
}
275255
}
276256

@@ -330,12 +310,6 @@ static cmark_node *finalize(cmark_parser *parser, cmark_node *b) {
330310
has_content = resolve_reference_link_definitions(parser, b);
331311
if (!has_content) {
332312
// remove blank node (former reference def)
333-
if (b->flags & CMARK_NODE__OPEN_BLOCK) {
334-
decr_open_block_count(parser, S_type(b));
335-
if (b->prev) {
336-
add_open_block_counts(parser, b->prev);
337-
}
338-
}
339313
cmark_node_free(b);
340314
}
341315
break;
@@ -408,17 +382,6 @@ static cmark_node *finalize(cmark_parser *parser, cmark_node *b) {
408382
return parent;
409383
}
410384

411-
// Recalculates the number of open blocks. Returns true if it matches what's currently stored
412-
// in parser. (Used to check that the counts in parser, which are updated incrementally, are
413-
// correct.)
414-
bool check_open_block_counts(cmark_parser *parser) {
415-
cmark_parser tmp_parser = {0}; // Only used for its open_block_counts and total_open_blocks fields.
416-
add_open_block_counts(&tmp_parser, parser->root);
417-
return
418-
tmp_parser.total_open_blocks == parser->total_open_blocks &&
419-
memcmp(tmp_parser.open_block_counts, parser->open_block_counts, sizeof(parser->open_block_counts)) == 0;
420-
}
421-
422385
// Add a node as child of another. Return pointer to child.
423386
static cmark_node *add_child(cmark_parser *parser, cmark_node *parent,
424387
cmark_node_type block_type, int start_column) {
@@ -437,14 +400,11 @@ static cmark_node *add_child(cmark_parser *parser, cmark_node *parent,
437400
if (parent->last_child) {
438401
parent->last_child->next = child;
439402
child->prev = parent->last_child;
440-
subtract_open_block_counts(parser, parent->last_child);
441403
} else {
442404
parent->first_child = child;
443405
child->prev = NULL;
444406
}
445407
parent->last_child = child;
446-
add_open_block_counts(parser, child);
447-
448408
return child;
449409
}
450410

@@ -1087,14 +1047,8 @@ static cmark_node *check_open_blocks(cmark_parser *parser, cmark_chunk *input,
10871047
*all_matched = false;
10881048
cmark_node *container = parser->root;
10891049
cmark_node_type cont_type;
1090-
cmark_parser tmp_parser; // Only used for its open_block_counts and total_open_blocks fields.
1091-
memcpy(tmp_parser.open_block_counts, parser->open_block_counts, sizeof(parser->open_block_counts));
1092-
tmp_parser.total_open_blocks = parser->total_open_blocks;
1093-
1094-
assert(check_open_block_counts(parser));
10951050

10961051
while (S_last_child_is_open(container)) {
1097-
decr_open_block_count(&tmp_parser, S_type(container));
10981052
container = container->last_child;
10991053
cont_type = S_type(container);
11001054

@@ -1106,53 +1060,6 @@ static cmark_node *check_open_blocks(cmark_parser *parser, cmark_chunk *input,
11061060
continue;
11071061
}
11081062

1109-
// This block of code is a workaround for the quadratic performance
1110-
// issue described here (issue 2):
1111-
//
1112-
// https://github.com/github/cmark-gfm/security/advisories/GHSA-66g8-4hjf-77xh
1113-
//
1114-
// If the current line is empty then we might be able to skip directly
1115-
// to the end of the list of open blocks. To determine whether this is
1116-
// possible, we have been maintaining a count of the number of
1117-
// different types of open blocks. The main criterium is that every
1118-
// remaining block, except the last element of the list, is a LIST or
1119-
// ITEM. The code below checks the conditions, and if they're ok, skips
1120-
// forward to parser->current.
1121-
if (parser->blank && parser->indent == 0) { // Current line is empty
1122-
// Make sure that parser->current doesn't point to a closed block.
1123-
if (parser->current->flags & CMARK_NODE__OPEN_BLOCK) {
1124-
if (parser->current->flags & CMARK_NODE__OPEN) {
1125-
const size_t n_list = read_open_block_count(&tmp_parser, CMARK_NODE_LIST);
1126-
const size_t n_item = read_open_block_count(&tmp_parser, CMARK_NODE_ITEM);
1127-
// At most one block can be something other than a LIST or ITEM.
1128-
if (n_list + n_item + 1 >= tmp_parser.total_open_blocks) {
1129-
// Check that parser->current is suitable for jumping to.
1130-
switch (S_type(parser->current)) {
1131-
case CMARK_NODE_LIST:
1132-
case CMARK_NODE_ITEM:
1133-
if (n_list + n_item != tmp_parser.total_open_blocks) {
1134-
if (parser->current->last_child == NULL) {
1135-
// There's another node type somewhere in the middle of
1136-
// the list, so don't attempt the optimization.
1137-
break;
1138-
}
1139-
}
1140-
// fall through
1141-
case CMARK_NODE_CODE_BLOCK:
1142-
case CMARK_NODE_PARAGRAPH:
1143-
case CMARK_NODE_HTML_BLOCK:
1144-
// Jump to parser->current
1145-
container = parser->current;
1146-
cont_type = S_type(container);
1147-
break;
1148-
default:
1149-
break;
1150-
}
1151-
}
1152-
}
1153-
}
1154-
}
1155-
11561063
switch (cont_type) {
11571064
case CMARK_NODE_BLOCK_QUOTE:
11581065
if (!parse_block_quote_prefix(parser, input))
@@ -1286,9 +1193,8 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container,
12861193
has_content = resolve_reference_link_definitions(parser, *container);
12871194

12881195
if (has_content) {
1289-
cmark_node_set_type(*container, CMARK_NODE_HEADING);
1290-
decr_open_block_count(parser, CMARK_NODE_PARAGRAPH);
1291-
incr_open_block_count(parser, CMARK_NODE_HEADING);
1196+
1197+
(*container)->type = (uint16_t)CMARK_NODE_HEADING;
12921198
(*container)->as.heading.level = lev;
12931199
(*container)->as.heading.setext = true;
12941200
S_advance_offset(parser, input, input->len - 1 - parser->offset, false);
@@ -1443,7 +1349,7 @@ static void add_text_to_container(cmark_parser *parser, cmark_node *container,
14431349
S_set_last_line_blank(container, last_line_blank);
14441350

14451351
tmp = container;
1446-
while (tmp->parent && S_last_line_blank(tmp->parent)) {
1352+
while (tmp->parent) {
14471353
S_set_last_line_blank(tmp->parent, false);
14481354
tmp = tmp->parent;
14491355
}
@@ -1572,7 +1478,6 @@ static void S_process_line(cmark_parser *parser, const unsigned char *buffer,
15721478

15731479
parser->line_number++;
15741480

1575-
assert(parser->current->next == NULL);
15761481
last_matched_container = check_open_blocks(parser, &input, &all_matched);
15771482

15781483
if (!last_matched_container)

src/cmark-gfm.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@ char *cmark_markdown_to_html(const char *text, size_t len, int options);
3737
#define CMARK_NODE_TYPE_MASK (0xc000)
3838
#define CMARK_NODE_VALUE_MASK (0x3fff)
3939

40-
/**
41-
* This is the maximum number of block types (CMARK_NODE_DOCUMENT,
42-
* CMARK_NODE_HEADING, ...). It needs to be bigger than the number of
43-
* hardcoded block types (below) to allow for extensions (see
44-
* cmark_syntax_extension_add_node). But it also determines the size of the
45-
* open_block_counts array in the cmark_parser struct, so we don't want it
46-
* to be excessively large.
47-
*/
48-
#define CMARK_NODE_TYPE_BLOCK_LIMIT 0x20
49-
5040
typedef enum {
5141
/* Error status */
5242
CMARK_NODE_NONE = 0x0000,

src/node.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ typedef struct {
5050

5151
enum cmark_node__internal_flags {
5252
CMARK_NODE__OPEN = (1 << 0),
53-
CMARK_NODE__OPEN_BLOCK = (1 << 1),
54-
CMARK_NODE__LAST_LINE_BLANK = (1 << 2),
55-
CMARK_NODE__LAST_LINE_CHECKED = (1 << 3),
53+
CMARK_NODE__LAST_LINE_BLANK = (1 << 1),
54+
CMARK_NODE__LAST_LINE_CHECKED = (1 << 2),
5655

5756
// Extensions can register custom flags by calling `cmark_register_node_flag`.
5857
// This is the starting value for the custom flags.
59-
CMARK_NODE__REGISTER_FIRST = (1 << 4),
58+
CMARK_NODE__REGISTER_FIRST = (1 << 3),
6059
};
6160

6261
typedef uint16_t cmark_node_internal_flags;

src/parser.h

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,8 @@ struct cmark_parser {
5050
cmark_llist *syntax_extensions;
5151
cmark_llist *inline_syntax_extensions;
5252
cmark_ispunct_func backslash_ispunct;
53-
54-
/**
55-
* The "open" blocks are the blocks visited by the loop in
56-
* check_open_blocks (blocks.c). I.e. the blocks in this list:
57-
*
58-
* parser->root->last_child->...->last_child
59-
*
60-
* open_block_counts is used to keep track of how many of each type of
61-
* node are currently in the open blocks list. Knowing these counts can
62-
* sometimes help to end the loop in check_open_blocks early, improving
63-
* efficiency.
64-
*
65-
* The count is stored at this offset: type - CMARK_NODE_TYPE_BLOCK - 1
66-
* For example, CMARK_NODE_LIST (0x8003) is stored at offset 2.
67-
*/
68-
size_t open_block_counts[CMARK_NODE_TYPE_BLOCK_LIMIT];
69-
size_t total_open_blocks;
7053
};
7154

72-
static CMARK_INLINE void incr_open_block_count(cmark_parser *parser, cmark_node_type type) {
73-
assert(type > CMARK_NODE_TYPE_BLOCK);
74-
assert(type <= CMARK_NODE_TYPE_BLOCK + CMARK_NODE_TYPE_BLOCK_LIMIT);
75-
parser->open_block_counts[type - CMARK_NODE_TYPE_BLOCK - 1]++;
76-
parser->total_open_blocks++;
77-
}
78-
79-
static CMARK_INLINE void decr_open_block_count(cmark_parser *parser, cmark_node_type type) {
80-
assert(type > CMARK_NODE_TYPE_BLOCK);
81-
assert(type <= CMARK_NODE_TYPE_BLOCK + CMARK_NODE_TYPE_BLOCK_LIMIT);
82-
assert(parser->open_block_counts[type - CMARK_NODE_TYPE_BLOCK - 1] > 0);
83-
parser->open_block_counts[type - CMARK_NODE_TYPE_BLOCK - 1]--;
84-
assert(parser->total_open_blocks > 0);
85-
parser->total_open_blocks--;
86-
}
87-
88-
static CMARK_INLINE size_t read_open_block_count(cmark_parser *parser, cmark_node_type type) {
89-
assert(type > CMARK_NODE_TYPE_BLOCK);
90-
assert(type <= CMARK_NODE_TYPE_BLOCK + CMARK_NODE_TYPE_BLOCK_LIMIT);
91-
return parser->open_block_counts[type - CMARK_NODE_TYPE_BLOCK - 1];
92-
}
93-
9455
#ifdef __cplusplus
9556
}
9657
#endif

src/syntax_extension.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ cmark_syntax_extension *cmark_syntax_extension_new(const char *name) {
2929
cmark_node_type cmark_syntax_extension_add_node(int is_inline) {
3030
cmark_node_type *ref = !is_inline ? &CMARK_NODE_LAST_BLOCK : &CMARK_NODE_LAST_INLINE;
3131

32-
if ((*ref & CMARK_NODE_VALUE_MASK) >= CMARK_NODE_TYPE_BLOCK_LIMIT) {
33-
// This assertion will fail if you try to register more extensions than
34-
// are currently allowed by CMARK_NODE_TYPE_BLOCK_MAXNUM. Try increasing
35-
// the limit.
32+
if ((*ref & CMARK_NODE_VALUE_MASK) == CMARK_NODE_VALUE_MASK) {
3633
assert(false);
3734
return (cmark_node_type) 0;
3835
}

0 commit comments

Comments
 (0)