dictionary REFACTOR unify functions pattern to return LY_ERR

Change API functions manipulating with libyang dictionary to return
LY_ERR value as a primary indicator of the result and the value from the
dictionary is returned via optional output parameter.
diff --git a/src/common.h b/src/common.h
index e4c12b8..0e740f3 100644
--- a/src/common.h
+++ b/src/common.h
@@ -302,8 +302,13 @@
  * @param[in] LEN length of the string in WORD to store.
  * @param[in,out] DYNAMIC Set to 1 if STR is dynamically allocated, 0 otherwise. If set to 1, zerocopy version of lydict_insert is used.
  */
-#define INSERT_STRING(CTX, STRING, LEN, DYNAMIC) \
-    (DYNAMIC ? lydict_insert_zc(CTX, (char *)(STRING)) : lydict_insert(CTX, LEN ? (STRING) : "", LEN)); DYNAMIC = 0
+#define INSERT_STRING_RET(CTX, STRING, LEN, DYNAMIC, TARGET) \
+    if (DYNAMIC) { \
+        LY_CHECK_RET(lydict_insert_zc(CTX, (char *)(STRING), &(TARGET))); \
+    } else { \
+        LY_CHECK_RET(lydict_insert(CTX, LEN ? (STRING) : "", LEN, &(TARGET))); \
+    } \
+    DYNAMIC = 0
 
 #define FREE_STRING(CTX, STRING) if (STRING) {lydict_remove(CTX, STRING);}
 
diff --git a/src/dict.h b/src/dict.h
index 88e9647..69bf7d3 100644
--- a/src/dict.h
+++ b/src/dict.h
@@ -19,6 +19,8 @@
 #include <stdint.h>
 #include <string.h>
 
+#include "log.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -45,9 +47,12 @@
  * NULL terminated string, the len parameter says number of bytes stored in
  * dictionary. The specified number of bytes is duplicated and terminating NULL
  * byte is added automatically. If \p len is 0, it is count automatically using strlen().
- * @return pointer to the string stored in the dictionary, NULL if \p value was NULL.
+ * @param[out] str_p Optional parameter to get pointer to the string corresponding to the @p value and stored in dictionary.
+ * @return LY_SUCCESS in case of successful insertion into dictionary, note that the function does not return LY_EEXIST.
+ * @return LY_EINVAL in case of invalid input parameters.
+ * @return LY_EMEM in case of memory allocation failure.
  */
-const char *lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len);
+LY_ERR lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p);
 
 /**
  * @brief Insert string into dictionary - zerocopy version. If the string is
@@ -61,9 +66,12 @@
  * dictionary. Otherwise, the reference counter is incremented and the value is
  * freed. So, after calling the function, caller is supposed to not use the
  * value address anymore. If NULL, function does nothing.
- * @return pointer to the string stored in the dictionary, NULL if \p value was NULL.
+ * @param[out] str_p Optional parameter to get pointer to the string corresponding to the @p value and stored in dictionary.
+ * @return LY_SUCCESS in case of successful insertion into dictionary, note that the function does not return LY_EEXIST.
+ * @return LY_EINVAL in case of invalid input parameters.
+ * @return LY_EMEM in case of memory allocation failure.
  */
-const char *lydict_insert_zc(const struct ly_ctx *ctx, char *value);
+LY_ERR lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p);
 
 /**
  * @brief Remove specified string from the dictionary. It decrement reference
diff --git a/src/hash_table.c b/src/hash_table.c
index 9d34ca9..5f1af01 100644
--- a/src/hash_table.c
+++ b/src/hash_table.c
@@ -178,10 +178,10 @@
     pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
 }
 
-static char *
-dict_insert(const struct ly_ctx *ctx, char *value, size_t len, ly_bool zerocopy)
+LY_ERR
+dict_insert(const struct ly_ctx *ctx, char *value, size_t len, ly_bool zerocopy, const char **str_p)
 {
-    LY_ERR ret = 0;
+    LY_ERR ret = LY_SUCCESS;
     struct dict_rec *match = NULL, rec;
     uint32_t hash;
 
@@ -199,6 +199,7 @@
         if (zerocopy) {
             free(value);
         }
+        ret = LY_SUCCESS;
     } else if (ret == LY_SUCCESS) {
         if (!zerocopy) {
             /*
@@ -206,49 +207,52 @@
              * record is already inserted in hash table
              */
             match->value = malloc(sizeof *match->value * (len + 1));
-            LY_CHECK_ERR_RET(!match->value, LOGMEM(ctx), NULL);
+            LY_CHECK_ERR_RET(!match->value, LOGMEM(ctx), LY_EMEM);
             memcpy(match->value, value, len);
             match->value[len] = '\0';
         }
     } else {
         /* lyht_insert returned error */
-        LOGINT(ctx);
         if (zerocopy) {
             free(value);
         }
-        return NULL;
+        return ret;
     }
 
-    return match->value;
+    if (str_p) {
+        *str_p = match->value;
+    }
+
+    return ret;
 }
 
-API const char *
-lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len)
+API LY_ERR
+lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
 {
-    const char *result;
+    LY_ERR result;
 
-    LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
+    LY_CHECK_ARG_RET(ctx, ctx, value, LY_EINVAL);
 
     if (!len) {
         len = strlen(value);
     }
 
     pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
-    result = dict_insert(ctx, (char *)value, len, 0);
+    result = dict_insert(ctx, (char *)value, len, 0, str_p);
     pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
 
     return result;
 }
 
-API const char *
-lydict_insert_zc(const struct ly_ctx *ctx, char *value)
+API LY_ERR
+lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
 {
-    const char *result;
+    LY_ERR result;
 
-    LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
+    LY_CHECK_ARG_RET(ctx, ctx, value, LY_EINVAL);
 
     pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
-    result = dict_insert(ctx, value, strlen(value), 1);
+    result = dict_insert(ctx, value, strlen(value), 1, str_p);
     pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
 
     return result;
diff --git a/src/hash_table.h b/src/hash_table.h
index ae4008d..91c3da2 100644
--- a/src/hash_table.h
+++ b/src/hash_table.h
@@ -196,7 +196,8 @@
  * @param[in] hash Hash of the stored value.
  * @param[out] match_p Pointer to the stored value, optional
  * @return LY_SUCCESS on success,
- * @return LY_EEXIST if the value is already present.
+ * @return LY_EEXIST in case the value is already present.
+ * @return LY_EMEM in case of memory allocation failure.
  */
 LY_ERR lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p);
 
@@ -212,7 +213,8 @@
  * @param[in] resize_val_equal Val equal callback to use for resizing.
  * @param[out] match_p Pointer to the stored value, optional
  * @return LY_SUCCESS on success,
- * @return LY_EEXIST if the value is already present.
+ * @return LY_EEXIST in case the value is already present.
+ * @return LY_EMEM in case of memory allocation failure.
  */
 LY_ERR lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash, values_equal_cb resize_val_equal,
         void **match_p);
diff --git a/src/parser.c b/src/parser.c
index 7c483c4..6b6ab74 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -257,22 +257,22 @@
     switch (in->type) {
     case LY_IN_FILEPATH:
         if (realpath(in->method.fpath.filepath, path) != NULL) {
-            *filepath = lydict_insert(ctx, path, 0);
+            lydict_insert(ctx, path, 0, filepath);
         } else {
-            *filepath = lydict_insert(ctx, in->method.fpath.filepath, 0);
+            lydict_insert(ctx, in->method.fpath.filepath, 0, filepath);
         }
 
         break;
     case LY_IN_FD:
 #ifdef __APPLE__
         if (fcntl(in->method.fd, F_GETPATH, path) != -1) {
-            *filepath = lydict_insert(ctx, path, 0);
+            lydict_insert(ctx, path, 0, filepath);
         }
 #else
         /* get URI if there is /proc */
         sprintf(proc_path, "/proc/self/fd/%d", in->method.fd);
         if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
-            *filepath = lydict_insert(ctx, path, len);
+            lydict_insert(ctx, path, len, filepath);
         }
 #endif
         break;
diff --git a/src/parser_json.c b/src/parser_json.c
index 8a9d280..776c65b 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -317,8 +317,8 @@
                 }
                 if (!p) {
                     LY_ARRAY_NEW_GOTO(ctx, prefixes, p, ret, error);
-                    p->id = lydict_insert(ctx, start, len);
-                    p->module_name = lydict_insert(ctx, start, len);
+                    LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->id), error);
+                    LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->module_name), error);
                 } /* else the prefix already present */
             }
             stop = stop + bytes;
@@ -569,7 +569,7 @@
         if (prev != meta_container->name) {
             /* metas' names are stored in dictionary, so checking pointers must works */
             lydict_remove(lydctx->jsonctx->ctx, prev);
-            prev = lydict_insert(lydctx->jsonctx->ctx, meta_container->name, 0);
+            LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name, 0, &prev), cleanup);
             instance = 1;
         } else {
             instance++;
diff --git a/src/parser_lyb.c b/src/parser_lyb.c
index 12a6911..fdc9257 100644
--- a/src/parser_lyb.c
+++ b/src/parser_lyb.c
@@ -432,14 +432,12 @@
         LY_ARRAY_INCREMENT(*prefs);
 
         /* prefix */
-        ret = lyb_read_string(&str, 1, lybctx);
-        LY_CHECK_GOTO(ret, cleanup);
-        (*prefs)[i].id = lydict_insert_zc(lybctx->ctx, str);
+        LY_CHECK_GOTO(ret = lyb_read_string(&str, 1, lybctx), cleanup);
+        LY_CHECK_GOTO(ret = lydict_insert_zc(lybctx->ctx, str, &(*prefs)[i].id), cleanup);
 
         /* module reference is the same as JSON name */
-        ret = lyb_read_string(&str, 1, lybctx);
-        LY_CHECK_GOTO(ret, cleanup);
-        (*prefs)[i].module_name = lydict_insert_zc(lybctx->ctx, str);
+        LY_CHECK_GOTO(ret = lyb_read_string(&str, 1, lybctx), cleanup);
+        LY_CHECK_GOTO(ret = lydict_insert_zc(lybctx->ctx, str, &(*prefs)[i].module_name), cleanup);
     }
 
 cleanup:
@@ -540,7 +538,7 @@
     }
     ly_free_val_prefs(lybctx->ctx, val_prefs);
     if (ret) {
-        ly_free_attr_siblings(lybctx->ctx, *attr);
+        lyd_free_attr_siblings(lybctx->ctx, *attr);
         *attr = NULL;
     }
     return ret;
@@ -884,7 +882,7 @@
     ly_free_val_prefs(ctx, val_prefs);
 
     lyd_free_meta_siblings(meta);
-    ly_free_attr_siblings(ctx, attr);
+    lyd_free_attr_siblings(ctx, attr);
     lyd_free_tree(node);
     return ret;
 }
diff --git a/src/parser_stmt.c b/src/parser_stmt.c
index 3c36fbd..f9fee9b 100644
--- a/src/parser_stmt.c
+++ b/src/parser_stmt.c
@@ -81,14 +81,14 @@
     LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
 
     /* store name and insubstmt info */
-    e->name = lydict_insert(PARSER_CTX(ctx), stmt->stmt, 0);
+    LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->stmt, 0, &e->name));
     e->insubstmt = insubstmt;
     e->insubstmt_index = insubstmt_index;
     /* TODO (duplicate) e->child = stmt->child; */
 
     /* get optional argument */
     if (stmt->arg) {
-        e->argument = lydict_insert(PARSER_CTX(ctx), stmt->arg, 0);
+        LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &e->argument));
     }
 
     return LY_SUCCESS;
@@ -120,7 +120,7 @@
     }
 
     LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg));
-    *value = lydict_insert(PARSER_CTX(ctx), stmt->arg, 0);
+    LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, value));
 
     for (child = stmt->child; child; child = child->next) {
         struct ly_in *in;
@@ -163,7 +163,7 @@
 
     /* allocate new pointer */
     LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
-    *item = lydict_insert(PARSER_CTX(ctx), stmt->arg, 0);
+    LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, item));
 
     for (child = stmt->child; child; child = child->next) {
         struct ly_in *in;
@@ -251,7 +251,7 @@
     const struct lysp_stmt *child;
 
     LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
-    restr->arg = lydict_insert(PARSER_CTX(ctx), stmt->arg, 0);
+    LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &restr->arg));
 
     for (child = stmt->child; child; child = child->next) {
         struct ly_in *in;
@@ -412,7 +412,7 @@
         LY_CHECK_RET(lysp_check_enum_name(ctx, stmt->arg, strlen(stmt->arg)));
     } /* else nothing specific for YANG_BIT */
 
-    enm->name = lydict_insert(PARSER_CTX(ctx), stmt->arg, 0);
+    LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &enm->name));
     CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
 
     for (child = stmt->child; child; child = child->next) {
@@ -605,7 +605,7 @@
 
     assert(buf[0] == 0x06);
     buf[0] = 0x15;
-    *pat = lydict_insert_zc(PARSER_CTX(ctx), buf);
+    LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
 
     for (child = stmt->child; child; child = child->next) {
         struct ly_in *in;
@@ -652,7 +652,7 @@
     memmove(buf + 1, stmt->arg, arg_len);
     buf[0] = 0x06; /* pattern's default regular-match flag */
     buf[arg_len + 1] = '\0'; /* terminating NULL byte */
-    restr->arg = lydict_insert_zc(PARSER_CTX(ctx), buf);
+    LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg));
 
     for (child = stmt->child; child; child = child->next) {
         struct ly_in *in;
@@ -709,7 +709,7 @@
         LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
         return LY_EVALID;
     }
-    type->name = lydict_insert(PARSER_CTX(ctx), stmt->arg, 0);
+    LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &type->name));
 
     for (child = stmt->child; child; child = child->next) {
         struct ly_in *in;
diff --git a/src/parser_xml.c b/src/parser_xml.c
index 496e9ff..43f6925 100644
--- a/src/parser_xml.c
+++ b/src/parser_xml.c
@@ -195,7 +195,7 @@
 
 cleanup:
     if (ret) {
-        ly_free_attr_siblings(xmlctx->ctx, *attr);
+        lyd_free_attr_siblings(xmlctx->ctx, *attr);
         *attr = NULL;
     }
     return ret;
@@ -611,7 +611,7 @@
 
 error:
     lyd_free_meta_siblings(meta);
-    ly_free_attr_siblings(ctx, attr);
+    lyd_free_attr_siblings(ctx, attr);
     lyd_free_tree(node);
     return ret;
 }
@@ -710,7 +710,7 @@
     attr = NULL;
 
 cleanup:
-    ly_free_attr_siblings(xmlctx->ctx, attr);
+    lyd_free_attr_siblings(xmlctx->ctx, attr);
     return ret;
 }
 
@@ -897,7 +897,7 @@
 cleanup:
     if (ret) {
         lyd_free_tree(*envp);
-        ly_free_attr_siblings(xmlctx->ctx, attr);
+        lyd_free_attr_siblings(xmlctx->ctx, attr);
     }
     return ret;
 }
diff --git a/src/parser_yang.c b/src/parser_yang.c
index 529db42..22cfce2 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -40,9 +40,9 @@
  * @param[in] WORD string to store.
  * @param[in] LEN length of the string in WORD to store.
  */
-#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
-    if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
-    else {(TARGET) = lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN);}
+#define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
+    if (BUF) {LY_CHECK_RET(lydict_insert_zc((CTX)->ctx, WORD, &(TARGET)));}\
+    else {LY_CHECK_RET(lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN, &(TARGET)));}
 
 /**
  * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
@@ -785,7 +785,7 @@
         par_child->next = stmt;
     }
 
-    stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
+    LY_CHECK_RET(lydict_insert(ctx->ctx, word, word_len, &stmt->stmt));
     stmt->kw = kw;
 
     /* get optional argument */
@@ -793,9 +793,9 @@
 
     if (word) {
         if (buf) {
-            stmt->arg = lydict_insert_zc(ctx->ctx, word);
+            LY_CHECK_RET(lydict_insert_zc(ctx->ctx, word, &stmt->arg));
         } else {
-            stmt->arg = lydict_insert(ctx->ctx, word, word_len);
+            LY_CHECK_RET(lydict_insert(ctx->ctx, word, word_len, &stmt->arg));
         }
     }
 
@@ -831,7 +831,7 @@
     LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
 
     /* store name and insubstmt info */
