[nft PATCH 04/12] segtree: Fix memory leaks

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This fixes memory leaks in three places:

* set_overlap():

The allocated intervals have to be freed again before returning to
caller. While being at it, reduce indenting level in said function to
stay below 80 columns boundary.

* range_is_prefix():
* interval_map_decompose():

GMP documentation suggests to call mpz_clear() for all mpz_t type
variables once they are not used anymore to free the space they occupy.

Signed-off-by: Phil Sutter <phil@xxxxxx>
---
 src/segtree.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/src/segtree.c b/src/segtree.c
index f8d62d7a24ce4..1970265a9f11c 100644
--- a/src/segtree.c
+++ b/src/segtree.c
@@ -359,20 +359,29 @@ static int set_overlap(struct list_head *msgs, const struct set *set,
 	struct elementary_interval *new_intervals[init->size];
 	struct elementary_interval *intervals[set->init->size];
 	unsigned int n, m, i, j;
+	int ret = 0;
 
 	n = expr_to_intervals(init, keylen, new_intervals);
 	m = expr_to_intervals(set->init, keylen, intervals);
 
 	for (i = 0; i < n; i++) {
 		for (j = 0; j < m; j++) {
-			if (interval_overlap(new_intervals[i], intervals[j]))
-				return expr_error(msgs,
-					new_intervals[i]->expr,
-					"interval overlaps with an existing one");
+			if (!interval_overlap(new_intervals[i], intervals[j]))
+				continue;
+
+			expr_error(msgs, new_intervals[i]->expr,
+				   "interval overlaps with an existing one");
+			ret = -1;
+			goto out;
 		}
 	}
+out:
+	for (i = 0; i < n; i++)
+		ei_destroy(new_intervals[i]);
+	for (i = 0; i < m; i++)
+		ei_destroy(intervals[i]);
 
-	return 0;
+	return ret;
 }
 
 static int set_to_segtree(struct list_head *msgs, struct set *set,
@@ -695,11 +704,14 @@ void get_set_decompose(struct table *table, struct set *set)
 static bool range_is_prefix(const mpz_t range)
 {
 	mpz_t tmp;
+	bool ret;
 
 	mpz_init_set(tmp, range);
 	mpz_add_ui(tmp, tmp, 1);
 	mpz_and(tmp, range, tmp);
-	return !mpz_cmp_ui(tmp, 0);
+	ret = !mpz_cmp_ui(tmp, 0);
+	mpz_clear(tmp);
+	return ret;
 }
 
 static struct expr *expr_value(struct expr *expr)
@@ -860,6 +872,9 @@ void interval_map_decompose(struct expr *set)
 		compound_expr_add(set, i);
 	}
 
+	mpz_clear(range);
+	mpz_clear(p);
+
 	xfree(ranges);
 	xfree(elements);
 }
-- 
2.16.1

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux