lyds_tree BUGFIX for-iteration over lyd and unlink

Watch out for infinite loops when iterating over nodes
while reordering.
diff --git a/src/tree_data.c b/src/tree_data.c
index a8c42bf..8488401 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -548,7 +548,7 @@
     struct lyd_node_inner *par;
     struct lyd_node *first_sibling;
 
-    assert(!node->next && (node->prev == node));
+    assert(!node->next && (node->prev == node) && (sibling != node));
 
     if (sibling->next) {
         /* sibling had a succeeding node */
@@ -584,7 +584,7 @@
 {
     struct lyd_node_inner *par;
 
-    assert(!node->next && (node->prev == node));
+    assert(!node->next && (node->prev == node) && (sibling != node));
 
     node->next = sibling;
     /* covers situation of sibling being first */
diff --git a/src/tree_data_sorted.c b/src/tree_data_sorted.c
index 69a459b..ce0820c 100644
--- a/src/tree_data_sorted.c
+++ b/src/tree_data_sorted.c
@@ -935,7 +935,7 @@
     pool->rbn = NULL;
 
     for (meta = pool->meta; meta; meta = next) {
-        next = meta->next ? meta->next : NULL;
+        next = meta->next;
         RBT_SET(meta, NULL);
         lyd_free_meta_single(meta);
     }
@@ -1055,9 +1055,10 @@
     LY_ERR ret;
     ly_bool max;
     struct rb_node *rbn;
-    struct lyd_node *iter;
+    struct lyd_node *iter, *next;
 
-    for (iter = node; iter && (iter->schema == (*leader)->schema); iter = iter->next) {
+    for (iter = node; iter && (iter->schema == (*leader)->schema); iter = next) {
+        next = iter->next;
         ret = lyds_create_node(iter, &rbn);
         LY_CHECK_RET(ret);
         rb_insert_node(rbt, rbn, &max);
@@ -1118,7 +1119,7 @@
         struct rb_node **rbt, struct lyds_pool *pool, struct lyd_node **next)
 {
     ly_bool max;
-    struct lyd_node *iter;
+    struct lyd_node *iter, *next_node;
 
     /* let's begin with the leader */
     RBN_RESET(pool->rbn, *leader);
@@ -1126,7 +1127,8 @@
     pool->rbn = rb_iter_next(&pool->iter_state);
 
     /* continue with the rest of the nodes */
-    for (iter = (*leader)->next; iter && (iter->schema == (*leader)->schema); iter = iter->next) {
+    for (iter = (*leader)->next; iter && (iter->schema == (*leader)->schema); iter = next_node) {
+        next_node = iter->next;
         if (!pool->rbn) {
             *next = iter;
             return;