-    e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
+    LY_CHECK_RET(lydict_insert(ctx->ctx, ext_name, ext_name_len, &e->name));
     e->insubstmt = insubstmt;
     e->insubstmt_index = insubstmt_index;
 
@@ -839,7 +839,7 @@
     LY_CHECK_RET(get_argument(ctx, in, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
 
     if (word) {
-        INSERT_WORD(ctx, buf, e->argument, word, word_len);
+        INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
     }
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
@@ -880,7 +880,7 @@
     LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
 
     /* store value and spend buf if allocated */
-    INSERT_WORD(ctx, buf, *value, word, word_len);
+    INSERT_WORD_RET(ctx, buf, *value, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -972,7 +972,7 @@
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
 
-    INSERT_WORD(ctx, buf, *belongsto, word, word_len);
+    INSERT_WORD_RET(ctx, buf, *belongsto, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
         switch (kw) {
@@ -1070,7 +1070,7 @@
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
 
-    INSERT_WORD(ctx, buf, inc->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
 
     /* submodules share the namespace with the module names, so there must not be
      * a module of the same name in the context, no need for revision matching */
@@ -1126,7 +1126,7 @@
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, imp->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
         switch (kw) {
@@ -1240,7 +1240,7 @@
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
 
-    INSERT_WORD(ctx, buf, *item, word, word_len);
+    INSERT_WORD_RET(ctx, buf, *item, word, word_len);
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
         case LY_STMT_EXTENSION_INSTANCE:
@@ -1376,7 +1376,7 @@
     LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
 
     CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
-    INSERT_WORD(ctx, buf, restr->arg, word, word_len);
+    INSERT_WORD_RET(ctx, buf, restr->arg, word, word_len);
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
         case LY_STMT_DESCRIPTION:
@@ -1498,13 +1498,13 @@
 
     when = calloc(1, sizeof *when);
     LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
+    *when_p = when;
 
     /* get value */
-    LY_CHECK_ERR_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len), free(when), LY_EMEM);
+    LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
     CHECK_NONEMPTY(ctx, word_len, "when");
-    INSERT_WORD(ctx, buf, when->cond, word, word_len);
+    INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
 
-    *when_p = when;
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -1551,7 +1551,7 @@
 
     /* get name */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, any->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
 
     /* parse substatements */
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
@@ -1704,7 +1704,7 @@
         LY_CHECK_ERR_RET(ret, free(buf), ret);
     } /* else nothing specific for YANG_BIT */
 
-    INSERT_WORD(ctx, buf, enm->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
     CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
@@ -1898,7 +1898,7 @@
 
     assert(buf[0] == 0x06);
     buf[0] = 0x15;
-    *pat = lydict_insert_zc(ctx->ctx, buf);
+    LY_CHECK_RET(lydict_insert_zc(ctx->ctx, buf, pat));
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -1947,7 +1947,7 @@
     memmove(buf + 1, word, word_len);
     buf[0] = 0x06; /* pattern's default regular-match flag */
     buf[word_len + 1] = '\0'; /* terminating NULL byte */
-    restr->arg = lydict_insert_zc(ctx->ctx, buf);
+    LY_CHECK_RET(lydict_insert_zc(ctx->ctx, buf, &restr->arg));
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -2004,7 +2004,7 @@
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, type->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -2108,7 +2108,7 @@
 
     /* get name */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, leaf->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
 
     /* parse substatements */
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
@@ -2377,7 +2377,7 @@
 
     /* get name */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, llist->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
 
     /* parse substatements */
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
@@ -2474,7 +2474,7 @@
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
     CHECK_NONEMPTY(ctx, word_len, "refine");
-    INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
+    INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -2542,7 +2542,7 @@
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
 
     /* parse substatements */
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
@@ -2697,7 +2697,7 @@
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, act->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
     act->nodetype = parent ? LYS_ACTION : LYS_RPC;
     act->parent = parent;
 
@@ -2767,7 +2767,7 @@
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, notif->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
     notif->nodetype = LYS_NOTIF;
     notif->parent = parent;
 
@@ -2859,7 +2859,7 @@
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, grp->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
     grp->nodetype = LYS_GROUPING;
     grp->parent = parent;
 
@@ -2953,7 +2953,7 @@
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
     CHECK_NONEMPTY(ctx, word_len, "augment");
-    INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
+    INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
     aug->nodetype = LYS_AUGMENT;
     aug->parent = parent;
 
@@ -3052,7 +3052,7 @@
 
     /* get name */
     LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, uses->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
 
     /* parse substatements */
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
@@ -3119,7 +3119,7 @@
 
     /* get name */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, cas->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
 
     /* parse substatements */
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
@@ -3200,7 +3200,7 @@
 
     /* get name */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, choice->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
 
     /* parse substatements */
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
@@ -3297,7 +3297,7 @@
 
     /* get name */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, cont->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
 
     /* parse substatements */
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
@@ -3405,7 +3405,7 @@
 
     /* get name */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, list->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
 
     /* parse substatements */
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
@@ -3586,7 +3586,7 @@
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, *argument, word, word_len);
+    INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -3626,7 +3626,7 @@
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, ex->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -3884,7 +3884,7 @@
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
     CHECK_NONEMPTY(ctx, word_len, "deviation");
-    INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
+    INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
         switch (kw) {
@@ -3938,7 +3938,7 @@
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, feat->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -3987,7 +3987,7 @@
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, ident->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -4043,7 +4043,7 @@
 
     /* (sub)module name */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
 
@@ -4253,7 +4253,7 @@
 
     /* submodule name */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    INSERT_WORD(ctx, buf, submod->name, word, word_len);
+    INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
 
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 2e57f93..bfaa155 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -342,7 +342,7 @@
                 /* go to value */
                 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
                 LY_CHECK_RET(yin_validate_value(ctx, val_type));
-                *arg_val = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
+                INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg_val);
                 LY_CHECK_RET(!(*arg_val), LY_EMEM);
             } else {
                 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, ctx->xmlctx->name_len,
@@ -515,7 +515,7 @@
     FREE_STRING(ctx->xmlctx->ctx, real_value);
     saved_value[0] = 0x06;
     saved_value[len + 1] = '\0';
-    restr->arg = lydict_insert_zc(ctx->xmlctx->ctx, saved_value);
+    LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, saved_value, &restr->arg));
     LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
     type->flags |= LYS_SET_PATTERN;
 
@@ -787,7 +787,7 @@
 
     /* modify the new value */
     modified_val[0] = 0x15;
-    *pat = lydict_insert_zc(ctx->xmlctx->ctx, modified_val);
+    LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, modified_val, pat));
 
     return yin_parse_content(ctx, subelems, 1, LY_STMT_MODIFIER, NULL, exts);
 }
@@ -2754,12 +2754,15 @@
 static const char *
 name2nsname(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len)
 {
+    const char *ename = NULL;
     const struct lyxml_ns *ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
+
     LY_CHECK_ERR_RET(!ns, LOGINT(ctx->xmlctx->ctx), NULL);
 
     if (!strcmp(ns->uri, YIN_NS_URI)) {
         /* standard YANG statement in YIN namespace - keep it unprefixed as in case of YANG */
-        return lydict_insert(ctx->xmlctx->ctx, name, name_len);
+        lydict_insert(ctx->xmlctx->ctx, name, name_len, &ename);
+        return ename;
     }
     /* some statement in special namespace (extension instance) */
     size_t ns_len = strlen(ns->uri);
@@ -2776,7 +2779,8 @@
     strncpy(temp, name, name_len);
     result[len] = '\0';
 
-    return lydict_insert_zc(ctx->xmlctx->ctx, result);
+    lydict_insert_zc(ctx->xmlctx->ctx, result, &ename);
+    return ename;
 }
 
 LY_ERR
@@ -3055,7 +3059,7 @@
         /* no resources are allocated in this branch, no need to use cleanup label */
         LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG));
         if (text_content) {
-            *text_content = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
+            INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *text_content);
             LY_CHECK_RET(!*text_content, LY_EMEM);
         }
 
@@ -3101,11 +3105,11 @@
             last_subelem = new_subelem;
 
             last_subelem->flags |= LYS_YIN_ATTR;
-            last_subelem->stmt = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len);
+            LY_CHECK_RET(lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last_subelem->stmt));
             LY_CHECK_RET(!last_subelem->stmt, LY_EMEM);
             LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
 
