From d663b95a8cc57c1e92fab7464aaa41fecfc096c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= Date: Mon, 19 Aug 2024 13:18:51 +0200 Subject: [PATCH] tree data BUGFIX don't send null argument into strncmp 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 #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: ed74a24ea tree data UPDATE recognize special JSON [null] value --- src/tree_data_new.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tree_data_new.c b/src/tree_data_new.c index 8406a8882..106d44172 100644 --- a/src/tree_data_new.c +++ b/src/tree_data_new.c @@ -1733,7 +1733,7 @@ lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct ly if (r && (r != LY_EINCOMPLETE)) { /* creating opaque leaf-list */ hints = LYD_NODEHINT_LEAFLIST; - if ((format == LY_VALUE_JSON) && !ly_strncmp("[null]", value, value_len)) { + if ((format == LY_VALUE_JSON) && value && !ly_strncmp("[null]", value, value_len)) { hints |= LYD_VALHINT_EMPTY; } LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, @@ -1774,7 +1774,7 @@ lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct ly if (r && (r != LY_EINCOMPLETE)) { /* creating opaque leaf */ hints = 0; - if (value && (format == LY_VALUE_JSON) && !ly_strncmp("[null]", value, value_len)) { + if (value && (format == LY_VALUE_JSON) && value && !ly_strncmp("[null]", value, value_len)) { hints |= LYD_VALHINT_EMPTY; } LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,