[ldns-users] [Patch] Memory leak in ldns_pkt_rr_list_by_name()

Johannes Naab naab at net.in.tum.de
Thu Sep 4 18:42:46 UTC 2014


Hi,

ldns_pkt_rr_list_by_name() is as found by valgrind leaking memory by
first copying the necessary sections and never freeing the resulting
list.

ldns_pkt_rr_list_by_type() and ldns_pkt_rr_list_by_name_and_type() do
properly free the intermediate copy.

The patch fixes the problem the same way as ldns_pkt_rr_list_by_type()
and ldns_pkt_rr_list_by_name_and_type() are dealing with it.


An alternative approach avoiding some ldns_rr_clone() calls would be to
free all non-matching RR already in the for loop and using only
ldns_rr_list_free() at the end.


Regards,
Johannes
-------------- next part --------------
>From dd64bfe840755d18c675bb2cc13c1adaaec8205e Mon Sep 17 00:00:00 2001
From: Johannes Naab <naab at net.in.tum.de>
Date: Thu, 4 Sep 2014 19:49:36 +0200
Subject: [PATCH] Fix memory leak in ldns_pkt_rr_list_by_name()

The intermediate copy for section selection wasn't freed.
---
 packet.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/packet.c b/packet.c
index 0ac64c5..659156a 100644
--- a/packet.c
+++ b/packet.c
@@ -273,9 +273,15 @@ ldns_pkt_rr_list_by_name(ldns_pkt *packet,
 			if (ret == NULL) {
 				ret = ldns_rr_list_new();
 			}
-			ldns_rr_list_push_rr(ret, ldns_rr_list_rr(rrs, i));
+			ldns_rr_list_push_rr(ret,
+					     ldns_rr_clone(
+						ldns_rr_list_rr(rrs, i))
+					    );
 		}
 	}
+
+	ldns_rr_list_deep_free(rrs);
+
 	return ret;
 }
 
-- 
1.7.10.4



More information about the ldns-users mailing list