-            last_subelem->arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
+            INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last_subelem->arg);
             LY_CHECK_RET(!last_subelem->arg, LY_EMEM);
         } else {
             LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
@@ -3132,7 +3136,7 @@
         }
     } else if (ctx->xmlctx->value_len) {
         /* save text content */
-        e->argument = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
+        INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, e->argument);
         LY_CHECK_RET(!e->argument, LY_EMEM);
 
         /* parser next */
@@ -3277,7 +3281,7 @@
         }
 
         /* load and save content */
-        *arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
+        INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg);
         LY_CHECK_RET(!*arg, LY_EMEM);
 
         /* load closing tag of subelement */
@@ -3340,13 +3344,13 @@
             last = new;
 
             last->flags |= LYS_YIN_ATTR;
-            last->stmt = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len);
+            LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last->stmt), cleanup);
             last->kw = LY_STMT_NONE;
             /* attributes with prefix are ignored */
             if (!ctx->xmlctx->prefix) {
                 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
 
-                last->arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
+                INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last->arg);
                 LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup);
             } else {
                 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
@@ -3375,7 +3379,7 @@
     } else {
         /* save element content */
         if (ctx->xmlctx->value_len) {
-            (*element)->arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
+            INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, (*element)->arg);
             LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup);
         }
 
diff --git a/src/plugins_exts_nacm.c b/src/plugins_exts_nacm.c
index 7e353ff..5be0619 100644
--- a/src/plugins_exts_nacm.c
+++ b/src/plugins_exts_nacm.c
@@ -103,7 +103,7 @@
                 inherited->parent = iter;
                 inherited->parent_type = LYEXT_PAR_NODE;
                 if (c_ext->argument) {
-                    inherited->argument = lydict_insert(cctx->ctx, c_ext->argument, strlen(c_ext->argument));
+                    LY_CHECK_RET(lydict_insert(cctx->ctx, c_ext->argument, strlen(c_ext->argument), &inherited->argument));
                 }
                 /* TODO duplicate extension instances */
                 inherited->data = c_ext->data;
diff --git a/src/plugins_types.c b/src/plugins_types.c
index db236d2..f82a330 100644
--- a/src/plugins_types.c
+++ b/src/plugins_types.c
@@ -188,7 +188,7 @@
 static LY_ERR
 ly_type_dup_simple(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
 {
-    dup->canonical = lydict_insert(ctx, original->canonical, strlen(original->canonical));
+    LY_CHECK_RET(lydict_insert(ctx, original->canonical, strlen(original->canonical), &dup->canonical));
     dup->ptr = original->ptr;
     dup->realtype = original->realtype;
     return LY_SUCCESS;
@@ -557,7 +557,7 @@
     }
 
     /* store everything */
-    storage->canonical = lydict_insert_zc(ctx, str);
+    LY_CHECK_RET(lydict_insert_zc(ctx, str, &storage->canonical));
     storage->int64 = num;
     storage->realtype = type;
 
@@ -611,7 +611,7 @@
     }
 
     /* store everything */
-    storage->canonical = lydict_insert_zc(ctx, str);
+    LY_CHECK_RET(lydict_insert_zc(ctx, str, &storage->canonical));
     storage->int64 = num;
     storage->realtype = type;
 
@@ -677,7 +677,7 @@
         LY_CHECK_RET(ly_type_validate_range(type->basetype, type_dec->range, d, buf, err));
     }
 
-    storage->canonical = lydict_insert(ctx, buf, strlen(buf));
+    LY_CHECK_RET(lydict_insert(ctx, buf, strlen(buf), &storage->canonical));
     storage->dec64 = d;
     storage->realtype = type;
 
@@ -767,9 +767,9 @@
     }
 
     if (start != 0 || (value_len && stop != value_len - 1)) {
-        storage->canonical = lydict_insert(ctx, &value[start], stop + 1 - start);
+        LY_CHECK_RET(lydict_insert(ctx, &value[start], stop + 1 - start, &storage->canonical));
     } else {
-        storage->canonical = lydict_insert(ctx, value_len ? value : "", value_len);
+        LY_CHECK_RET(lydict_insert(ctx, value_len ? value : "", value_len, &storage->canonical));
     }
     storage->ptr = NULL;
     storage->realtype = type;
@@ -820,9 +820,9 @@
     LY_CHECK_RET(ly_type_validate_patterns(type_str->patterns, value, value_len, err));
 
     if (options & LY_TYPE_OPTS_DYNAMIC) {
-        storage->canonical = lydict_insert_zc(ctx, (char *)value);
+        LY_CHECK_RET(lydict_insert_zc(ctx, (char *)value, &storage->canonical));
     } else {
-        storage->canonical = lydict_insert(ctx, value_len ? value : "", value_len);
+        LY_CHECK_RET(lydict_insert(ctx, value_len ? value : "", value_len, &storage->canonical));
     }
     storage->ptr = NULL;
     storage->realtype = type;
@@ -919,19 +919,21 @@
         items = NULL;
 
         if (!ws_count && !lws_count && (options & LY_TYPE_OPTS_DYNAMIC)) {
-            can = lydict_insert_zc(ctx, (char *)value);
+            r = lydict_insert_zc(ctx, (char *)value, &can);
+            LY_CHECK_ERR_GOTO(r, ret = r, error);
             value = NULL;
             options &= ~LY_TYPE_OPTS_DYNAMIC;
         } else {
-            can = lydict_insert(ctx, value_len ? &value[lws_count] : "", value_len - ws_count - lws_count);
+            r = lydict_insert(ctx, value_len ? &value[lws_count] : "", value_len - ws_count - lws_count, &can);
+            LY_CHECK_ERR_GOTO(r, ret = r, error);
         }
     } else {
         buf = malloc(buf_size * sizeof *buf);
         LY_CHECK_ERR_GOTO(!buf, LOGMEM(ctx); ret = LY_EMEM, error);
         index = 0;
 
-        ret = ly_set_dup(items, NULL, &items_ordered);
-        LY_CHECK_GOTO(ret, error);
+        r = ly_set_dup(items, NULL, &items_ordered);
+        LY_CHECK_ERR_GOTO(r, ret = r, error);
         items_ordered->count = 0;
 
         /* generate ordered bits list */
@@ -948,7 +950,8 @@
         /* termination NULL-byte */
         buf[index] = '\0';
 
-        can = lydict_insert_zc(ctx, buf);
+        r = lydict_insert_zc(ctx, buf, &can);
+        LY_CHECK_ERR_GOTO(r, ret = r, error);
         buf = NULL;
     }
 
@@ -999,7 +1002,7 @@
         dup->bits_items[u] = original->bits_items[u];
     }
 
-    dup->canonical = lydict_insert(ctx, original->canonical, strlen(original->canonical));
+    LY_CHECK_RET(lydict_insert(ctx, original->canonical, strlen(original->canonical), &dup->canonical));
     dup->realtype = original->realtype;
     return LY_SUCCESS;
 }
@@ -1062,9 +1065,9 @@
     /* validation done */
 
     if (options & LY_TYPE_OPTS_DYNAMIC) {
-        storage->canonical = lydict_insert_zc(ctx, (char *)value);
+        LY_CHECK_RET(lydict_insert_zc(ctx, (char *)value, &storage->canonical));
     } else {
-        storage->canonical = lydict_insert(ctx, value_len ? value : "", value_len);
+        LY_CHECK_RET(lydict_insert(ctx, value_len ? value : "", value_len, &storage->canonical));
     }
     storage->enum_item = &type_enum->enums[u];
     storage->realtype = type;
@@ -1114,9 +1117,9 @@
     }
 
     if (options & LY_TYPE_OPTS_DYNAMIC) {
-        storage->canonical = lydict_insert_zc(ctx, (char *)value);
+        LY_CHECK_RET(lydict_insert_zc(ctx, (char *)value, &storage->canonical));
     } else {
-        storage->canonical = lydict_insert(ctx, value, value_len);
+        LY_CHECK_RET(lydict_insert(ctx, value, value_len, &storage->canonical));
     }
     storage->boolean = i;
     storage->realtype = type;
@@ -1150,7 +1153,7 @@
         }
     }
 
