github / cmark-gfm

GitHub's fork of cmark, a CommonMark parsing and rendering library and program in C
Other
893 stars 173 forks source link

`cell_index` is not initialized when a table cell is created using cmark_node_new_with_ext() #360

Open steffen-kiess opened 6 months ago

steffen-kiess commented 6 months ago

Starting with commit 5e8ad61d0a79eb7f7b8ae0863e2ee19387f734f0 a table cell keeps track of its index. When creating a table cell with cmark_node_new_with_ext(), the cell_index value is not initialized anywhere. This will cause functions like cmark_render_html() and cmark_render_xml() to crash.

Example code:

#include <string.h>

#include <cmark-gfm.h>
#include <cmark-gfm-core-extensions.h>

const char* doc_str = "\n"
"| foo | bar |\n"
"| --- | --- |\n"
"| baz | bim |\n"
"";

int main() {
  cmark_gfm_core_extensions_ensure_registered();
  cmark_parser* parser = cmark_parser_new(0);
  cmark_parser_attach_syntax_extension(parser, cmark_find_syntax_extension("table"));
  cmark_parser_feed(parser, doc_str, strlen(doc_str));
  cmark_node* doc = cmark_parser_finish(parser);

  const char* html = cmark_render_html(doc, 0, 0);
  printf("%s\n", html);

  const char* xml = cmark_render_xml(doc, 0);
  printf("%s\n", xml);

  cmark_node* table = cmark_node_first_child(doc);
  cmark_node* header = cmark_node_first_child(table);
  cmark_node* cell = cmark_node_first_child(header);
  printf("cell is a '%s'\n", cmark_node_get_type_string(cell));

  cmark_node* new_cell = cmark_node_new_with_ext(cmark_node_get_type(cell), cmark_node_get_syntax_extension(cell));
  printf("new_cell is a '%s'\n", cmark_node_get_type_string(new_cell));

  cmark_node_prepend_child(header, new_cell);
  cmark_node_unlink(cell);

  const char* html2 = cmark_render_html(doc, 0, 0);
  printf("%s\n", html2);

  const char* xml2 = cmark_render_xml(doc, 0);
  printf("%s\n", xml2);
}