Skip to content

Commit b67fff3

Browse files
affan-archmichalvasko
authored andcommitted
parser cbor BUGFIX metadata coupled with the wrong node
lydcbor_subtree_r() processes all the members of a map in one call, but attr_node, set for a "@" member, was not reset for the following members, so any later "@name" metadata were coupled with the parent instead of the named sibling. With an opaque parent, and also for opaque leaf-list instances, the leaf-list error path then dereferenced the NULL schema of the previous instance.
1 parent 7e8fa4b commit b67fff3

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎src/parser_cbor.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,9 @@ lydcbor_meta_attr(struct lyd_cbor_ctx *lydctx, const struct lysc_node *snode, st
896896
LY_CHECK_GOTO((cbor_typeof(item) != CBOR_TYPE_MAP) && !lydcbor_is_null(item), representation_error);
897897

898898
if (!node || (node->schema != prev->schema)) {
899-
LOG_LOCSET(prev->schema);
899+
LOG_LOCSET(snode);
900900
LOGVAL(ctx, NULL, LYVE_REFERENCE, "Missing CBOR data instance #%" PRIu32 " of %s:%s to be coupled with metadata.",
901-
i + 1, prev->schema->module->name, prev->schema->name);
901+
i + 1, snode->module->name, snode->name);
902902
LOG_LOCBACK(1);
903903
rc = LY_EVALID;
904904
goto cleanup;
@@ -1471,6 +1471,7 @@ lydcbor_subtree_r(struct lyd_cbor_ctx *lydctx, struct lyd_node *parent, struct l
14711471
free(key_str);
14721472
key_str = NULL;
14731473
key_len = 0;
1474+
attr_node = NULL;
14741475

14751476
if (!key_item || !value_item) {
14761477
LOGVAL(ctx, NULL, LYVE_SYNTAX, "Null key or value in CBOR map");

‎tests/utests/data/test_cbor.c‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,66 @@ test_attributes(void **state)
464464
lyd_free_all(tree);
465465
}
466466

467+
static void
468+
test_attributes_parent(void **state)
469+
{
470+
struct lyd_node *tree;
471+
char *buffer, *json;
472+
struct ly_out *out;
473+
size_t i;
474+
475+
/* metadata of the parent ("@") followed by metadata of its children */
476+
const struct test_case tests[] = {
477+
{
478+
"leaf",
479+
"{\"cbor-test:cont-root\":{\"@\":{\"cbor-test:a1\":\"val1\"},\"lf-root-str\":\"x\","
480+
"\"@lf-root-str\":{\"cbor-test:a2\":5}}}"
481+
},
482+
{
483+
"leaf-list",
484+
"{\"cbor-test:list-nested\":[{\"@\":{\"cbor-test:a1\":\"val1\"},\"id\":\"a\",\"tags\":[\"t1\",\"t2\"],"
485+
"\"@tags\":[{\"cbor-test:a2\":1},{\"cbor-test:a2\":2}]}]}"
486+
},
487+
};
488+
489+
/* {"cbor-test:anydata-payload": {"foo": {"@": {"cbor-test:a1": "val1"}, "cbor-test:ll-str": ["a", "b"],
490+
* "@cbor-test:ll-str": [{"cbor-test:a2": 1}, {"cbor-test:a2": 2}]}}} */
491+
const char data1[] = "\xa1\x78\x19" "cbor-test:anydata-payload" "\xa1\x63" "foo" "\xa3"
492+
"\x61" "@" "\xa1\x6c" "cbor-test:a1" "\x64" "val1"
493+
"\x70" "cbor-test:ll-str" "\x82\x61" "a" "\x61" "b"
494+
"\x71" "@cbor-test:ll-str" "\x82\xa1\x6c" "cbor-test:a2" "\x01\xa1\x6c" "cbor-test:a2" "\x02";
495+
496+
/* {"cbor-test:ll-intg32": ["x"], "@cbor-test:ll-intg32": [null, null]} */
497+
const char data2[] = "\xa2\x73" "cbor-test:ll-intg32" "\x81\x61" "x" "\x74" "@cbor-test:ll-intg32" "\x82\xf6\xf6";
498+
499+
for (i = 0; i < sizeof tests / sizeof *tests; ++i) {
500+
CHECK_PARSE_LYD_PARAM(tests[i].json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
501+
502+
assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buffer, 0, &out));
503+
assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_CBOR, 0));
504+
lyd_free_all(tree);
505+
506+
assert_int_equal(LY_SUCCESS, lyd_parse_data_mem_len(UTEST_LYCTX, buffer, (uint32_t)ly_out_printed(out), LYD_CBOR,
507+
0, LYD_VALIDATE_PRESENT, &tree));
508+
ly_out_free(out, NULL, 0);
509+
free(buffer);
510+
511+
assert_int_equal(LY_SUCCESS, lyd_print_mem(&json, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_SIBLINGS));
512+
assert_string_equal(json, tests[i].json);
513+
free(json);
514+
lyd_free_all(tree);
515+
}
516+
517+
/* anydata content is parsed as opaque nodes, the "@" metadata belong to "foo" */
518+
CHECK_PARSE_LYD_PARAM_LEN(data1, sizeof data1 - 1, LYD_CBOR, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
519+
lyd_free_all(tree);
520+
521+
/* opaque leaf-list instance */
522+
CHECK_PARSE_LYD_PARAM_LEN(data2, sizeof data2 - 1, LYD_CBOR, LYD_PARSE_ONLY | LYD_PARSE_OPAQ, 0, LY_EVALID, tree);
523+
CHECK_LOG_CTX("Missing CBOR data instance #2 of cbor-test:ll-intg32 to be coupled with metadata.",
524+
"/cbor-test:ll-intg32", 1);
525+
}
526+
467527
static void
468528
test_invalid_input(void **state)
469529
{
@@ -651,6 +711,7 @@ main(void)
651711
UTEST(test_opaque, setup),
652712
UTEST(test_opaque_list, setup),
653713
UTEST(test_attributes, setup),
714+
UTEST(test_attributes_parent, setup),
654715
UTEST(test_invalid_input, setup),
655716
UTEST(test_negint, setup),
656717
UTEST(test_anydata_strict, setup),

0 commit comments

Comments
 (0)