-    storage->canonical = lydict_insert(ctx, "", 0);
+    LY_CHECK_RET(lydict_insert(ctx, "", 0, &storage->canonical));
     storage->ptr = NULL;
     storage->realtype = type;
 
@@ -1275,7 +1278,7 @@
     /* get JSON form since there is no canonical */
     str = (char *)ly_type_print_identityref(storage, LY_PREF_JSON, NULL, &dyn);
     assert(str && dyn);
-    storage->canonical = lydict_insert_zc(ctx, str);
+    LY_CHECK_RET(lydict_insert_zc(ctx, str, &storage->canonical));
     storage->realtype = type;
 
     if (options & LY_TYPE_OPTS_DYNAMIC) {
@@ -1342,7 +1345,6 @@
     struct lysc_type_instanceid *type_inst = (struct lysc_type_instanceid *)type;
     char *errmsg = NULL, *str;
     struct ly_path *path = NULL;
-    struct ly_set predicates = {0};
     struct lyxp_expr *exp = NULL;
     const struct lysc_node *ctx_scnode;
     int rc = 0;
@@ -1357,7 +1359,7 @@
     if ((options & LY_TYPE_OPTS_SCHEMA) && (options & LY_TYPE_OPTS_INCOMPLETE_DATA)) {
         /* we have incomplete schema tree */
         /* HACK temporary storing of the original value */
-        storage->canonical = lydict_insert(ctx, value_len ? value : "", value_len);
+        LY_CHECK_RET(lydict_insert(ctx, value_len ? value : "", value_len, &storage->canonical));
         goto cleanup;
     }
 
@@ -1425,12 +1427,11 @@
     /* store JSON string value */
     str = (char *)ly_type_print_instanceid(storage, LY_PREF_JSON, NULL, &dyn);
     assert(str && dyn);
-    storage->canonical = lydict_insert_zc(ctx, str);
+    LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, str, &storage->canonical), error);
 
     storage->realtype = type;
 
 cleanup:
-    ly_set_erase(&predicates, NULL);
     lyxp_expr_free(ctx, exp);
     ly_path_free(ctx, path);
 
@@ -1445,7 +1446,6 @@
     }
 
 error:
-    ly_set_erase(&predicates, NULL);
     lyxp_expr_free(ctx, exp);
     ly_path_free(ctx, path);
 
@@ -1453,7 +1453,7 @@
         if (rc == -1) {
             *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
             ret = LY_EMEM;
-        } else {
+        } else if (errmsg) {
             *err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, NULL);
         }
     }
@@ -1651,7 +1651,7 @@
 static LY_ERR
 ly_type_dup_instanceid(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
 {
-    dup->canonical = lydict_insert(ctx, original->canonical, strlen(original->canonical));
+    LY_CHECK_RET(lydict_insert(ctx, original->canonical, strlen(original->canonical), &dup->canonical));
     dup->realtype = original->realtype;
     return ly_path_dup(ctx, original->target, &dup->target);
 }
@@ -2102,10 +2102,18 @@
     } else {
         /* prepare subvalue storage */
         subvalue = calloc(1, sizeof *subvalue);
+        if (!subvalue) {
+            *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+            return LY_EMEM;
+        }
         subvalue->value = calloc(1, sizeof *subvalue->value);
+        if (!subvalue->value) {
+            *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+            return LY_EMEM;
+        }
 
         /* remember the original value */
-        subvalue->original = lydict_insert(ctx, value_len ? value : "", value_len);
+        LY_CHECK_RET(lydict_insert(ctx, value_len ? value : "", value_len, &subvalue->original));
 
         /* store format-specific data for later prefix resolution */
         subvalue->format = format;
@@ -2159,7 +2167,7 @@
 
     /* success */
 
-    storage->canonical = lydict_insert(ctx, subvalue->value->canonical, strlen(subvalue->value->canonical));
+    LY_CHECK_RET(lydict_insert(ctx, subvalue->value->canonical, strlen(subvalue->value->canonical), &storage->canonical));
     storage->subvalue = subvalue;
     storage->realtype = type;
 
@@ -2202,15 +2210,15 @@
 static LY_ERR
 ly_type_dup_union(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
 {
-    dup->canonical = lydict_insert(ctx, original->canonical, strlen(original->canonical));
+    LY_CHECK_RET(lydict_insert(ctx, original->canonical, strlen(original->canonical), &dup->canonical));
 
     dup->subvalue = calloc(1, sizeof *dup->subvalue);
     LY_CHECK_ERR_RET(!dup->subvalue, LOGMEM(ctx), LY_EMEM);
     dup->subvalue->value = calloc(1, sizeof *dup->subvalue->value);
     LY_CHECK_ERR_RET(!dup->subvalue->value, LOGMEM(ctx), LY_EMEM);
-    original->subvalue->value->realtype->plugin->duplicate(ctx, original->subvalue->value, dup->subvalue->value);
+    LY_CHECK_RET(original->subvalue->value->realtype->plugin->duplicate(ctx, original->subvalue->value, dup->subvalue->value));
 
-    dup->subvalue->original = lydict_insert(ctx, original->subvalue->original, strlen(original->subvalue->original));
+    LY_CHECK_RET(lydict_insert(ctx, original->subvalue->original, strlen(original->subvalue->original), &dup->subvalue->original));
     dup->subvalue->format = original->subvalue->format;
     dup->subvalue->prefix_data = ly_type_union_dup_prefix_data(ctx, original->subvalue->format,
                                                                original->subvalue->prefix_data);
diff --git a/src/printer_xml.c b/src/printer_xml.c
index 1fc29e4..21d8dc5 100644
--- a/src/printer_xml.c
+++ b/src/printer_xml.c
@@ -93,7 +93,7 @@
 
     /* and added into namespaces */
     if (new_prefix) {
-        new_prefix = lydict_insert(ctx->ctx, new_prefix, 0);
+        LY_CHECK_RET(lydict_insert(ctx->ctx, new_prefix, 0, &new_prefix), NULL);
     }
     LY_CHECK_RET(ly_set_add(&ctx->prefix, (void *)new_prefix, LY_SET_OPT_USEASLIST, NULL), NULL);
     LY_CHECK_RET(ly_set_add(&ctx->ns, (void *)ns, LY_SET_OPT_USEASLIST, &i), NULL);
diff --git a/src/tree_data.c b/src/tree_data.c
index 23f85ef..1159387 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -696,6 +696,7 @@
         ly_bool *dynamic, uint32_t value_hint, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
         const char *module_key, size_t module_key_len, struct lyd_node **node)
 {
+    LY_ERR ret = LY_SUCCESS;
     struct lyd_node_opaq *opaq;
 
     assert(ctx && name && name_len);
@@ -709,27 +710,33 @@
 
     opaq->prev = (struct lyd_node *)opaq;
 
-    opaq->name = lydict_insert(ctx, name, name_len);
+    LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name), finish);
+
     opaq->format = format;
     if (pref_len) {
-        opaq->prefix.id = lydict_insert(ctx, prefix, pref_len);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->prefix.id), finish);
     }
     if (module_key_len) {
-        opaq->prefix.module_ns = lydict_insert(ctx, module_key, module_key_len);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->prefix.module_ns), finish);
     }
 
     opaq->val_prefs = val_prefs;
     if (dynamic && *dynamic) {
-        opaq->value = lydict_insert_zc(ctx, (char *)value);
+        LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
         *dynamic = 0;
     } else {
-        opaq->value = lydict_insert(ctx, value, value_len);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
     }
     opaq->hint = value_hint;
     opaq->ctx = ctx;
 
-    *node = (struct lyd_node *)opaq;
-    return LY_SUCCESS;
+finish:
+    if (ret) {
+        lyd_free_tree((struct lyd_node *)opaq);
+    } else {
+        *node = (struct lyd_node *)opaq;
+    }
+    return ret;
 }
 
 API LY_ERR
@@ -2121,7 +2128,7 @@
         size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, uint32_t value_hint, LY_PREFIX_FORMAT format,
         void *prefix_data, const struct lysc_node *ctx_snode)
 {
-    LY_ERR ret;
+    LY_ERR ret, rc;
     struct lysc_ext_instance *ant = NULL;
     struct lyd_meta *mt, *last;
     LY_ARRAY_COUNT_TYPE u;
@@ -2152,7 +2159,8 @@
         free(mt);
         return ret;
     }
