libyang BUGFIX memory leaks

reported by coverity
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 05a15be..dc48f6d 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -1758,7 +1758,10 @@
 
     when = calloc(1, sizeof *when);
     LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
-    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
+
+    ret = lyxml_ctx_next(ctx->xmlctx);
+    LY_CHECK_ERR_RET(ret, free(when), ret);
+
     ret = yin_parse_attribute(ctx, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN);
     LY_CHECK_ERR_RET(ret, free(when), ret);
 
diff --git a/src/printer.c b/src/printer.c
index a4905c8..b324030 100644
--- a/src/printer.c
+++ b/src/printer.c
@@ -394,6 +394,8 @@
     (*out)->method.fpath.f = fopen(filepath, "w");
     if (!(*out)->method.fpath.f) {
         LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", filepath, strerror(errno));
+        free(*out);
+        *out = NULL;
         return LY_ESYS;
     }
     (*out)->method.fpath.filepath = strdup(filepath);
diff --git a/src/tree_schema.c b/src/tree_schema.c
index d63ab06..604cce0 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -774,7 +774,7 @@
         LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
         break;
     }
-    LY_CHECK_RET(ret, NULL);
+    LY_CHECK_GOTO(ret, error);
 
     /* make sure that the newest revision is at position 0 */
     lysp_sort_revisions(submod->revs);
@@ -841,7 +841,7 @@
     LY_ARRAY_SIZE_TYPE u, v;
     struct lys_yang_parser_ctx *yangctx = NULL;
     struct lys_yin_parser_ctx *yinctx = NULL;
-    struct lys_parser_ctx *pctx;
+    struct lys_parser_ctx *pctx = NULL;
 
     LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
 
@@ -862,7 +862,7 @@
         LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
         break;
     }
-    LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
+    LY_CHECK_GOTO(ret, error);
 
     /* make sure that the newest revision is at position 0 */
     lysp_sort_revisions(mod->parsed->revs);
@@ -982,7 +982,9 @@
     ly_set_rm(&ctx->list, mod, NULL);
 error:
     lys_module_free(mod, NULL);
-    ly_set_erase(&pctx->tpdfs_nodes, NULL);
+    if (pctx) {
+        ly_set_erase(&pctx->tpdfs_nodes, NULL);
+    }
     if (format == LYS_IN_YANG) {
         yang_parser_ctx_free(yangctx);
     } else {