BUGFIX memory leaks
diff --git a/src/parser_yang.c b/src/parser_yang.c
index 08b5189..1970927 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -1013,6 +1013,14 @@
     stmt = calloc(1, sizeof *stmt);
     LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
 
+    /* insert into parent statements */
+    if (!*child) {
+        *child = stmt;
+    } else {
+        for (par_child = *child; par_child->next; par_child = par_child->next);
+        par_child->next = stmt;
+    }
+
     stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
 
     /* get optional argument */
@@ -1026,14 +1034,6 @@
         }
     }
 
-    /* insert into parent statements */
-    if (!*child) {
-        *child = stmt;
-    } else {
-        for (par_child = *child; par_child->next; par_child = par_child->next);
-        par_child->next = stmt;
-    }
-
     YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) {
         LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &stmt->child));
     }
diff --git a/src/tree_schema_compile.c b/src/tree_schema_compile.c
index 2c4ea54..a91af18 100644
--- a/src/tree_schema_compile.c
+++ b/src/tree_schema_compile.c
@@ -3868,6 +3868,7 @@
 
     return cs;
 error:
+    lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
     return NULL;
 
 #undef UNIQUE_CHECK
diff --git a/src/xml.c b/src/xml.c
index 8fa1c7e..df8a829 100644
--- a/src/xml.c
+++ b/src/xml.c
@@ -350,7 +350,12 @@
     LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF);
 error:
     if (!(*buffer)) {
+        /* buffer not provided, buf is local */
         free(buf);
+    } else if (buf) {
+        /* buf is shared with caller via buffer, but buf could be reallocated, so update the provided buffer */
+        (*buffer) = buf;
+        (*buffer_size) = size;
     }
     return LY_EVALID;