Skip to content

lyd_insert_after sets prev/next pointers incorrectly #19

@lukasmacko

Description

@lukasmacko

The program creates node in order l1, l2, l3 and the tries to adjust the order to be l2, l1, l3.

#include <stdio.h>
#include <stdlib.h>
#include <libyang/libyang.h>

int main(int argc, char **argv)
{
    struct ly_ctx *ctx = ly_ctx_new(".");
    const struct lys_module *test_module = ly_ctx_load_module(ctx, "test-module", NULL);

    if (NULL == test_module){
        puts("load module failed");
        return 1;
    }   

    struct lyd_node *l1 = lyd_new(NULL, test_module, "list");
    lyd_new_leaf(l1, test_module, "key", "A");

    struct lyd_node *l2 = lyd_new(NULL, test_module, "list");
    lyd_new_leaf(l2, test_module, "key", "B");


    struct lyd_node *l3 = lyd_new(NULL, test_module, "list");
    lyd_new_leaf(l3, test_module, "key", "C");

    lyd_insert_after(l1, l2);
    lyd_insert_after(l2, l3);

    /* ERROR*/
    lyd_insert_after(l2, l1); 

   /* THIS WORK as expected
    lyd_insert_before(l1, l2); */

    lyd_free_withsiblings(l1);

    ly_ctx_destroy(ctx, NULL);
    return 0;
}

It might be related to the fact that reordered nodes has no parent (top-level nodes)

The program sets pointer like this:
incorrect

The expected state(in comment):
expected

test-module.yang

module test-module {
  namespace "urn:ietf:params:xml:ns:yang:test-module";
  prefix tm;

  organization "organization";
  description
    "example yang module";

  list list {
    key "key";
    leaf key {
      type string;
    }
  }
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions