Release the array of intervals and the segtree in case of error, otherwise these structures and objects are never released: SUMMARY: AddressSanitizer: 2864 byte(s) leaked in 37 allocation(s). Moreover, improve existing a test coverage of this error path. Signed-off-by: Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx> --- src/segtree.c | 16 ++++++++++++++-- tests/py/inet/ip.t | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/segtree.c b/src/segtree.c index 5eaf684578bf..f721954f76eb 100644 --- a/src/segtree.c +++ b/src/segtree.c @@ -288,6 +288,7 @@ out: return 0; err: + mpz_clear(p); errno = EEXIST; if (new->expr->etype == EXPR_MAPPING) { new_expr = new->expr->left; @@ -437,7 +438,7 @@ static int set_to_segtree(struct list_head *msgs, struct set *set, { struct elementary_interval **intervals; struct expr *i, *next; - unsigned int n; + unsigned int n, m; int err = 0; /* We are updating an existing set with new elements, check if the new @@ -467,8 +468,19 @@ static int set_to_segtree(struct list_head *msgs, struct set *set, */ for (n = 0; n < init->size; n++) { err = ei_insert(msgs, tree, intervals[n], merge); - if (err < 0) + if (err < 0) { + struct elementary_interval *ei; + struct rb_node *node, *next; + + for (m = n; m < init->size; m++) + ei_destroy(intervals[m]); + + rb_for_each_entry_safe(ei, node, next, &tree->root, rb_node) { + ei_remove(tree, ei); + ei_destroy(ei); + } break; + } } xfree(intervals); diff --git a/tests/py/inet/ip.t b/tests/py/inet/ip.t index 86604a6363dd..ac5b825e4a34 100644 --- a/tests/py/inet/ip.t +++ b/tests/py/inet/ip.t @@ -8,4 +8,4 @@ ip saddr . ip daddr . ether saddr { 1.1.1.1 . 2.2.2.2 . ca:fe:ca:fe:ca:fe };ok ip saddr vmap { 10.0.1.0-10.0.1.255 : accept, 10.0.1.1-10.0.2.255 : drop };fail -ip saddr vmap { 1.1.1.1-1.1.1.255 : accept, 1.1.1.0-1.1.2.1 : drop};fail +ip saddr vmap { 3.3.3.3-3.3.3.4 : accept, 1.1.1.1-1.1.1.255 : accept, 1.1.1.0-1.1.2.1 : drop};fail -- 2.20.1