libyang BUGFIX dict leak fixes
diff --git a/src/resolve.c b/src/resolve.c
index a3fd7ad..f3d6304 100644
--- a/src/resolve.c
+++ b/src/resolve.c
@@ -1538,6 +1538,7 @@
             f_size--;
 
             if (r == -1) {
+                lydict_remove(node->module->ctx, iff_data->fname);
                 free(iff_data);
                 goto error;
             }
@@ -3156,13 +3157,15 @@
     struct ly_ctx *ctx = module->ctx;
 
     assert(value);
+    memset(&node, 0, sizeof node);
 
     if (type->base <= LY_TYPE_DER) {
         /* the type was not resolved yet, nothing to do for now */
-        return EXIT_FAILURE;
+        ret = EXIT_FAILURE;
+        goto cleanup;
     } else if (!tpdf && !module->implemented) {
         /* do not check defaults in not implemented module's data */
-        return EXIT_SUCCESS;
+        goto cleanup;
     } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) {
         /* identityrefs are checked when instantiated in data instead of typedef,
          * but in typedef the value has to be modified to include the prefix */
@@ -3173,39 +3176,41 @@
                 /* default prefix of the module where the typedef is defined */
                 if (asprintf(&s, "%s:%s", lys_main_module(module)->name, *value) == -1) {
                     LOGMEM(ctx);
-                    return -1;
+                    ret = -1;
+                    goto cleanup;
                 }
                 dflt = lydict_insert_zc(ctx, s);
             }
             lydict_remove(ctx, *value);
             *value = dflt;
+            dflt = NULL;
         }
-        return EXIT_SUCCESS;
+        goto cleanup;
     } else if (type->base == LY_TYPE_LEAFREF && tpdf) {
         /* leafref in typedef cannot be checked */
-        return EXIT_SUCCESS;
+        goto cleanup;
     }
 
-    dflt = *value;
+    dflt = lydict_insert(ctx, *value, 0);
     if (!dflt) {
         /* we do not have a new default value, so is there any to check even, in some base type? */
         for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) {
             if (base_tpdf->dflt) {
-                dflt = base_tpdf->dflt;
+                dflt = lydict_insert(ctx, base_tpdf->dflt, 0);
                 break;
             }
         }
 
         if (!dflt) {
             /* no default value, nothing to check, all is well */
-            return EXIT_SUCCESS;
+            goto cleanup;
         }
 
         /* so there is a default value in a base type, but can the default value be no longer valid (did we define some new restrictions)? */
         switch (type->base) {
         case LY_TYPE_IDENT:
             if (lys_main_module(base_tpdf->type.parent->module)->implemented) {
-                return EXIT_SUCCESS;
+                goto cleanup;
             } else {
                 /* check the default value from typedef, but use also the typedef's module
                  * due to possible searching in imported modules which is expected in
@@ -3218,29 +3223,29 @@
         case LY_TYPE_BOOL:
         case LY_TYPE_EMPTY:
             /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */
-            return EXIT_SUCCESS;
+            goto cleanup;
         case LY_TYPE_BITS:
             /* the default value must match the restricted list of values, if the type was restricted */
             if (type->info.bits.count) {
                 break;
             }
-            return EXIT_SUCCESS;
+            goto cleanup;
         case LY_TYPE_ENUM:
             /* the default value must match the restricted list of values, if the type was restricted */
             if (type->info.enums.count) {
                 break;
             }
-            return EXIT_SUCCESS;
+            goto cleanup;
         case LY_TYPE_DEC64:
             if (type->info.dec64.range) {
                 break;
             }
-            return EXIT_SUCCESS;
+            goto cleanup;
         case LY_TYPE_BINARY:
             if (type->info.binary.length) {
                 break;
             }
-            return EXIT_SUCCESS;
+            goto cleanup;
         case LY_TYPE_INT8:
         case LY_TYPE_INT16:
         case LY_TYPE_INT32:
@@ -3252,35 +3257,45 @@
             if (type->info.num.range) {
                 break;
             }
-            return EXIT_SUCCESS;
+            goto cleanup;
         case LY_TYPE_STRING:
             if (type->info.str.length || type->info.str.patterns) {
                 break;
             }
-            return EXIT_SUCCESS;
+            goto cleanup;
         case LY_TYPE_UNION:
             /* way too much trouble learning whether we need to check the default again, so just do it */
             break;
         default:
             LOGINT(ctx);
-            return -1;
+            ret = -1;
+            goto cleanup;
         }
     } else if (type->base == LY_TYPE_EMPTY) {
         LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name);
         LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value.");
-        return -1;
+        ret = -1;
+        goto cleanup;
     }
 
     /* dummy leaf */
     memset(&node, 0, sizeof node);
-    node.value_str = dflt;
+    node.value_str = lydict_insert(ctx, dflt, 0);
     node.value_type = type->base;
 
     if (tpdf) {
         node.schema = calloc(1, sizeof (struct lys_node_leaf));
-        LY_CHECK_ERR_RETURN(!node.schema, LOGMEM(ctx), -1);
+        if (!node.schema) {
+            LOGMEM(ctx);
+            ret = -1;
+            goto cleanup;
+        }
         r = asprintf((char **)&node.schema->name, "typedef-%s-default", ((struct lys_tpdf *)type->parent)->name);
-        LY_CHECK_ERR_RETURN(r == -1, LOGMEM(ctx); free(node.schema), -1);
+        if (r == -1) {
+            LOGMEM(ctx);
+            ret = -1;
+            goto cleanup;
+        }
         node.schema->module = module;
         memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
     } else {
@@ -3290,19 +3305,21 @@
     if (type->base == LY_TYPE_LEAFREF) {
         if (!type->info.lref.target) {
             ret = EXIT_FAILURE;
-            goto finish;
+            goto cleanup;
         }
         ret = check_default(&type->info.lref.target->type, &dflt, module, 0);
         if (!ret) {
             /* adopt possibly changed default value to its canonical form */
             if (*value) {
+                lydict_remove(ctx, *value);
                 *value = dflt;
+                dflt = NULL;
             }
         }
     } else {
         if (!lyp_parse_value(type, &node.value_str, NULL, &node, NULL, module, 1, 1)) {
             /* possible forward reference */
-            ret = 1;
+            ret = EXIT_FAILURE;
             if (base_tpdf) {
                 /* default value is defined in some base typedef */
                 if ((type->base == LY_TYPE_BITS && type->der->type.der) ||
@@ -3315,23 +3332,28 @@
             }
         } else {
             /* success - adopt canonical form from the node into the default value */
-            if (dflt != node.value_str) {
+            if (!ly_strequal(dflt, node.value_str, 1)) {
                 /* this can happen only if we have non-inherited default value,
                  * inherited default values are already in canonical form */
-                assert(dflt == *value);
+                assert(ly_strequal(dflt, *value, 1));
+
+                lydict_remove(ctx, *value);
                 *value = node.value_str;
+                node.value_str = NULL;
             }
         }
     }
 
-finish:
+cleanup:
     if (node.value_type == LY_TYPE_BITS) {
         free(node.value.bit);
     }
-    if (tpdf) {
+    lydict_remove(ctx, node.value_str);
+    if (tpdf && node.schema) {
         free((char *)node.schema->name);
         free(node.schema);
     }
+    lydict_remove(ctx, dflt);
 
     return ret;
 }