Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to free whole datatree #12

Closed
lukasmacko opened this issue Jan 7, 2016 · 2 comments
Closed

Function to free whole datatree #12

lukasmacko opened this issue Jan 7, 2016 · 2 comments

Comments

@lukasmacko
Copy link
Contributor

I would like to suggest including a function that frees whole data tree including siblings. It doesn't make difference if there is only one node at root level, however if there are multiple nodes it would be handy. Currently, you can read data tree by one function call but you have to use loop to free it.

Example:

module example-module {
   namespace "urn:ietf:params:xml:ns:yang:example";
   prefix ie; 

   organization "organization";
   description  "example yang module";
   contact      "contact@example.com";

   container container {
      config true;
      list list {
         leaf leaf {
            type string;
         }
         leaf key1 {
            type string;
         }
         key "key1";
      }   
   }   
   leaf-list number{
      type uint16;
   }   
}
FILE *data = fopen("example-module.data","r"); 

struct lyd_node *data_tree = lyd_parse_fd(ctx, fileno(data), LYD_XML, 0); 
fclose(data); 
lyd_free(data_tree); /*This causes MEMLEAK if data contains container and leaflist*/

My suggestion:

/**
 * Free the subtree and its siblings.
 * @param [in] root
 */
void lyd_free_datatree(struct lyd_node *root){
    struct lyd_node *next = NULL;
    while(NULL != root){
        next = root->next;
        lyd_free(root);
        root = next;
    }
}

(The function name could be also lyd_free_with_siblings.)

What do you think? Or is there something I've overlooked?

rkrejci added a commit that referenced this issue Jan 7, 2016
@rkrejci
Copy link
Collaborator

rkrejci commented Jan 7, 2016

My first thinking was that using simple loop or our LY_TREE_FOR/LYTREE_FOR_SAFE macros is not a big deal for callers. But now I see that the function makes sense since there is possible some optimization in case the provided node is not the "first" sibling. The function was named lyd_free_withsiblings() and it is available from 0.5.3.

@rkrejci rkrejci closed this as completed Jan 7, 2016
@lukasmacko
Copy link
Contributor Author

ok, thanks!

jktjkt added a commit to jktjkt/libyang that referenced this issue Jan 26, 2021
libnetconf2's test `test_server_thread` eventually ends up in this stack
trace:

 libyang/src/tree_schema.c:3288:16: runtime error: null pointer passed as argument 1, which is declared to never be null
 /nix/store/ypih4394q488ljr421x8jak55vmr0ckn-glibc-2.32-dev/include/string.h:44:28: note: nonnull attribute specified here
    #0 0x7f565ebf8f6e in lys_node_dup_recursion libyang/src/tree_schema.c:3288:9
    CESNET#1 0x7f565ebf3409 in lys_node_dup libyang/src/tree_schema.c:3609:14
    CESNET#2 0x7f565ebbf62c in yang_check_deviation libyang/src/parser_yang.c:4646:26
    CESNET#3 0x7f565eba6531 in yang_check_sub_module libyang/src/parser_yang.c:4784:13
    CESNET#4 0x7f565eba3d1c in yang_read_module libyang/src/parser_yang.c:2705:13
    CESNET#5 0x7f565ebd1077 in lys_parse_mem_ libyang/src/tree_schema.c:1083:15
    CESNET#6 0x7f565ebd31ab in lys_parse_fd_ libyang/src/tree_schema.c:1271:14
    CESNET#7 0x7f565e92f291 in ly_ctx_load_localfile libyang/src/context.c:916:39
    CESNET#8 0x7f565e92c31b in ly_ctx_load_sub_module libyang/src/context.c:1063:19
    CESNET#9 0x7f565e92fdcc in ly_ctx_load_module libyang/src/context.c:1102:12
    CESNET#10 0x597422 in main libnetconf2/tests/test_server_thread.c:686:5
    CESNET#11 0x7f565dc98dbc in __libc_start_main (/nix/store/kah5n342wz4i0s9lz9ka4bgz91xa2i94-glibc-2.32/lib/libc.so.6+0x23dbc)
    CESNET#12 0x42a4d9 in _start /build/glibc-2.32/csu/../sysdeps/x86_64/start.S:120

In other words, it would call `memcpy` with the source parameter being
NULL, which is [explicitly said to be undefined
behavior](https://www.imperialviolet.org/2016/06/26/nonnull.html).
peckato1 added a commit to peckato1/libyang that referenced this issue Aug 19, 2024
The recent patch was sending a NULL pointer to ly_strncmp which passed
the NULL to strncmp. However, strncmp arguments are attributed as
nonnull which make undefined behaviour sanitizer unhappy.

  /build/libyang/src/ly_common.c:114:30: runtime error: null pointer passed as argument 2, which is declared to never be null
  /usr/include/string.h:160:33: note: nonnull attribute specified here
      #0 0x79c7f4278907 in ly_strncmp /build/libyang/src/ly_common.c:114:14
      CESNET#1 0x79c7f43faf24 in lyd_new_path_ /build/libyang/src/tree_data_new.c:1736:55
      CESNET#2 0x79c7f43fdf61 in lyd_new_path2 /build/libyang/src/tree_data_new.c:1861:12
      CESNET#3 0x79c7f5249b24 in sr_lyd_new_path /build/dependencies/sysrepo/src/ly_wrap.c:644:9
      CESNET#4 0x79c7f52ec883 in sr_edit_add /build/dependencies/sysrepo/src/edit_diff.c:3765:20
      CESNET#5 0x79c7f5195824 in sr_delete_item /build/dependencies/sysrepo/src/sysrepo.c:3453:16
      CESNET#6 0x5c9e51a98f6e in clear_test /build/sysrepo/tests/test_edit.c:120:5
      CESNET#7 0x79c7f56da98c  (/usr/lib/libcmocka.so.0+0x698c) (BuildId: 4d284c2a057f6dbeaa60a5e68cb5891237d21ae1)
      CESNET#8 0x79c7f56db51c in _cmocka_run_group_tests (/usr/lib/libcmocka.so.0+0x751c) (BuildId: 4d284c2a057f6dbeaa60a5e68cb5891237d21ae1)
      CESNET#9 0x5c9e51a91f41 in main /build/sysrepo/tests/test_edit.c:1571:12
      CESNET#10 0x79c7f3b4ec87  (/usr/lib/libc.so.6+0x25c87) (BuildId: 32a656aa5562eece8c59a585f5eacd6cf5e2307b)
      CESNET#11 0x79c7f3b4ed4b in __libc_start_main (/usr/lib/libc.so.6+0x25d4b) (BuildId: 32a656aa5562eece8c59a585f5eacd6cf5e2307b)
      CESNET#12 0x5c9e5195a2b4 in _start (/build/sysrepo/build-clang-asan/tests/test_edit+0x3a2b4) (BuildId: dc378717130c22094495ee54dcfbdc3254b5f981)

Fixes: ed74a24 tree data UPDATE recognize special JSON [null] value
peckato1 added a commit to peckato1/libyang that referenced this issue Aug 19, 2024
The recent patch was sending a NULL pointer to ly_strncmp which passed
the NULL to strncmp. However, strncmp arguments are attributed as
nonnull which make undefined behaviour sanitizer unhappy.

This ubsan report appeared when running sysrepo tests:

  /build/libyang/src/ly_common.c:114:30: runtime error: null pointer passed as argument 2, which is declared to never be null
  /usr/include/string.h:160:33: note: nonnull attribute specified here
      #0 0x79c7f4278907 in ly_strncmp /build/libyang/src/ly_common.c:114:14
      CESNET#1 0x79c7f43faf24 in lyd_new_path_ /build/libyang/src/tree_data_new.c:1736:55
      CESNET#2 0x79c7f43fdf61 in lyd_new_path2 /build/libyang/src/tree_data_new.c:1861:12
      CESNET#3 0x79c7f5249b24 in sr_lyd_new_path /build/dependencies/sysrepo/src/ly_wrap.c:644:9
      CESNET#4 0x79c7f52ec883 in sr_edit_add /build/dependencies/sysrepo/src/edit_diff.c:3765:20
      CESNET#5 0x79c7f5195824 in sr_delete_item /build/dependencies/sysrepo/src/sysrepo.c:3453:16
      CESNET#6 0x5c9e51a98f6e in clear_test /build/sysrepo/tests/test_edit.c:120:5
      CESNET#7 0x79c7f56da98c  (/usr/lib/libcmocka.so.0+0x698c) (BuildId: 4d284c2a057f6dbeaa60a5e68cb5891237d21ae1)
      CESNET#8 0x79c7f56db51c in _cmocka_run_group_tests (/usr/lib/libcmocka.so.0+0x751c) (BuildId: 4d284c2a057f6dbeaa60a5e68cb5891237d21ae1)
      CESNET#9 0x5c9e51a91f41 in main /build/sysrepo/tests/test_edit.c:1571:12
      CESNET#10 0x79c7f3b4ec87  (/usr/lib/libc.so.6+0x25c87) (BuildId: 32a656aa5562eece8c59a585f5eacd6cf5e2307b)
      CESNET#11 0x79c7f3b4ed4b in __libc_start_main (/usr/lib/libc.so.6+0x25d4b) (BuildId: 32a656aa5562eece8c59a585f5eacd6cf5e2307b)
      CESNET#12 0x5c9e5195a2b4 in _start (/build/sysrepo/build-clang-asan/tests/test_edit+0x3a2b4) (BuildId: dc378717130c22094495ee54dcfbdc3254b5f981)

Fixes: ed74a24 tree data UPDATE recognize special JSON [null] value
peckato1 added a commit to peckato1/libyang that referenced this issue Aug 19, 2024
The recent patch was sending a NULL pointer to ly_strncmp which passed
the NULL to strncmp. However, strncmp arguments are attributed as
nonnull which make undefined behaviour sanitizer unhappy.

This ubsan report appeared when running sysrepo tests:

  /build/libyang/src/ly_common.c:114:30: runtime error: null pointer passed as argument 2, which is declared to never be null
  /usr/include/string.h:160:33: note: nonnull attribute specified here
      #0 0x79c7f4278907 in ly_strncmp /build/libyang/src/ly_common.c:114:14
      CESNET#1 0x79c7f43faf24 in lyd_new_path_ /build/libyang/src/tree_data_new.c:1736:55
      CESNET#2 0x79c7f43fdf61 in lyd_new_path2 /build/libyang/src/tree_data_new.c:1861:12
      CESNET#3 0x79c7f5249b24 in sr_lyd_new_path /build/dependencies/sysrepo/src/ly_wrap.c:644:9
      CESNET#4 0x79c7f52ec883 in sr_edit_add /build/dependencies/sysrepo/src/edit_diff.c:3765:20
      CESNET#5 0x79c7f5195824 in sr_delete_item /build/dependencies/sysrepo/src/sysrepo.c:3453:16
      CESNET#6 0x5c9e51a98f6e in clear_test /build/sysrepo/tests/test_edit.c:120:5
      CESNET#7 0x79c7f56da98c  (/usr/lib/libcmocka.so.0+0x698c) (BuildId: 4d284c2a057f6dbeaa60a5e68cb5891237d21ae1)
      CESNET#8 0x79c7f56db51c in _cmocka_run_group_tests (/usr/lib/libcmocka.so.0+0x751c) (BuildId: 4d284c2a057f6dbeaa60a5e68cb5891237d21ae1)
      CESNET#9 0x5c9e51a91f41 in main /build/sysrepo/tests/test_edit.c:1571:12
      CESNET#10 0x79c7f3b4ec87  (/usr/lib/libc.so.6+0x25c87) (BuildId: 32a656aa5562eece8c59a585f5eacd6cf5e2307b)
      CESNET#11 0x79c7f3b4ed4b in __libc_start_main (/usr/lib/libc.so.6+0x25d4b) (BuildId: 32a656aa5562eece8c59a585f5eacd6cf5e2307b)
      CESNET#12 0x5c9e5195a2b4 in _start (/build/sysrepo/build-clang-asan/tests/test_edit+0x3a2b4) (BuildId: dc378717130c22094495ee54dcfbdc3254b5f981)

Fixes: ed74a24 tree data UPDATE recognize special JSON [null] value
michalvasko pushed a commit that referenced this issue Aug 19, 2024
The recent patch was sending a NULL pointer to ly_strncmp which passed
the NULL to strncmp. However, strncmp arguments are attributed as
nonnull which make undefined behaviour sanitizer unhappy.

This ubsan report appeared when running sysrepo tests:

  /build/libyang/src/ly_common.c:114:30: runtime error: null pointer passed as argument 2, which is declared to never be null
  /usr/include/string.h:160:33: note: nonnull attribute specified here
      #0 0x79c7f4278907 in ly_strncmp /build/libyang/src/ly_common.c:114:14
      #1 0x79c7f43faf24 in lyd_new_path_ /build/libyang/src/tree_data_new.c:1736:55
      #2 0x79c7f43fdf61 in lyd_new_path2 /build/libyang/src/tree_data_new.c:1861:12
      #3 0x79c7f5249b24 in sr_lyd_new_path /build/dependencies/sysrepo/src/ly_wrap.c:644:9
      #4 0x79c7f52ec883 in sr_edit_add /build/dependencies/sysrepo/src/edit_diff.c:3765:20
      #5 0x79c7f5195824 in sr_delete_item /build/dependencies/sysrepo/src/sysrepo.c:3453:16
      #6 0x5c9e51a98f6e in clear_test /build/sysrepo/tests/test_edit.c:120:5
      #7 0x79c7f56da98c  (/usr/lib/libcmocka.so.0+0x698c) (BuildId: 4d284c2a057f6dbeaa60a5e68cb5891237d21ae1)
      #8 0x79c7f56db51c in _cmocka_run_group_tests (/usr/lib/libcmocka.so.0+0x751c) (BuildId: 4d284c2a057f6dbeaa60a5e68cb5891237d21ae1)
      #9 0x5c9e51a91f41 in main /build/sysrepo/tests/test_edit.c:1571:12
      #10 0x79c7f3b4ec87  (/usr/lib/libc.so.6+0x25c87) (BuildId: 32a656aa5562eece8c59a585f5eacd6cf5e2307b)
      #11 0x79c7f3b4ed4b in __libc_start_main (/usr/lib/libc.so.6+0x25d4b) (BuildId: 32a656aa5562eece8c59a585f5eacd6cf5e2307b)
      #12 0x5c9e5195a2b4 in _start (/build/sysrepo/build-clang-asan/tests/test_edit+0x3a2b4) (BuildId: dc378717130c22094495ee54dcfbdc3254b5f981)

Fixes: ed74a24 tree data UPDATE recognize special JSON [null] value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants