Skip to content

Commit d7e8c53

Browse files
committed
schema compile amend BUGFIX memory leak
1 parent 21459b2 commit d7e8c53

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

src/schema_compile_amend.c

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -659,11 +659,10 @@ lysp_node_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct l
659659
* @return LY_ERR value.
660660
*/
661661
static LY_ERR
662-
lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_module *pmod, const struct lysp_node *pnode,
663-
ly_bool with_links, struct lysp_node **dup_p)
662+
lysp_dup_single(struct lysc_ctx *cctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p)
664663
{
665664
LY_ERR ret = LY_SUCCESS;
666-
void *mem = NULL;
665+
struct lysp_node *dup = NULL;
667666

668667
if (!pnode) {
669668
*dup_p = NULL;
@@ -672,63 +671,63 @@ lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_module *pmod, const
672671

673672
switch (pnode->nodetype) {
674673
case LYS_CONTAINER:
675-
mem = calloc(1, sizeof(struct lysp_node_container));
674+
dup = calloc(1, sizeof(struct lysp_node_container));
676675
break;
677676
case LYS_LEAF:
678-
mem = calloc(1, sizeof(struct lysp_node_leaf));
677+
dup = calloc(1, sizeof(struct lysp_node_leaf));
679678
break;
680679
case LYS_LEAFLIST:
681-
mem = calloc(1, sizeof(struct lysp_node_leaflist));
680+
dup = calloc(1, sizeof(struct lysp_node_leaflist));
682681
break;
683682
case LYS_LIST:
684-
mem = calloc(1, sizeof(struct lysp_node_list));
683+
dup = calloc(1, sizeof(struct lysp_node_list));
685684
break;
686685
case LYS_CHOICE:
687-
mem = calloc(1, sizeof(struct lysp_node_choice));
686+
dup = calloc(1, sizeof(struct lysp_node_choice));
688687
break;
689688
case LYS_CASE:
690-
mem = calloc(1, sizeof(struct lysp_node_case));
689+
dup = calloc(1, sizeof(struct lysp_node_case));
691690
break;
692691
case LYS_ANYDATA:
693692
case LYS_ANYXML:
694-
mem = calloc(1, sizeof(struct lysp_node_anydata));
693+
dup = calloc(1, sizeof(struct lysp_node_anydata));
695694
break;
696695
case LYS_INPUT:
697696
case LYS_OUTPUT:
698-
mem = calloc(1, sizeof(struct lysp_node_action_inout));
697+
dup = calloc(1, sizeof(struct lysp_node_action_inout));
699698
break;
700699
case LYS_ACTION:
701700
case LYS_RPC:
702-
mem = calloc(1, sizeof(struct lysp_node_action));
701+
dup = calloc(1, sizeof(struct lysp_node_action));
703702
break;
704703
case LYS_NOTIF:
705-
mem = calloc(1, sizeof(struct lysp_node_notif));
704+
dup = calloc(1, sizeof(struct lysp_node_notif));
706705
break;
707706
default:
708-
LOGINT_RET(ctx);
707+
LOGINT_RET(cctx->ctx);
709708
}
710-
LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
711-
LY_CHECK_GOTO(ret = lysp_node_dup(ctx, pmod, mem, pnode), cleanup);
709+
LY_CHECK_ERR_GOTO(!dup, LOGMEM(cctx->ctx); ret = LY_EMEM, cleanup);
710+
LY_CHECK_GOTO(ret = lysp_node_dup(cctx->ctx, cctx->pmod, dup, pnode), cleanup);
712711

713712
if (with_links) {
714713
/* copy also parent, child, action, and notification pointers */
715-
((struct lysp_node *)mem)->parent = pnode->parent;
714+
dup->parent = pnode->parent;
716715
switch (pnode->nodetype) {
717716
case LYS_CONTAINER:
718-
((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
719-
((struct lysp_node_container *)mem)->actions = ((struct lysp_node_container *)pnode)->actions;
720-
((struct lysp_node_container *)mem)->notifs = ((struct lysp_node_container *)pnode)->notifs;
717+
((struct lysp_node_container *)dup)->child = ((struct lysp_node_container *)pnode)->child;
718+
((struct lysp_node_container *)dup)->actions = ((struct lysp_node_container *)pnode)->actions;
719+
((struct lysp_node_container *)dup)->notifs = ((struct lysp_node_container *)pnode)->notifs;
721720
break;
722721
case LYS_LIST:
723-
((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
724-
((struct lysp_node_list *)mem)->actions = ((struct lysp_node_list *)pnode)->actions;
725-
((struct lysp_node_list *)mem)->notifs = ((struct lysp_node_list *)pnode)->notifs;
722+
((struct lysp_node_list *)dup)->child = ((struct lysp_node_list *)pnode)->child;
723+
((struct lysp_node_list *)dup)->actions = ((struct lysp_node_list *)pnode)->actions;
724+
((struct lysp_node_list *)dup)->notifs = ((struct lysp_node_list *)pnode)->notifs;
726725
break;
727726
case LYS_CHOICE:
728-
((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
727+
((struct lysp_node_choice *)dup)->child = ((struct lysp_node_choice *)pnode)->child;
729728
break;
730729
case LYS_CASE:
731-
((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
730+
((struct lysp_node_case *)dup)->child = ((struct lysp_node_case *)pnode)->child;
732731
break;
733732
default:
734733
break;
@@ -737,9 +736,9 @@ lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_module *pmod, const
737736

738737
cleanup:
739738
if (ret) {
740-
free(mem);
739+
lysp_dev_node_free(cctx, dup);
741740
} else {
742-
*dup_p = mem;
741+
*dup_p = dup;
743742
}
744743
return ret;
745744
}
@@ -1766,7 +1765,7 @@ lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node
17661765

17671766
if (!*dev_pnode) {
17681767
/* first refine on this node, create a copy first */
1769-
LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, ctx->pmod, pnode, 1, dev_pnode), cleanup);
1768+
LY_CHECK_GOTO(ret = lysp_dup_single(ctx, pnode, 1, dev_pnode), cleanup);
17701769
}
17711770

17721771
/* apply all the refines by changing (the copy of) the parsed node */
@@ -1797,7 +1796,7 @@ lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node
17971796

17981797
if (!*dev_pnode) {
17991798
/* first deviation on this node, create a copy first */
1800-
LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, ctx->pmod, pnode, 1, dev_pnode), cleanup);
1799+
LY_CHECK_GOTO(ret = lysp_dup_single(ctx, pnode, 1, dev_pnode), cleanup);
18011800
}
18021801

18031802
/* apply all the deviates by changing (the copy of) the parsed node */

0 commit comments

Comments
 (0)