Skip to content

Commit 36e36cd

Browse files
SjlverAshe Connor
authored andcommitted
Fix a buffer overread in the CMark tables extension. (commonmark#128)
* Fix a buffer overread in the CMark tables extension. The following Markdown string is an example that causes an overread: "|\n-|" This was discovered by the Google Autofuzz project. * Add regression test for table buffer overread
1 parent 3c4da5b commit 36e36cd

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

extensions/table.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,17 @@ static table_row *row_from_string(cmark_syntax_extension *self,
114114
cmark_parser *parser, unsigned char *string,
115115
int len) {
116116
table_row *row = NULL;
117-
bufsize_t cell_matched, pipe_matched, offset;
117+
bufsize_t cell_matched = 1, pipe_matched = 1, offset;
118118

119119
row = (table_row *)parser->mem->calloc(1, sizeof(table_row));
120120
row->n_columns = 0;
121121
row->cells = NULL;
122122

123123
offset = scan_table_cell_end(string, len, 0);
124124

125-
do {
125+
// Parse the cells of the row. Stop if we reach the end of the input, or if we
126+
// cannot detect any more cells.
127+
while (offset < len && (cell_matched || pipe_matched)) {
126128
cell_matched = scan_table_cell(string, len, offset);
127129
pipe_matched = scan_table_cell_end(string, len, offset + cell_matched);
128130

@@ -149,7 +151,7 @@ static table_row *row_from_string(cmark_syntax_extension *self,
149151
pipe_matched = scan_table_row_end(string, len, offset);
150152
offset += pipe_matched;
151153
}
152-
} while ((cell_matched || pipe_matched) && offset < len);
154+
}
153155

154156
if (offset != len || !row->n_columns) {
155157
free_table_row(parser->mem, row);

test/regression.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,13 @@ Issue #530 - link parsing corner cases
241241
<p><a href="%3C%3Cb">a</a></p>
242242
<p><a href="%3Cb">a</a></p>
243243
````````````````````````````````
244+
245+
Pull request #128 - Buffer overread in tables extension
246+
247+
```````````````````````````````` example table
248+
|
249+
-|
250+
.
251+
<p>|
252+
-|</p>
253+
````````````````````````````````

0 commit comments

Comments
 (0)