-    mt->name = lydict_insert(mod->ctx, name, name_len);
+    rc = lydict_insert(mod->ctx, name, name_len, &mt->name);
+    LY_CHECK_ERR_RET(rc, free(mt), rc);
 
     /* insert as the last attribute */
     if (parent) {
@@ -2199,6 +2207,7 @@
         size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, uint32_t value_hint, LYD_FORMAT format,
         struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len)
 {
+    LY_ERR ret = LY_SUCCESS;
     struct lyd_attr *at, *last;
 
     assert(ctx && (parent || attr) && (!parent || !parent->schema));
@@ -2210,23 +2219,23 @@
 
     at = calloc(1, sizeof *at);
     LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
-    at->parent = (struct lyd_node_opaq *)parent;
-    at->name = lydict_insert(ctx, name, name_len);
+    LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name), finish);
     if (dynamic && *dynamic) {
-        at->value = lydict_insert_zc(ctx, (char *)value);
+        ret = lydict_insert_zc(ctx, (char *)value, &at->value);
+        LY_CHECK_GOTO(ret, finish);
         *dynamic = 0;
     } else {
-        at->value = lydict_insert(ctx, value, value_len);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
     }
 
     at->hint = value_hint;
     at->format = format;
     at->val_prefs = val_prefs;
     if (prefix_len) {
-        at->prefix.id = lydict_insert(ctx, prefix, prefix_len);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->prefix.id), finish);
     }
     if (module_key_len) {
-        at->prefix.module_ns = lydict_insert(ctx, module_key, module_key_len);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->prefix.module_ns), finish);
     }
 
     /* insert as the last attribute */
@@ -2237,7 +2246,10 @@
         last->next = at;
     }
 
-    if (attr) {
+finish:
+    if (ret) {
+        lyd_free_attr_single(ctx, at);
+    } else if (attr) {
         *attr = at;
     }
     return LY_SUCCESS;
@@ -2539,21 +2551,21 @@
                 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
             }
         }
-        opaq->name = lydict_insert(LYD_CTX(node), orig->name, 0);
+        LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name, 0, &opaq->name), error);
         opaq->format = orig->format;
         if (orig->prefix.id) {
-            opaq->prefix.id = lydict_insert(LYD_CTX(node), orig->prefix.id, 0);
+            LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->prefix.id, 0, &opaq->prefix.id), error);
         }
-        opaq->prefix.module_ns = lydict_insert(LYD_CTX(node), orig->prefix.module_ns, 0);
+        LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->prefix.module_ns, 0, &opaq->prefix.module_ns), error);
         if (orig->val_prefs) {
             LY_ARRAY_CREATE_GOTO(LYD_CTX(node), opaq->val_prefs, LY_ARRAY_COUNT(orig->val_prefs), ret, error);
             LY_ARRAY_FOR(orig->val_prefs, u) {
-                opaq->val_prefs[u].id = lydict_insert(LYD_CTX(node), orig->val_prefs[u].id, 0);
-                opaq->val_prefs[u].module_ns = lydict_insert(LYD_CTX(node), orig->val_prefs[u].module_ns, 0);
+                LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].id, 0, &opaq->val_prefs[u].id), error);
+                LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].module_ns, 0, &opaq->val_prefs[u].module_ns), error);
                 LY_ARRAY_INCREMENT(opaq->val_prefs);
             }
         }
-        opaq->value = lydict_insert(LYD_CTX(node), orig->value, 0);
+        LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
         opaq->ctx = orig->ctx;
     } else if (dup->schema->nodetype & LYD_NODE_TERM) {
         struct lyd_node_term *term = (struct lyd_node_term *)dup;
@@ -2720,7 +2732,7 @@
 API LY_ERR
 lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
 {
-    LY_ERR ret;
+    LY_ERR ret = LY_SUCCESS;
     struct lyd_meta *mt, *last;
 
     LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
@@ -2728,13 +2740,13 @@
     /* create a copy */
     mt = calloc(1, sizeof *mt);
     LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
-    mt->parent = node;
     mt->annotation = meta->annotation;
     ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
-    LY_CHECK_ERR_RET(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), ret);
-    mt->name = lydict_insert(LYD_CTX(node), meta->name, 0);
+    LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
+    LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
 
     /* insert as the last attribute */
+    mt->parent = node;
     if (node->meta) {
         for (last = node->meta; last->next; last = last->next) {}
         last->next = mt;
@@ -2742,7 +2754,10 @@
         node->meta = mt;
     }
 
-    if (dup) {
+finish:
+    if (ret) {
+        lyd_free_meta_single(mt);
+    } else if (dup) {
         *dup = mt;
     }
     return LY_SUCCESS;
diff --git a/src/tree_data.h b/src/tree_data.h
index 33855ec..871fe1b 100644
--- a/src/tree_data.h
+++ b/src/tree_data.h
@@ -894,7 +894,7 @@
  * @param[in] ctx Context where the attributes were created.
  * @param[in] attr Attribute to free.
  */
-void ly_free_attr_single(const struct ly_ctx *ctx, struct lyd_attr *attr);
+void lyd_free_attr_single(const struct ly_ctx *ctx, struct lyd_attr *attr);
 
 /**
  * @brief Free the attribute with any following attributes.
@@ -902,7 +902,7 @@
  * @param[in] ctx Context where the attributes were created.
  * @param[in] attr First attribute to free.
  */
-void ly_free_attr_siblings(const struct ly_ctx *ctx, struct lyd_attr *attr);
+void lyd_free_attr_siblings(const struct ly_ctx *ctx, struct lyd_attr *attr);
 
 /**
  * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
diff --git a/src/tree_data_free.c b/src/tree_data_free.c
index 176d39f..333c5d6 100644
--- a/src/tree_data_free.c
+++ b/src/tree_data_free.c
@@ -77,7 +77,7 @@
 }
 
 static void
-ly_free_attr(const struct ly_ctx *ctx, struct lyd_attr *attr, ly_bool siblings)
+lyd_free_attr(const struct ly_ctx *ctx, struct lyd_attr *attr, ly_bool siblings)
 {
     struct lyd_attr *iter;
     LY_ARRAY_COUNT_TYPE u;
@@ -128,15 +128,15 @@
 }
 
 API void
-ly_free_attr_single(const struct ly_ctx *ctx, struct lyd_attr *attr)
+lyd_free_attr_single(const struct ly_ctx *ctx, struct lyd_attr *attr)
 {
-    ly_free_attr(ctx, attr, 0);
+    lyd_free_attr(ctx, attr, 0);
 }
 
 API void
-ly_free_attr_siblings(const struct ly_ctx *ctx, struct lyd_attr *attr)
+lyd_free_attr_siblings(const struct ly_ctx *ctx, struct lyd_attr *attr)
 {
-    ly_free_attr(ctx, attr, 1);
+    lyd_free_attr(ctx, attr, 1);
 }
 
 void
@@ -197,7 +197,7 @@
     }
 
     if (!node->schema) {
-        ly_free_attr_siblings(LYD_CTX(node), opaq->attr);
+        lyd_free_attr_siblings(LYD_CTX(node), opaq->attr);
     } else {
         /* free the node's metadata */
         lyd_free_meta_siblings(node->meta);
diff --git a/src/tree_data_helpers.c b/src/tree_data_helpers.c
index a8da204..a3e331f 100644
--- a/src/tree_data_helpers.c
+++ b/src/tree_data_helpers.c
@@ -280,7 +280,7 @@
     case LYD_ANYDATA_XML:
     case LYD_ANYDATA_JSON:
         if (value->str) {
-            t->value.str = lydict_insert(LYD_CTX(trg), value->str, 0);
+            LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
         }
         break;
     case LYD_ANYDATA_LYB:
diff --git a/src/tree_schema.c b/src/tree_schema.c
index dc2cf6e..3b6bc19 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -1016,7 +1016,7 @@
     /* make sure that the newest revision is at position 0 */
     lysp_sort_revisions(mod->parsed->revs);
     if (mod->parsed->revs) {
-        mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
     }
 
     /* decide the latest revision */
diff --git a/src/tree_schema_compile.c b/src/tree_schema_compile.c
index 8fbe2f1..cb15a97 100644
--- a/src/tree_schema_compile.c
+++ b/src/tree_schema_compile.c
@@ -50,7 +50,9 @@
  * @param[in] ORIG String to duplicate.
  * @param[out] DUP Where to store the result.
  */
-#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
+#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
+
+#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
 
 #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
     if (ARRAY_P) { \
@@ -355,8 +357,8 @@
         LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
         memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
     }
-    DUP_STRING(ctx, orig->eapptag, dup->eapptag);
-    DUP_STRING(ctx, orig->emsg, dup->emsg);
+    DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
+    DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
     dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
 
     return dup;
@@ -510,8 +512,8 @@
         /* compile the extension definition */
         ext_p->compiled = calloc(1, sizeof **ext);
         ext_p->compiled->refcount = 1;
-        DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
-        DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
+        DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
+        DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done);
         ext_p->compiled->module = (struct lys_module *)ext_mod;
         COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
 
@@ -542,13 +544,15 @@
 lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
         LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
 {
-    LY_ERR ret = LY_EVALID;
+    LY_ERR ret = LY_SUCCESS;
     const char *name;
     size_t u;
     LY_ARRAY_COUNT_TYPE v;
     const char *prefixed_name = NULL;
 
-    DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
+    DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
+    LY_CHECK_RET(ret);
+
     ext->insubstmt = ext_p->insubstmt;
     ext->insubstmt_index = ext_p->insubstmt_index;
     ext->module = ctx->mod_def;
@@ -564,7 +568,7 @@
          * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
          * YANG (import) prefix must be done here. */
         if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
-            prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
+            LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup);
             u = 0;
         } else {
             assert(ctx->mod_def->parsed);
@@ -573,7 +577,7 @@
                     char *s;
                     LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1,
                                       ret = LY_EMEM, cleanup);
