Skip to content

Commit e2667c9

Browse files
affan-archmichalvasko
authored andcommitted
printer cbor BUGFIX double free when printing opaque lists
1 parent c081b5f commit e2667c9

2 files changed

Lines changed: 60 additions & 21 deletions

File tree

‎src/printer_cbor.c‎

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,15 +1148,15 @@ cbor_print_opaq(struct cborpr_ctx *pctx, const struct lyd_node_opaq *node, cbor_
11481148
}
11491149

11501150
if (first) {
1151-
key = cbor_print_member_name2(pctx, pctx->parent, node->format, &node->name, 0);
1152-
LY_CHECK_RET(!key, LY_EMEM);
1153-
11541151
if (hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST)) {
1152+
/* start new array, the member name is generated once the array is completed */
11551153
cbor_item_t *new_array = cbor_new_indefinite_array();
11561154

1157-
LY_CHECK_ERR_RET(!new_array, free(key), LY_EMEM);
1158-
LY_CHECK_ERR_RET(cbor_print_array_open(pctx, &node->node, new_array),
1159-
cbor_decref(&new_array); free(key), LY_EINVAL);
1155+
LY_CHECK_RET(!new_array, LY_EMEM);
1156+
LY_CHECK_ERR_RET(cbor_print_array_open(pctx, &node->node, new_array), cbor_decref(&new_array), LY_EINVAL);
1157+
} else {
1158+
key = cbor_print_member_name2(pctx, pctx->parent, node->format, &node->name, 0);
1159+
LY_CHECK_RET(!key, LY_EMEM);
11601160
}
11611161
}
11621162

@@ -1238,25 +1238,29 @@ cbor_print_opaq(struct cborpr_ctx *pctx, const struct lyd_node_opaq *node, cbor_
12381238
}
12391239

12401240
if (last && (hints & (LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST))) {
1241-
if (key) {
1242-
struct cbor_pair pair = {
1243-
.key = cbor_move(cbor_build_string(key)),
1244-
.value = cbor_move(cbor_current_array(pctx))
1245-
};
1241+
cbor_item_t *array = cbor_current_array(pctx);
12461242

1247-
free(key);
1248-
1249-
if (!cbor_map_add(parent_map, pair)) {
1250-
cbor_decref(&pair.key);
1251-
cbor_decref(&pair.value);
1252-
return LY_EMEM;
1253-
}
1243+
key = cbor_print_member_name2(pctx, pctx->parent, node->format, &node->name, 0);
1244+
if (!key) {
1245+
cbor_decref(&array);
1246+
cbor_print_array_close(pctx);
1247+
return LY_EMEM;
12541248
}
1255-
cbor_print_array_close(pctx);
1256-
}
12571249

1258-
if (key) {
1250+
/* add completed array to parent map */
1251+
struct cbor_pair pair = {
1252+
.key = cbor_move(cbor_build_string(key)),
1253+
.value = cbor_move(array)
1254+
};
1255+
12591256
free(key);
1257+
1258+
if (!cbor_map_add(parent_map, pair)) {
1259+
cbor_decref(&pair.key);
1260+
cbor_decref(&pair.value);
1261+
return LY_EMEM;
1262+
}
1263+
cbor_print_array_close(pctx);
12601264
}
12611265

12621266
return LY_SUCCESS;

‎tests/utests/data/test_cbor.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,40 @@ test_opaque(void **state)
365365
lyd_free_all(tree);
366366
}
367367

368+
static void
369+
test_opaque_list(void **state)
370+
{
371+
struct lyd_node *tree;
372+
char *buffer, *json;
373+
struct ly_out *out;
374+
size_t i;
375+
376+
/* invalid leaf-list values are stored as opaque nodes, one and more instances */
377+
const struct test_case tests[] = {
378+
{"single", "{\"cbor-test:ll-intg32\":[\"not a number\"]}"},
379+
{"multiple", "{\"cbor-test:ll-intg32\":[\"x\",\"y\",\"z\"]}"},
380+
{"nested", "{\"cbor-test:list-nested\":[{\"id\":\"a\",\"inner\":[{\"iid\":1,\"vals\":[\"q\"]}]}]}"},
381+
};
382+
383+
for (i = 0; i < sizeof tests / sizeof *tests; ++i) {
384+
CHECK_PARSE_LYD_PARAM(tests[i].json, LYD_JSON, LYD_PARSE_ONLY | LYD_PARSE_OPAQ, 0, LY_SUCCESS, tree);
385+
386+
assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buffer, 0, &out));
387+
assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_CBOR, 0));
388+
lyd_free_all(tree);
389+
390+
assert_int_equal(LY_SUCCESS, lyd_parse_data_mem_len(UTEST_LYCTX, buffer, (uint32_t)ly_out_printed(out), LYD_CBOR,
391+
LYD_PARSE_ONLY | LYD_PARSE_OPAQ, 0, &tree));
392+
ly_out_free(out, NULL, 0);
393+
free(buffer);
394+
395+
assert_int_equal(LY_SUCCESS, lyd_print_mem(&json, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_SIBLINGS));
396+
assert_string_equal(json, tests[i].json);
397+
free(json);
398+
lyd_free_all(tree);
399+
}
400+
}
401+
368402
static void
369403
test_attributes(void **state)
370404
{
@@ -437,6 +471,7 @@ main(void)
437471
UTEST(test_operations, setup),
438472
UTEST(test_schema_mount, setup),
439473
UTEST(test_opaque, setup),
474+
UTEST(test_opaque_list, setup),
440475
UTEST(test_attributes, setup),
441476
UTEST(test_invalid_input, setup),
442477
UTEST(test_negint, setup),

0 commit comments

Comments
 (0)