From 8d00d9d5104553d1037f64f09873cea98f9d1f64 Mon Sep 17 00:00:00 2001 From: Stefan Gula Date: Sun, 5 Oct 2025 01:46:33 +0200 Subject: [PATCH] validation BUGFIX segfault when using containter with when condition This patch fixes the segfault, which is happening when RPC output data, which uses NP-container with when condition are being validated. --- src/validation.c | 2 +- tests/utests/data/test_validation.c | 38 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/validation.c b/src/validation.c index a10e19d66..010cc4bfb 100644 --- a/src/validation.c +++ b/src/validation.c @@ -1969,7 +1969,7 @@ lyd_validate_tree(struct lyd_node *root, const struct lysc_ext_instance *ext, st if (lysc_has_when(node->schema)) { /* when evaluation */ - r = ly_set_add(node_when, (void *)node, 1, NULL); + r = ly_set_add(node_when, (void *)node, 0, NULL); LY_CHECK_ERR_GOTO(r, rc = r, cleanup); } diff --git a/tests/utests/data/test_validation.c b/tests/utests/data/test_validation.c index 4b4e4e010..32d08e073 100644 --- a/tests/utests/data/test_validation.c +++ b/tests/utests/data/test_validation.c @@ -72,6 +72,43 @@ test_when(void **state) lyd_free_all(tree); } +static void +test_when_rpc_reply(void **state) +{ + struct ly_in *in; + struct lyd_node *tree; + const char *schema = + "module a {\n" + " namespace urn:tests:a;\n" + " prefix a;\n" + " yang-version 1.1;\n" + "\n" + " rpc rpc1 {\n" + " output {\n" + " container c1 {\n" + " when \"../c = 'val_c'\";\n" + " leaf a {\n" + " type string;\n" + " }\n" + " }\n" + " leaf c {\n" + " type string;\n" + " }\n" + " }\n" + " }\n" + "}"; + + UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL); + + const char *data = "{\"a:rpc1\": {\"c\":\"wrong\"}}"; + + assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); + assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_REPLY_YANG, 0, NULL, &tree)); + assert_int_equal(LY_SUCCESS, lyd_validate_op(tree, NULL, LYD_TYPE_REPLY_YANG, NULL)); + ly_in_free(in, 0); + lyd_free_all(tree); +} + static void test_mandatory_when(void **state) { @@ -1600,6 +1637,7 @@ main(void) { const struct CMUnitTest tests[] = { UTEST(test_when), + UTEST(test_when_rpc_reply), UTEST(test_mandatory), UTEST(test_mandatory_when), UTEST(test_type_incomplete_when),