From 2d3b40dce68d9aeebba162fcb9997960b39c631d Mon Sep 17 00:00:00 2001 From: Christian Hopps Date: Wed, 14 Feb 2024 17:32:04 -0500 Subject: [PATCH] lib: actually create the tree for the conversion Before this fix would always return empty results b/c there was no libyang tree to print to output format. Signed-off-by: Christian Hopps (cherry picked from commit dff28248c3c1dee0d1c9f9225dab66224c6aac54) --- lib/yang.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/yang.c b/lib/yang.c index 8a2abfaf01..3ce24a318d 100644 --- a/lib/yang.c +++ b/lib/yang.c @@ -795,23 +795,38 @@ char *yang_convert_lyd_format(const uint8_t *data, size_t data_len, LYD_FORMAT out_format, bool shrink) { struct lyd_node *tree = NULL; - uint8_t *result = NULL; uint32_t options = LYD_PRINT_WD_EXPLICIT | LYD_PRINT_WITHSIBLINGS; + uint8_t *result = NULL; + LY_ERR err; assert(out_format != LYD_LYB); - if (!MGMT_MSG_VALIDATE_NUL_TERM(data, data_len)) + if (in_format != LYD_LYB && !MGMT_MSG_VALIDATE_NUL_TERM(data, data_len)) { + zlog_err("Corrupt input data, no NUL terminating byte"); return NULL; + } if (in_format == out_format) return darr_strdup((const char *)data); + err = lyd_parse_data_mem(ly_native_ctx, (const char *)data, in_format, + LYD_PARSE_ONLY, 0, &tree); + + if (err) { + flog_err_sys(EC_LIB_LIBYANG, + "cannot parse input data to convert: %s", + ly_last_errmsg()); + return NULL; + } + if (shrink) options |= LYD_PRINT_SHRINK; /* Take a guess at the initial capacity based on input data size */ darr_ensure_cap(result, data_len); - if (yang_print_tree_append(&result, tree, out_format, options)) { + err = yang_print_tree_append(&result, tree, out_format, options); + lyd_free_all(tree); + if (err) { darr_free(result); return NULL; } -- 2.39.5