Skip to content

Commit 5ce3080

Browse files
committed
schema tree BUGFIX freeing nodes with no module set
Context must be passed explicitly for these cases. Fixes #1452
1 parent 59a0bff commit 5ce3080

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

src/parser_yin.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4213,7 +4213,7 @@ read_yin_case(struct lys_module *module, struct lys_node *parent, struct lyxml_e
42134213
while (root.child) {
42144214
lyxml_free(ctx, root.child);
42154215
}
4216-
lys_node_free(retval, NULL, 0);
4216+
lys_node_free(ctx, retval, NULL, 0);
42174217

42184218
return NULL;
42194219
}
@@ -4420,7 +4420,7 @@ read_yin_choice(struct lys_module *module, struct lys_node *parent, struct lyxml
44204420

44214421
error:
44224422
lyxml_free(ctx, dflt);
4423-
lys_node_free(retval, NULL, 0);
4423+
lys_node_free(ctx, retval, NULL, 0);
44244424
return NULL;
44254425
}
44264426

@@ -4581,7 +4581,7 @@ read_yin_anydata(struct lys_module *module, struct lys_node *parent, struct lyxm
45814581
return retval;
45824582

45834583
error:
4584-
lys_node_free(retval, NULL, 0);
4584+
lys_node_free(ctx, retval, NULL, 0);
45854585
return NULL;
45864586
}
45874587

@@ -4803,7 +4803,7 @@ read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_e
48034803
return retval;
48044804

48054805
error:
4806-
lys_node_free(retval, NULL, 0);
4806+
lys_node_free(ctx, retval, NULL, 0);
48074807
return NULL;
48084808
}
48094809

@@ -5117,7 +5117,7 @@ read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyx
51175117
return retval;
51185118

51195119
error:
5120-
lys_node_free(retval, NULL, 0);
5120+
lys_node_free(ctx, retval, NULL, 0);
51215121
return NULL;
51225122
}
51235123

