data tree BUGFIX change leaf value in its canonical form

Fixes cesnet/netopeer2#143
diff --git a/src/tree_data.c b/src/tree_data.c
index 866edaf..4a0bc52 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -817,23 +817,7 @@
 
     if (!leaf) {
         ly_errno = LY_EINVAL;
-        return EXIT_FAILURE;
-    }
-
-    /* key value cannot be changed */
-    if (leaf->parent && (leaf->parent->schema->nodetype == LYS_LIST)) {
-        slist = (struct lys_node_list *)leaf->parent->schema;
-        for (i = 0; i < slist->keys_size; ++i) {
-            if (ly_strequal(slist->keys[i]->name, leaf->schema->name, 1)) {
-                LOGVAL(LYE_SPEC, LY_VLOG_LYD, leaf, "List key value cannot be changed.");
-                return EXIT_FAILURE;
-            }
-        }
-    }
-
-    if (!strcmp(leaf->value_str, val_str ? val_str : "")) {
-        /* the value remains the same */
-        return EXIT_SUCCESS;
+        return -1;
     }
 
     backup = leaf->value_str;
@@ -844,7 +828,26 @@
     if (!lyp_parse_value(&((struct lys_node_leaf *)leaf->schema)->type, &leaf->value_str, NULL, leaf, NULL, 1, 0)) {
         lydict_remove(leaf->schema->module->ctx, leaf->value_str);
         leaf->value_str = backup;
-        return EXIT_FAILURE;
+        return -1;
+    }
+
+    if (!strcmp(backup, leaf->value_str)) {
+        /* the value remains the same */
+        lydict_remove(leaf->schema->module->ctx, backup);
+        return 1;
+    }
+
+    /* key value cannot be changed */
+    if (leaf->parent && (leaf->parent->schema->nodetype == LYS_LIST)) {
+        slist = (struct lys_node_list *)leaf->parent->schema;
+        for (i = 0; i < slist->keys_size; ++i) {
+            if (ly_strequal(slist->keys[i]->name, leaf->schema->name, 1)) {
+                LOGVAL(LYE_SPEC, LY_VLOG_LYD, leaf, "List key value cannot be changed.");
+                lydict_remove(leaf->schema->module->ctx, leaf->value_str);
+                leaf->value_str = backup;
+                return -1;
+            }
+        }
     }
 
     /* value is correct, remove backup */
@@ -873,7 +876,7 @@
         }
     }
 
-    return EXIT_SUCCESS;
+    return 0;
 }
 
 static struct lyd_node *
diff --git a/src/tree_data.h b/src/tree_data.h
index ef5cf82..e5e5347 100644
--- a/src/tree_data.h
+++ b/src/tree_data.h
@@ -677,7 +677,7 @@
  * @param[in] leaf A leaf node to change.
  * @param[in] val_str String form of the new value to be set to the \p leaf. In case the type is #LY_TYPE_INST
  * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
- * @return 0 on success, non-zero on error.
+ * @return 0 on success, <0 on error, 1 if the (canonical) value matched the original one and no change occured.
  */
 int lyd_change_leaf(struct lyd_node_leaf_list *leaf, const char *val_str);