[ldns-users] ldns_resolver_pop_nameserver and ldns_resolver_deep_free

Willem Toorop willem at nlnetlabs.nl
Tue Aug 21 08:46:32 UTC 2012


Hi Marius,

I have fixed it by testing for ns_count == 1 in
ldns_resolver_pop_nameserver which I believe is more understandable at
first sight (you don't need to know realloc with size 0 equals free :).
Thanks for noticing and reporting!

-- Willem


Index: resolver.c
===================================================================
--- resolver.c	(revision 3708)
+++ resolver.c	(working copy)
@@ -253,13 +253,20 @@

 	pop = nameservers[ns_count - 1];

-	nameservers = LDNS_XREALLOC(nameservers, ldns_rdf *, (ns_count - 1));
-	rtt = LDNS_XREALLOC(rtt, size_t, (ns_count - 1));
+	if (ns_count == 1) {
+		LDNS_FREE(nameservers);
+		LDNS_FREE(rtt);

-        if(nameservers)
+		ldns_resolver_set_nameservers(r, NULL);
+		ldns_resolver_set_rtt(r, NULL);
+	} else {
+		nameservers = LDNS_XREALLOC(nameservers, ldns_rdf *,
+				(ns_count - 1));
+		rtt = LDNS_XREALLOC(rtt, size_t, (ns_count - 1));
+
 	        ldns_resolver_set_nameservers(r, nameservers);
-        if(rtt)
 	        ldns_resolver_set_rtt(r, rtt);
+	}
 	/* decr the count */
 	ldns_resolver_dec_nameserver_count(r);
 	return pop;


Op 26-07-12 09:58, Marius Rieder schreef:
> Hi,
> 
> If i remove all nameserver with ldns_resolver_pop_nameserver and
> do a ldns_resolver_deep_free later. Ldns double frees the
> r->_nameservers and r->_rtt.
> 
> ldns_resolver_pop_nameserver reallocs [1]boath with size 0, which is a
> equivalent to free. Which is perfect fine, but do not update the pointer
> in the ldns_resolver.
> 
> [1]http://www.nlnetlabs.nl/projects/ldns/doc/resolver_8c_source.html#l00256
> 
> As realloc could return NULL in case of a failed realloc aswell, it may
> be nessesary to check the ns_count eigther in
> ldns_resolver_pop_nameserver or ldns_resolver_deep_free.
> 
>  - Marius
> 




More information about the ldns-users mailing list