[ldns-users] contrib/python: memory leak in ldns_pkt.new_query

Johannes Naab jn at stusta.de
Tue Aug 6 17:59:32 UTC 2013


Hi,

The python wrapper for ldns appears to leak memory when packets are
created by ldns_pkt.new_query.

The following is a short example (tested in ldns-1.6.16, Python 2.7.5
Linux 3.10, amd64):
#!/usr/bin/env python2.7
import ldns

dname = ldns.ldns_dname("test.nic.cz")

def leak():
    pkt = ldns.ldns_pkt.new_query(dname, ldns.LDNS_RR_TYPE_A,
ldns.LDNS_RR_CLASS_IN, 0)

while True:
    leak()


Root of the problem seems to be, that the pointer to the ldns_pkt struct
is not considered owned by the swig wrapper, and thus the pkt struct is
not freed upon garbage collection in python.

import ldns
dname = ldns.ldns_dname("test.nic.cz")
pkt = ldns.ldns_pkt.new_query(dname, ldns.LDNS_RR_TYPE_A,
                              ldns.LDNS_RR_CLASS_IN, 0)
print pkt.thisown

=> False.

If new_query is replaced by new_query_frm_str the packet is "owned" by
the swig wrapper, and the packet does not leak.


pkt = ldns.ldns_pkt.new_query_frm_str(str(dname), ldns.LDNS_RR_TYPE_A,
                                      ldns.LDNS_RR_CLASS_IN, 0)
print pkt.thisown

=> True

I tried to convince swig that the struct should be owned by the wrapper
by inserting a "%newobject ldns_pkt_query_new;" below line 44 in
contrib/python/ldns_packet.i. This made swig own the pointer, but
freeing the packet on garbage collection made python crash (memory
corruption, double free). My knowledge and understanding of swig is limited.


Johannes



More information about the ldns-users mailing list