-                    prefixed_name = lydict_insert_zc(ctx->ctx, s);
+                    LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup);
                     u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
                     break;
                 }
@@ -582,6 +586,7 @@
         if (!prefixed_name) {
             LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
                    "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
+            ret = LY_EVALID;
             goto cleanup;
         }
     } else {
@@ -594,11 +599,13 @@
         if (!ext_mod) {
             LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
                 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
+            ret = LY_EVALID;
             goto cleanup;
         } else if (!ext_mod->parsed->extensions) {
             LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
                     "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
                     prefixed_name, ext_mod->name);
+            ret = LY_EVALID;
             goto cleanup;
         }
     }
@@ -608,13 +615,14 @@
     LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
         if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
             /* compile extension definition and assign it */
-            LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
+            LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup);
             break;
         }
     }
     if (!ext->def) {
         LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
                "Extension definition of extension instance \"%s\" not found.", prefixed_name);
+        ret = LY_EVALID;
         goto cleanup;
     }
 
@@ -647,17 +655,18 @@
             /* missing extension's argument */
             LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
                    "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
+            ret = LY_EVALID;
             goto cleanup;
 
         }
-        ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
         stmt->flags |= LYS_YIN_ARGUMENT;
     }
     if (prefixed_name != ext_p->name) {
         lydict_remove(ctx->ctx, ext_p->name);
         ext_p->name = prefixed_name;
         if (!ext_p->argument && ext->argument) {
-            ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
+            LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup);
         }
     }
 
@@ -665,14 +674,13 @@
         if (ext->argument) {
             lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
         }
-        LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
+        LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
         if (ext->argument) {
             lysc_update_path(ctx, NULL, NULL);
         }
     }
     ext_p->compiled = ext;
 
-    ret = LY_SUCCESS;
 cleanup:
     if (prefixed_name && prefixed_name != ext_p->name) {
         lydict_remove(ctx->ctx, prefixed_name);
@@ -911,8 +919,8 @@
     (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
     (*when)->module = ctx->mod_def;
     (*when)->context = lysc_xpath_context(node);
-    DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
-    DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
+    DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
+    DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
     LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
     COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
     (*when)->flags = flags & LYS_STATUS_MASK;
@@ -936,10 +944,10 @@
     must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
     LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
     must->module = ctx->mod_def;
-    DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
-    DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
-    DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
-    DUP_STRING(ctx->ctx, must_p->ref, must->ref);
+    DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
+    DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
+    DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
+    DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
     COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
 
 done:
@@ -1023,9 +1031,9 @@
 
         LY_ARRAY_INCREMENT(*identities);
         COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
-        DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name);
-        DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc);
-        DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref);
+        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done);
+        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done);
+        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done);
         (*identities)[offset + u].module = ctx_sc->mod;
         COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
                            lys_compile_iffeature, ret, done);
@@ -1217,6 +1225,7 @@
 lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
         struct lysp_feature *features_p, struct lysc_feature **features)
 {
+    LY_ERR ret = LY_SUCCESS;
     LY_ARRAY_COUNT_TYPE offset = 0, u;
     struct lysc_ctx context = {0};
 
@@ -1244,9 +1253,9 @@
 
         LY_ARRAY_INCREMENT(*features);
         COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
-        DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
-        DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
-        DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
+        DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done);
+        DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done);
+        DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done);
         (*features)[offset + u].flags = features_p[u].flags;
         (*features)[offset + u].module = ctx_sc->mod;
 
@@ -1254,7 +1263,8 @@
     }
     lysc_update_path(ctx_sc, NULL, NULL);
 
-    return LY_SUCCESS;
+done:
+    return ret;
 }
 
 /**
@@ -1896,19 +1906,19 @@
     /* we rewrite the following values as the types chain is being processed */
     if (range_p->eapptag) {
         lydict_remove(ctx->ctx, (*range)->eapptag);
-        (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
     }
     if (range_p->emsg) {
         lydict_remove(ctx->ctx, (*range)->emsg);
-        (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
     }
     if (range_p->dsc) {
         lydict_remove(ctx->ctx, (*range)->dsc);
-        (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
     }
     if (range_p->ref) {
         lydict_remove(ctx->ctx, (*range)->ref);
-        (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
     }
     /* extensions are taken only from the last range by the caller */
 
@@ -2189,11 +2199,11 @@
         if (patterns_p[u].arg[0] == 0x15) {
             (*pattern)->inverted = 1;
         }
-        DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
-        DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
-        DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
-        DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
-        DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
+        DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr, ret, done);
+        DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
+        DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
+        DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
+        DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
         COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
     }
 done:
@@ -2260,9 +2270,9 @@
 
     LY_ARRAY_FOR(enums_p, u) {
         LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
-        DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
-        DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
-        DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
+        DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
+        DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done);
+        DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
         e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
         if (base_enums) {
             /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
@@ -2957,7 +2967,7 @@
 
         if (units && !*units) {
             /* inherit units */
-            DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
+            DUP_STRING_GOTO(ctx->ctx, tctx->tpdf->units, *units, ret, cleanup);
         }
         if (dflt && !*dflt) {
             /* inherit default */
@@ -3330,9 +3340,9 @@
      * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
     LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
 
-    DUP_STRING(ctx->ctx, action_p->name, action->name);
-    DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
-    DUP_STRING(ctx->ctx, action_p->ref, action->ref);
+    DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
+    DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
+    DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
     COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
     COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
 
@@ -3414,9 +3424,9 @@
     ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
     LY_CHECK_GOTO(ret, cleanup);
 
-    DUP_STRING(ctx->ctx, notif_p->name, notif->name);
-    DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
-    DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
+    DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
+    DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
+    DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
     COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
     COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
     if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
@@ -3572,7 +3582,7 @@
         LY_CHECK_GOTO(ret, done);
     }
     if (leaf_p->units) {
-        leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
         leaf->flags |= LYS_SET_UNITS;
     }
 
@@ -3613,7 +3623,7 @@
         LY_CHECK_GOTO(ret, done);
     }
     if (llist_p->units) {
-        llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
+        LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
         llist->flags |= LYS_SET_UNITS;
     }
 
@@ -4030,7 +4040,7 @@
         /* we need the implicit case first, so create a fake parsed case */
         cs_p = calloc(1, sizeof *cs_p);
         cs_p->nodetype = LYS_CASE;
-        DUP_STRING(ctx->ctx, child_p->name, cs_p->name);
+        DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, done);
         cs_p->child = child_p;
 
         /* make the child the only case child */
@@ -4046,6 +4056,7 @@
         child_p->next = child_next_p;
     }
 
+done:
     return ret;
 }
 
