Repository navigation
Description
Hello,
While fuzzing cmark 0.31.2 with Termo, a fuzzer I'm developing, I checked that cmark_render_commonmark output parses back to the same HTML. Two inputs break that. Both still reproduce on master (d56b4bd).
1. Everything after an invalid UTF-8 byte is dropped (CommonMark, LaTeX and man renderers)
// cmark 0.31.2 and master, clang 23, macOS arm64
#include <stdio.h>
#include <string.h>
#include "cmark.h"
int main(void) {
const char *md = "caf\xe9 au lait, please\n"; // Latin-1 é: not valid UTF-8
cmark_node *doc = cmark_parse_document(md, strlen(md), CMARK_OPT_DEFAULT);
printf("html: %s", cmark_render_html(doc, CMARK_OPT_DEFAULT));
printf("commonmark: %s", cmark_render_commonmark(doc, CMARK_OPT_DEFAULT, 0));
printf("latex: %s", cmark_render_latex(doc, CMARK_OPT_DEFAULT, 0));
printf("man: %s", cmark_render_man(doc, CMARK_OPT_DEFAULT, 0));
}Output (\xe9 shown as M-i):
html: <p>cafM-i au lait, please</p>
commonmark: caf
latex: caf
man: .PP
caf
The HTML renderer keeps the whole paragraph, but the other three stop at the invalid byte and silently drop the rest of the text node. The cause is in src/render.c, S_out:
len = cmark_utf8proc_iterate((const uint8_t *)source + i, length - i, &c);
if (len == -1) { // error condition
return; // return without rendering rest of string
}Without CMARK_OPT_VALIDATE_UTF8, invalid bytes reach the tree, so any text from a mis-encoded file loses content when it's rendered back to CommonMark. Possible fixes: write U+FFFD (or the raw byte) and continue with len = 1, or have the parser always replace invalid sequences.
2. A list item that starts with two adjacent lists does not round-trip
const char *md = "1. *\n - y\n"; // an item holding an empty `*` list, then a `-` list
cmark_node *doc = cmark_parse_document(md, strlen(md), CMARK_OPT_DEFAULT);
char *cm = cmark_render_commonmark(doc, CMARK_OPT_DEFAULT, 0);
cmark_node *again = cmark_parse_document(cm, strlen(cm), CMARK_OPT_DEFAULT);
// compare cmark_render_html(doc, …) with cmark_render_html(again, …)The original HTML has both lists inside the ordered item:
<ol>
<li>
<ul>
<li></li>
</ul>
<ul>
<li>y</li>
</ul>
</li>
</ol>With 0.31.2 the renderer prints the <!-- end list --> separator and the second list without the item's indentation, so y ends up outside the <ol>:
1. -
<!-- end list -->
- y
On master (after #638) the indentation now reaches four spaces, so the separator and the second list become an indented code block:
1. -
<!-- end list -->
- y
which renders after the </ol> as <pre><code><!-- end list -->\n - y\n</code></pre>. In both versions, the second list doesn't survive the round trip. This looks related to #638 (an empty item affecting the prefix), but this input still breaks after that fix.
Happy to test fixes against these inputs if it helps.
If you'd like me to keep fuzzing cmark (other parts of the API, or re-checking after a fix), feel free to reach out. I'd be glad to keep working on it with you.
Thanks!
Activity
jgm commentedon Nov 16, 2026
Thank you. Would you mind creating two separate issues for these instead of putting two separate things in one issue? It makes it much easier for us.