Skip to content

Commit

Permalink
lib: fix dnode_create to use correct libyang function.
Browse files Browse the repository at this point in the history
The previous use of `lyd_new_path()` returns the first node created, rather
than the xpath target node. The code is lucky in the sense that it is
normally only creating a single node rather than a branch. Fix this to
use `lyd_new_path2()` which returns the target node to actually implement
the semantics expected by callers of `dnode_create()` (i.e., returning the
newly created target node).

Signed-off-by: Christian Hopps <[email protected]>
  • Loading branch information
choppsv1 committed Jan 18, 2025
1 parent 3c3b559 commit 6066674
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/northbound.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,19 +685,30 @@ void nb_config_diff(const struct nb_config *config1,
lyd_free_all(diff);
}

static int dnode_create(struct nb_config *candidate, const char *xpath,
const char *value, uint32_t options,
struct lyd_node **new_dnode)
/**
* dnode_create() - create a new node in the tree
* @candidate: config tree to create node in.
* @xpath: target node to create.
* @value: value for the new if required.
* @options: lyd_new_path options
* @new_dnode: the newly created node. If options includes LYD_NEW_PATH_UPDATE,
* and the node exists (i.e., isn't create but updated), then
* new_node will be set to NULL not the existing node).
*
* Return: NB_OK or NB_ERR.
*/
static LY_ERR dnode_create(struct nb_config *candidate, const char *xpath, const char *value,
uint32_t options, struct lyd_node **new_dnode)
{
struct lyd_node *dnode;
LY_ERR err;

err = lyd_new_path(candidate->dnode, ly_native_ctx, xpath, value,
options, &dnode);
err = lyd_new_path2(candidate->dnode, ly_native_ctx, xpath, value, 0, 0, options, NULL,
&dnode);
if (err) {
flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path(%s) failed: %d",
__func__, xpath, err);
return NB_ERR;
return err;
} else if (dnode) {
err = lyd_new_implicit_tree(dnode, LYD_IMPLICIT_NO_STATE, NULL);
if (err) {
Expand All @@ -708,7 +719,7 @@ static int dnode_create(struct nb_config *candidate, const char *xpath,
}
if (new_dnode)
*new_dnode = dnode;
return NB_OK;
return LY_SUCCESS;
}

int nb_candidate_edit(struct nb_config *candidate, const struct nb_node *nb_node,
Expand Down

0 comments on commit 6066674

Please sign in to comment.