@@ -5490,7 +5490,7 @@ read_yin_list(struct lys_module *module, struct lys_node *parent, struct lyxml_e
54905490

54915491
error:
54925492

5493-
lys_node_free(retval, NULL, 0);
5493+
lys_node_free(ctx, retval, NULL, 0);
54945494
while (root.child) {
54955495
lyxml_free(ctx, root.child);
54965496
}
@@ -5714,7 +5714,7 @@ read_yin_container(struct lys_module *module, struct lys_node *parent, struct ly
57145714
return retval;
57155715

57165716
error:
5717-
lys_node_free(retval, NULL, 0);
5717+
lys_node_free(ctx, retval, NULL, 0);
57185718
while (root.child) {
57195719
lyxml_free(ctx, root.child);
57205720
}
@@ -5859,7 +5859,7 @@ read_yin_grouping(struct lys_module *module, struct lys_node *parent, struct lyx
58595859
return retval;
58605860

58615861
error:
5862-
lys_node_free(retval, NULL, 0);
5862+
lys_node_free(ctx, retval, NULL, 0);
58635863
while (root.child) {
58645864
lyxml_free(ctx, root.child);
58655865
}
@@ -6035,7 +6035,7 @@ read_yin_input_output(struct lys_module *module, struct lys_node *parent, struct
60356035
return retval;
60366036

60376037
error:
6038-
lys_node_free(retval, NULL, 0);
6038+
lys_node_free(ctx, retval, NULL, 0);
60396039
while (root.child) {
60406040
lyxml_free(ctx, root.child);
60416041
}
@@ -6216,7 +6216,7 @@ read_yin_notif(struct lys_module *module, struct lys_node *parent, struct lyxml_
62166216
return retval;
62176217

62186218
error:
6219-
lys_node_free(retval, NULL, 0);
6219+
lys_node_free(ctx, retval, NULL, 0);
62206220
while (root.child) {
62216221
lyxml_free(ctx, root.child);
62226222
}
@@ -6368,7 +6368,7 @@ read_yin_rpc_action(struct lys_module *module, struct lys_node *parent, struct l
63686368
return retval;
63696369

63706370
error:
6371-
lys_node_free(retval, NULL, 0);
6371+
lys_node_free(ctx, retval, NULL, 0);
63726372
while (root.child) {
63736373
lyxml_free(ctx, root.child);
63746374
}
@@ -6522,7 +6522,7 @@ read_yin_uses(struct lys_module *module, struct lys_node *parent, struct lyxml_e
65226522
return retval;
65236523

65246524
error:
6525-
lys_node_free(retval, NULL, 0);
6525+
lys_node_free(ctx, retval, NULL, 0);
65266526
return NULL;
65276527
}
65286528

src/resolve.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5654,7 +5654,7 @@ resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
56545654

56555655
fail:
56565656
LY_TREE_FOR_SAFE(uses->child, next, iter) {
5657-
lys_node_free(iter, NULL, 0);
5657+
lys_node_free(ctx, iter, NULL, 0);
56585658
}
56595659
free(refine_nodes);
56605660
return -1;

src/tree_internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,14 @@ void lys_node_unlink(struct lys_node *node);
368368
/**
369369
* @brief Free the schema node structure, includes unlinking it from the tree
370370
*
371+
* @param[in] ctx libang context to use, @p node may not have it filled (in groupings, for example).
371372
* @param[in] node Schema tree node to free. Do not use the pointer after calling this function.
372373
* @param[in] private_destructor Optional destructor function for private objects assigned
373374
* to the nodes via lys_set_private(). If NULL, the private objects are not freed by libyang.
374375
* @param[in] shallow Whether to do a shallow free only (on a shallow copy of a node).
375376
*/
376-
void lys_node_free(struct lys_node *node, void (*private_destructor)(const struct lys_node *node, void *priv), int shallow);
377+
void lys_node_free(struct ly_ctx *ctx, struct lys_node *node,
378+
void (*private_destructor)(const struct lys_node *node, void *priv), int shallow);
377379

378380
/**
379381
* @brief Free (and unlink it from the context) the specified schema.

src/tree_schema.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys
942942
iter->next = NULL;
943943
iter->prev = iter;
944944
iter->parent = NULL;
945-
lys_node_free(iter, NULL, 0);
945+
lys_node_free(ctx, iter, NULL, 0);
946946
} else {
947947
if (shortcase) {
948948
/* create the implicit case to allow it to serve as a target of the augments,
@@ -2464,7 +2464,7 @@ lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug,
24642464
/* children from a resolved augment are freed under the target node */
24652465
if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) {
24662466
LY_TREE_FOR_SAFE(aug->child, next, sub) {
2467-
lys_node_free(sub, private_destructor, 0);
2467+
lys_node_free(ctx, sub, private_destructor, 0);
24682468
}
24692469
}
24702470

@@ -2722,11 +2722,11 @@ lys_deviation_free(struct lys_module *module, struct lys_deviation *dev,
27222722

27232723
LY_TREE_DFS_END(dev->orig_node, next, elem);
27242724
}
2725-
lys_node_free(dev->orig_node, NULL, 0);
2725+
lys_node_free(ctx, dev->orig_node, NULL, 0);
27262726
} else {
27272727
/* it's just a shallow copy, freeing one node */
27282728
dev->orig_node->module = module;
2729-
lys_node_free(dev->orig_node, NULL, 1);
2729+
lys_node_free(ctx, dev->orig_node, NULL, 1);
27302730
}
27312731
}
27322732

@@ -2798,20 +2798,15 @@ lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses,
27982798
}
27992799

28002800
void
2801-
lys_node_free(struct lys_node *node, void (*private_destructor)(const struct lys_node *node, void *priv), int shallow)
2801+
lys_node_free(struct ly_ctx *ctx, struct lys_node *node,
2802+
void (*private_destructor)(const struct lys_node *node, void *priv), int shallow)
28022803
{
2803-
struct ly_ctx *ctx;
28042804
struct lys_node *sub, *next;
28052805

28062806
if (!node) {
28072807
return;
28082808
}
28092809

2810-
assert(node->module);
2811-
assert(node->module->ctx);
2812-
2813-
ctx = node->module->ctx;
2814-
28152810
/* remove private object */
28162811
if (node->priv && private_destructor) {
28172812
private_destructor(node, node->priv);
@@ -2827,7 +2822,7 @@ lys_node_free(struct lys_node *node, void (*private_destructor)(const struct lys
28272822

28282823
if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
28292824
LY_TREE_FOR_SAFE(node->child, next, sub) {
2830-
lys_node_free(sub, private_destructor, 0);
2825+
lys_node_free(ctx, sub, private_destructor, 0);
28312826
}
28322827
}
28332828

@@ -2942,7 +2937,7 @@ module_free_common(struct lys_module *module, void (*private_destructor)(const s
29422937
* are placed in the main module altogether */
29432938
if (!module->type) {
29442939
LY_TREE_FOR_SAFE(module->data, next, iter) {
2945-
lys_node_free(iter, private_destructor, 0);
2940+
lys_node_free(ctx, iter, private_destructor, 0);
29462941
}
29472942
}
29482943

@@ -3507,7 +3502,7 @@ lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const
35073502
return retval;
35083503

35093504
error:
3510-
lys_node_free(retval, NULL, 0);
3505+
lys_node_free(ctx, retval, NULL, 0);
35113506
return NULL;
35123507
}
35133508

@@ -5149,7 +5144,7 @@ lys_submodule_module_data_free(struct lys_submodule *submodule)
51495144
/* remove parsed data */
51505145
LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
51515146
if (elem->module == (struct lys_module *)submodule) {
5152-
lys_node_free(elem, NULL, 0);
5147+
lys_node_free(submodule->ctx, elem, NULL, 0);
51535148
}
51545149
}
51555150
}
@@ -5546,7 +5541,7 @@ lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, un
55465541
case LY_STMT_USES:
55475542
pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
55485543
LY_TREE_FOR_SAFE((struct lys_node *)(*pp), snext, siter) {
5549-
lys_node_free(siter, NULL, 0);
5544+
lys_node_free(ctx, siter, NULL, 0);
55505545
}
55515546
*pp = NULL;
55525547
break;

0 commit comments

Comments
 (0)