@@ -4800,13 +4811,13 @@
         /* description */
         if (rfn->dsc) {
             FREE_STRING(ctx->ctx, node->dsc);
-            node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
+            LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->dsc, 0, &node->dsc), cleanup);
         }
 
         /* reference */
         if (rfn->ref) {
             FREE_STRING(ctx->ctx, node->ref);
-            node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
+            LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->ref, 0, &node->ref), cleanup);
         }
 
         /* config */
@@ -5416,9 +5427,9 @@
     if (!(ctx->options & LYSC_OPT_FREE_SP)) {
         node->sp = node_p;
     }
-    DUP_STRING(ctx->ctx, node_p->name, node->name);
-    DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
-    DUP_STRING(ctx->ctx, node_p->ref, node->ref);
+    DUP_STRING_GOTO(ctx->ctx, node_p->name, node->name, ret, error);
+    DUP_STRING_GOTO(ctx->ctx, node_p->dsc, node->dsc, ret, error);
+    DUP_STRING_GOTO(ctx->ctx, node_p->ref, node->ref, ret, error);
     if (node_p->when) {
         LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
         LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
@@ -5535,7 +5546,7 @@
 static LY_ERR
 lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysc_node *target, uint32_t dev_flags, struct lysp_deviate_add *d)
 {
-    LY_ERR ret = LY_EVALID;
+    LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
     struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
     struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
     LY_ARRAY_COUNT_TYPE x;
@@ -5573,7 +5584,8 @@
         DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units);
 
         FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
-        DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units);
+        DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units, rc);
+        LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
     }
 
     /* *must-stmt */
@@ -6167,7 +6179,7 @@
 static LY_ERR
 lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d)
 {
-    LY_ERR ret = LY_EVALID;
+    LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
     struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
     struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
     LY_ARRAY_COUNT_TYPE x;
@@ -6224,7 +6236,8 @@
                             units, "replacing", "units", d->units);
 
         lydict_remove(ctx->ctx, leaf->units);
-        DUP_STRING(ctx->ctx, d->units, leaf->units);
+        DUP_STRING(ctx->ctx, d->units, leaf->units, rc);
+        LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
     }
 
     /* [default-stmt] */
@@ -6710,7 +6723,8 @@
                         const char ***units_array = (const char ***)substmts[u].storage;
                         LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
                     }
-                    *units = lydict_insert(ctx->ctx, stmt->arg, 0);
+                    r = lydict_insert(ctx->ctx, stmt->arg, 0, units);
+                    LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
                     break;
                 }
                 case LY_STMT_TYPE: {
@@ -7092,14 +7106,14 @@
     /* create the parsed extension instance manually */
     ext_p = calloc(1, sizeof *ext_p);
     LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
-    ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
-    ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
+    LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup);
+    LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup);
     ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
     ext_p->insubstmt_index = 0;
 
     stmt = calloc(1, sizeof *ext_p->child);
-    stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
-    stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
+    LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup);
+    LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup);
     stmt->kw = LY_STMT_TYPE;
     ext_p->child = stmt;
 
diff --git a/src/xml.c b/src/xml.c
index 8cb3a8c..7061538 100644
--- a/src/xml.c
+++ b/src/xml.c
@@ -1111,8 +1111,8 @@
                     }
                     if (!p) {
                         LY_ARRAY_NEW_GOTO(xmlctx->ctx, prefixes, p, ret, error);
-                        p->id = lydict_insert(xmlctx->ctx, start, len);
-                        p->module_ns = lydict_insert(xmlctx->ctx, ns->uri, 0);
+                        LY_CHECK_GOTO(ret = lydict_insert(xmlctx->ctx, start, len, &p->id), error);
+                        LY_CHECK_GOTO(ret = lydict_insert(xmlctx->ctx, ns->uri, 0, &p->module_ns), error);
                     } /* else the prefix already present */
                 }
             }
diff --git a/src/xpath.c b/src/xpath.c
index c20a384..bc23eae 100644
--- a/src/xpath.c
+++ b/src/xpath.c
@@ -2678,7 +2678,7 @@
     /* init lyxp_expr structure */
     ret = calloc(1, sizeof *ret);
     LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
-    ret->expr = lydict_insert(ctx, expr, expr_len);
+    LY_CHECK_GOTO(lydict_insert(ctx, expr, expr_len, &ret->expr), error);
     LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
     ret->used = 0;
     ret->size = LYXP_EXPR_SIZE_START;
@@ -2978,7 +2978,7 @@
 
     dup->used = exp->used;
     dup->size = exp->used;
-    dup->expr = lydict_insert(ctx, exp->expr, 0);
+    LY_CHECK_GOTO(lydict_insert(ctx, exp->expr, 0, &dup->expr), error);
 
     return dup;
 
@@ -7001,7 +7001,7 @@
     if (!scnode && moveto_mod) {
         /* we are not able to match based on a schema node and not all the modules match,
          * use dictionary for efficient comparison */
-        ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
+        LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
     }
 
 moveto:
diff --git a/tests/utests/schema/test_parser_yin.c b/tests/utests/schema/test_parser_yin.c
index e5fa612..94df4f0 100644
--- a/tests/utests/schema/test_parser_yin.c
+++ b/tests/utests/schema/test_parser_yin.c
@@ -1630,9 +1630,10 @@
 {
     struct test_parser_yin_state *st = *state;
     const char *data;
-    const char *pat = lydict_insert(st->ctx, "\006pattern", 8);
+    const char *pat;
     struct lysp_ext_instance *exts = NULL;
 
+    assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
     data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\">" EXT_SUBELEM "</modifier>" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(st, data, &pat, NULL, &exts), LY_SUCCESS);
     assert_string_equal(pat, "\x015pattern");
@@ -1643,7 +1644,7 @@
     exts = NULL;
     FREE_STRING(st->ctx, pat);
 
-    pat = lydict_insert(st->ctx, "\006pattern", 8);
+    assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
     data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(st, data, &pat, NULL, NULL), LY_EVALID);
     logbuf_assert("Invalid value \"invert\" of \"value\" attribute in \"modifier\" element. Only valid value is \"invert-match\". Line number 1.");
diff --git a/tests/utests/test_hash_table.c b/tests/utests/test_hash_table.c
index c9943ba..bf40371 100644
--- a/tests/utests/test_hash_table.c
+++ b/tests/utests/test_hash_table.c
@@ -73,12 +73,12 @@
 
     assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
 
-    assert_null(lydict_insert(NULL, NULL, 0));
+    assert_int_equal(LY_EINVAL, lydict_insert(NULL, NULL, 0, NULL));
     logbuf_assert("Invalid argument ctx (lydict_insert()).");
 
-    assert_null(lydict_insert_zc(NULL, NULL));
+    assert_int_equal(LY_EINVAL, lydict_insert_zc(NULL, NULL, NULL));
     logbuf_assert("Invalid argument ctx (lydict_insert_zc()).");
-    assert_null(lydict_insert_zc(ctx, NULL));
+    assert_int_equal(LY_EINVAL, lydict_insert_zc(ctx, NULL, NULL));
     logbuf_assert("Invalid argument value (lydict_insert_zc()).");
 
     ly_ctx_destroy(ctx, NULL);
@@ -89,19 +89,20 @@
 {
     (void) state; /* unused */
 
-    const char *str1, *str2;
+    const char *str1, *str2, *str3;
     struct ly_ctx *ctx;
 
     assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
 
     /* insert 2 strings, one of them repeatedly */
-    str1 = lydict_insert(ctx, "test1", 0);
+    assert_int_equal(LY_SUCCESS, lydict_insert(ctx, "test1", 0, &str1));
     assert_non_null(str1);
     /* via zerocopy we have to get the same pointer as provided */
     assert_non_null(str2 = strdup("test2"));
-    assert_true(str2 == lydict_insert_zc(ctx, (char *)str2));
+    assert_int_equal(LY_SUCCESS, lydict_insert_zc(ctx, (char *)str2, &str3));
+    assert_ptr_equal(str2, str3);
     /* here we get the same pointer as in case the string was inserted first time */
-    str2 = lydict_insert(ctx, "test1", 0);
+    assert_int_equal(LY_SUCCESS, lydict_insert(ctx, "test1", 0, &str2));
     assert_non_null(str2);
     assert_ptr_equal(str1, str2);