cmark

My personal build of CMark ✏️

Commit
677a22519a212093438f4e5a08d79f9c74013488
Parent
f7613e98edbb8987e0e0ecfb3c789c7ee8d60017
Author
John MacFarlane <jgm@berkeley.edu>
Date

Merge pull request #220 from nwellnhof/node_insert_fix

Don't allow insert_{before|after} root node

Diffstat

2 files changed, 6 insertions, 2 deletions

Status File Name N° Changes Insertions Deletions
Modified api_test/main.c 4 4 0
Modified src/node.c 4 2 2
diff --git a/api_test/main.c b/api_test/main.c
@@ -298,6 +298,10 @@ create_tree(test_batch_runner *runner)
 	cmark_node *doc = cmark_node_new(CMARK_NODE_DOCUMENT);
 
 	cmark_node *p = cmark_node_new(CMARK_NODE_PARAGRAPH);
+	OK(runner, !cmark_node_insert_before(doc, p),
+	   "insert before root fails");
+	OK(runner, !cmark_node_insert_after(doc, p),
+	   "insert after root fails");
 	OK(runner, cmark_node_append_child(doc, p), "append1");
 	INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append1 consistent");
 	OK(runner, cmark_node_parent(p) == doc, "node_parent");
diff --git a/src/node.c b/src/node.c
@@ -436,7 +436,7 @@ cmark_node_unlink(cmark_node *node) {
 int
 cmark_node_insert_before(cmark_node *node, cmark_node *sibling)
 {
-	if (!S_can_contain(node->parent, sibling)) {
+	if (!node->parent || !S_can_contain(node->parent, sibling)) {
 		return 0;
 	}
 
@@ -467,7 +467,7 @@ cmark_node_insert_before(cmark_node *node, cmark_node *sibling)
 int
 cmark_node_insert_after(cmark_node *node, cmark_node *sibling)
 {
-	if (!S_can_contain(node->parent, sibling)) {
+	if (!node->parent || !S_can_contain(node->parent, sibling)) {
 		return 0;
 	}