Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for empty buffer when rendering #254

Merged
merged 3 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ char *cmark_render(cmark_node *root, int options, int width,
}

// ensure final newline
if (renderer.buffer->ptr[renderer.buffer->size - 1] != '\n') {
if (renderer.buffer->size == 0 || renderer.buffer->ptr[renderer.buffer->size - 1] != '\n') {
cmark_strbuf_putc(renderer.buffer, '\n');
}

Expand Down
30 changes: 17 additions & 13 deletions test/cmark-fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
#include "cmark.h"

int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
int options = 0;
if (size > sizeof(options)) {
/* First 4 bytes of input are treated as options */
int options = *(const int *)data;
struct __attribute__((packed)) {
int options;
int width;
} fuzz_config;

if (size >= sizeof(fuzz_config)) {
/* The beginning of `data` is treated as fuzzer configuration */
memcpy(&fuzz_config, data, sizeof(fuzz_config));

/* Mask off valid option bits */
options = options & (CMARK_OPT_SOURCEPOS | CMARK_OPT_HARDBREAKS | CMARK_OPT_SAFE | CMARK_OPT_NOBREAKS | CMARK_OPT_NORMALIZE | CMARK_OPT_VALIDATE_UTF8 | CMARK_OPT_SMART);
fuzz_config.options &= (CMARK_OPT_SOURCEPOS | CMARK_OPT_HARDBREAKS | CMARK_OPT_SAFE | CMARK_OPT_NOBREAKS | CMARK_OPT_NORMALIZE | CMARK_OPT_VALIDATE_UTF8 | CMARK_OPT_SMART);

/* Remainder of input is the markdown */
const char *markdown = (const char *)(data + sizeof(options));
const size_t markdown_size = size - sizeof(options);
cmark_node *doc = cmark_parse_document(markdown, markdown_size, options);
const char *markdown = (const char *)(data + sizeof(fuzz_config));
const size_t markdown_size = size - sizeof(fuzz_config);
cmark_node *doc = cmark_parse_document(markdown, markdown_size, fuzz_config.options);

free(cmark_render_commonmark(doc, options, 80));
free(cmark_render_html(doc, options));
free(cmark_render_latex(doc, options, 80));
free(cmark_render_man(doc, options, 80));
free(cmark_render_xml(doc, options));
free(cmark_render_commonmark(doc, fuzz_config.options, fuzz_config.width));
free(cmark_render_html(doc, fuzz_config.options));
free(cmark_render_latex(doc, fuzz_config.options, fuzz_config.width));
free(cmark_render_man(doc, fuzz_config.options, fuzz_config.width));
free(cmark_render_xml(doc, fuzz_config.options));

cmark_node_free(doc);
}
Expand Down