Fix: Handle empty and/or unclosed fenced code block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
diff --git a/md4c/md4c.c b/md4c/md4c.c
index f46229f..2f3d394 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -114,7 +114,6 @@ enum MD_LINETYPE_tag {
MD_LINE_SETEXTHEADER,
MD_LINE_SETEXTUNDERLINE,
MD_LINE_INDENTEDCODE,
- MD_LINE_CODEFENCE,
MD_LINE_FENCEDCODE,
MD_LINE_HTML,
MD_LINE_TEXT
@@ -1174,11 +1173,18 @@ md_process_code_block(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
while(n_lines > 0 && lines[n_lines-1].beg == lines[n_lines-1].end) {
n_lines--;
}
+ }
- if(n_lines == 0)
- return 0;
+ /* Skip the first line in case of fenced code: It is the fence.
+ * (Only the starting fence is present due to logic in md_analyze_line().) */
+ if(lines[0].type == MD_LINE_FENCEDCODE) {
+ lines++;
+ n_lines--;
}
+ if(n_lines == 0)
+ return 0;
+
return md_process_verbatim_block(ctx, MD_TEXT_CODE, lines, n_lines);
}
@@ -1514,7 +1520,7 @@ redo_indentation_after_blockquote_mark:
line->beg = off;
/* Check whether we are fenced code continuation. */
- if(pivot_line->type == MD_LINE_FENCEDCODE || pivot_line->type == MD_LINE_CODEFENCE) {
+ if(pivot_line->type == MD_LINE_FENCEDCODE) {
/* We are another MD_LINE_FENCEDCODE unless we are closing fence
* which we transform into MD_LINE_BLANK. */
if(line->indent < ctx->code_indent_offset) {
@@ -1608,7 +1614,7 @@ redo_indentation_after_blockquote_mark:
if(CH(off) == _T('`') || CH(off) == _T('~')) {
if(md_is_opening_code_fence(ctx, off, &off) == 0) {
ctx->code_fence_indent = line->indent;
- line->type = MD_LINE_CODEFENCE;
+ line->type = MD_LINE_FENCEDCODE;
goto done;
}
}
@@ -1751,7 +1757,6 @@ md_process_block(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
break;
case MD_LINE_SETEXTUNDERLINE:
- case MD_LINE_CODEFENCE:
/* Noop. */
return 0;