plugins MAINTENANCE unification of duplication clb
diff --git a/src/plugins_types.c b/src/plugins_types.c
index ca7c384..b3aceb9 100644
--- a/src/plugins_types.c
+++ b/src/plugins_types.c
@@ -299,6 +299,7 @@
 API LY_ERR
 lyplg_type_dup_simple(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
 {
+    memset(dup, 0, sizeof *dup);
     LY_CHECK_RET(lydict_insert(ctx, original->_canonical, 0, &dup->_canonical));
     memcpy(dup->fixed_mem, original->fixed_mem, sizeof dup->fixed_mem);
     dup->realtype = original->realtype;
diff --git a/src/plugins_types/binary.c b/src/plugins_types/binary.c
index df77177..c5474df 100644
--- a/src/plugins_types/binary.c
+++ b/src/plugins_types/binary.c
@@ -341,29 +341,29 @@
     LY_ERR ret;
     struct lyd_value_binary *orig_val, *dup_val;
 
+    memset(dup, 0, sizeof *dup);
+
     ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
-    LY_CHECK_RET(ret);
+    LY_CHECK_GOTO(ret, error);
 
     LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
-    if (!dup_val) {
-        lydict_remove(ctx, dup->_canonical);
-        return LY_EMEM;
-    }
+    LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
 
     LYD_VALUE_GET(original, orig_val);
+
     if (orig_val->size) {
         dup_val->data = malloc(orig_val->size);
-        if (!dup_val->data) {
-            lydict_remove(ctx, dup->_canonical);
-            LYPLG_TYPE_VAL_INLINE_DESTROY(dup_val);
-            return LY_EMEM;
-        }
+        LY_CHECK_ERR_GOTO(!dup_val->data, ret = LY_EMEM, error);
         memcpy(dup_val->data, orig_val->data, orig_val->size);
     }
     dup_val->size = orig_val->size;
-
     dup->realtype = original->realtype;
+
     return LY_SUCCESS;
+
+error:
+    lyplg_type_free_binary(ctx, dup);
+    return ret;
 }
 
 API void
diff --git a/src/plugins_types/ipv4_address.c b/src/plugins_types/ipv4_address.c
index 176a726..1d3b07c 100644
--- a/src/plugins_types/ipv4_address.c
+++ b/src/plugins_types/ipv4_address.c
@@ -306,26 +306,26 @@
     LY_ERR ret;
     struct lyd_value_ipv4_address *orig_val, *dup_val;
 
+    memset(dup, 0, sizeof *dup);
+
     ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
-    LY_CHECK_RET(ret);
+    LY_CHECK_GOTO(ret, error);
 
     LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
-    if (!dup_val) {
-        lydict_remove(ctx, dup->_canonical);
-        return LY_EMEM;
-    }
+    LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
 
     LYD_VALUE_GET(original, orig_val);
+
     memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
     ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
-    if (ret) {
-        lydict_remove(ctx, dup->_canonical);
-        LYPLG_TYPE_VAL_INLINE_DESTROY(dup_val);
-        return ret;
-    }
+    LY_CHECK_GOTO(ret, error);
 
     dup->realtype = original->realtype;
     return LY_SUCCESS;
+
+error:
+    lyplg_type_free_ipv4_address(ctx, dup);
+    return ret;
 }
 
 /**
diff --git a/src/plugins_types/ipv4_prefix.c b/src/plugins_types/ipv4_prefix.c
index d630279..697e904 100644
--- a/src/plugins_types/ipv4_prefix.c
+++ b/src/plugins_types/ipv4_prefix.c
@@ -270,20 +270,23 @@
     LY_ERR ret;
     struct lyd_value_ipv4_prefix *orig_val, *dup_val;
 
+    memset(dup, 0, sizeof *dup);
+
     ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
-    LY_CHECK_RET(ret);
+    LY_CHECK_GOTO(ret, error);
 
     LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
-    if (!dup_val) {
-        lydict_remove(ctx, dup->_canonical);
-        return LY_EMEM;
-    }
+    LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
 
     LYD_VALUE_GET(original, orig_val);
     memcpy(dup_val, orig_val, sizeof *orig_val);
 
     dup->realtype = original->realtype;
     return LY_SUCCESS;
+
+error:
+    lyplg_type_free_ipv4_prefix(ctx, dup);
+    return ret;
 }
 
 /**
diff --git a/src/plugins_types/ipv6_address.c b/src/plugins_types/ipv6_address.c
index e7a5066..1ed2e8f 100644
--- a/src/plugins_types/ipv6_address.c
+++ b/src/plugins_types/ipv6_address.c
@@ -311,7 +311,7 @@
     memset(dup, 0, sizeof *dup);
 
     ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
-    LY_CHECK_RET(ret);
+    LY_CHECK_GOTO(ret, error);
 
     LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
     LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
diff --git a/src/plugins_types/ipv6_address_no_zone.c b/src/plugins_types/ipv6_address_no_zone.c
index 220f377..b69da95 100644
--- a/src/plugins_types/ipv6_address_no_zone.c
+++ b/src/plugins_types/ipv6_address_no_zone.c
@@ -247,20 +247,23 @@
     LY_ERR ret;
     struct lyd_value_ipv6_address_no_zone *orig_val, *dup_val;
 
+    memset(dup, 0, sizeof *dup);
+
     ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
-    LY_CHECK_RET(ret);
+    LY_CHECK_GOTO(ret, error);
 
     LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
-    if (!dup_val) {
-        lydict_remove(ctx, dup->_canonical);
-        return LY_EMEM;
-    }
+    LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
 
     LYD_VALUE_GET(original, orig_val);
     memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
 
     dup->realtype = original->realtype;
     return LY_SUCCESS;
+
+error:
+    lyplg_type_free_ipv6_address_no_zone(ctx, dup);
+    return ret;
 }
 
 /**
diff --git a/src/plugins_types/ipv6_prefix.c b/src/plugins_types/ipv6_prefix.c
index 6cf6790..4a4701f 100644
--- a/src/plugins_types/ipv6_prefix.c
+++ b/src/plugins_types/ipv6_prefix.c
@@ -284,20 +284,23 @@
     LY_ERR ret;
     struct lyd_value_ipv6_prefix *orig_val, *dup_val;
 
+    memset(dup, 0, sizeof *dup);
+
     ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
-    LY_CHECK_RET(ret);
+    LY_CHECK_GOTO(ret, error);
 
     LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
-    if (!dup_val) {
-        lydict_remove(ctx, dup->_canonical);
-        return LY_EMEM;
-    }
+    LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
 
     LYD_VALUE_GET(original, orig_val);
     memcpy(dup_val, orig_val, sizeof *orig_val);
 
     dup->realtype = original->realtype;
     return LY_SUCCESS;
+
+error:
+    lyplg_type_free_ipv6_prefix(ctx, dup);
+    return ret;
 }
 
 /**