]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
Memory leak fixes by Bedrich Kosata (at the cost of a bit performance).
authorWillem Toorop <willem@NLnetLabs.nl>
Wed, 13 Apr 2011 14:16:52 +0000 (14:16 +0000)
committerWillem Toorop <willem@NLnetLabs.nl>
Wed, 13 Apr 2011 14:16:52 +0000 (14:16 +0000)
Patch was in e-mail:

Message-ID: <4DA546A3.5030600@nic.cz>
Date: Wed, 13 Apr 2011 08:45:55 +0200
From: Bedrich Kosata <bedrich.kosata@nic.cz>
To: ldns-users@open.nlnetlabs.nl
Subject: Re: [ldns-users] pyldns - memory leaks and double freeing

contrib/python/ldns_packet.i
contrib/python/ldns_rdf.i
contrib/python/ldns_rr.i

index 21f31bd011b2f063bc974bf97ec5f3ed43bbc567..a1ad6b7c8214fcb2a37474b04160c7d14cc743ca 100644 (file)
  $result = SWIG_Python_AppendOutput($result, SWIG_NewPointerObj(SWIG_as_voidptr($1_pkt), SWIGTYPE_p_ldns_struct_pkt, SWIG_POINTER_OWN |  0 ));
 }
 
+%newobject ldns_pkt_new;
 %newobject ldns_pkt_clone;
 %newobject ldns_pkt_rr_list_by_type;
 %newobject ldns_pkt_rr_list_by_name_and_type;
 %newobject ldns_pkt_rr_list_by_name;
 %newobject ldns_update_pkt_new;
 
+
 %nodefaultctor ldns_struct_pkt; //no default constructor & destructor
 %nodefaultdtor ldns_struct_pkt;
 
@@ -69,9 +71,57 @@ void _ldns_pkt_free (ldns_pkt* p) {
 %newobject ldns_pkt_algorithm2str;
 %newobject ldns_pkt_cert_algorithm2str;
 
-%exception ldns_pkt_push_rr(ldns_pkt *packet, ldns_pkt_section section, ldns_rr *rr) %{ $action if (result) Py_INCREF(obj2); %}
-%exception ldns_pkt_push_rr_list(ldns_pkt *packet, ldns_pkt_section section, ldns_rr_list *list) %{ $action if (result) Py_INCREF(obj2); %}
 
+/* cloning of packet_lists to make them independent of the original packet */
+
+%newobject _ldns_pkt_additional;
+%newobject _ldns_pkt_answer;
+%newobject _ldns_pkt_authority;
+%newobject _ldns_pkt_question;
+
+%rename(__ldns_pkt_additional) ldns_pkt_additional;
+%inline %{
+ldns_rr_list* _ldns_pkt_additional(ldns_pkt* p) {
+   return ldns_rr_list_clone(ldns_pkt_additional(p));
+}
+%}
+
+%rename(__ldns_pkt_answer) ldns_pkt_answer;
+%inline %{
+ldns_rr_list* _ldns_pkt_answer(ldns_pkt* p) {
+   return ldns_rr_list_clone(ldns_pkt_answer(p));
+}
+%}
+
+%rename(__ldns_pkt_authority) ldns_pkt_authority;
+%inline %{
+ldns_rr_list* _ldns_pkt_authority(ldns_pkt* p) {
+   return ldns_rr_list_clone(ldns_pkt_authority(p));
+}
+%}
+
+%rename(__ldns_pkt_question) ldns_pkt_question;
+%inline %{
+ldns_rr_list* _ldns_pkt_question(ldns_pkt* p) {
+   return ldns_rr_list_clone(ldns_pkt_question(p));
+}
+%}
+
+/* clone data when pushed in */
+
+%rename(__ldns_pkt_push_rr) ldns_pkt_push_rr;
+%inline %{
+bool _ldns_pkt_push_rr(ldns_pkt* p, ldns_pkt_section sec, ldns_rr *rr) {
+   return ldns_pkt_push_rr(p, sec, ldns_rr_clone(rr));
+}
+%}
+
+%rename(__ldns_pkt_push_rr_list) ldns_pkt_push_rr_list;
+%inline %{
+bool _ldns_pkt_push_rr_list(ldns_pkt* p, ldns_pkt_section sec, ldns_rr_list *rrl) {
+   return ldns_pkt_push_rr_list(p, sec, ldns_rr_list_clone(rrl));
+}
+%}
 
 %feature("docstring") ldns_struct_pkt "LDNS packet object. 
 
@@ -224,7 +274,7 @@ This simple example instances a resolver in order to resolve NS for nic.cz.
                
                :returns: (ldns_rr_list \*) the section
             """
-            return _ldns.ldns_pkt_additional(self)
+            return _ldns._ldns_pkt_additional(self)
             #parameters: const ldns_pkt *,
             #retvals: ldns_rr_list *
 
@@ -252,7 +302,7 @@ This simple example instances a resolver in order to resolve NS for nic.cz.
                
                :returns: (ldns_rr_list \*) the section
             """
-            return _ldns.ldns_pkt_answer(self)
+            return _ldns._ldns_pkt_answer(self)
             #parameters: const ldns_pkt *,
             #retvals: ldns_rr_list *
 
@@ -279,7 +329,7 @@ This simple example instances a resolver in order to resolve NS for nic.cz.
                
                :returns: (ldns_rr_list \*) the section
             """
-            return _ldns.ldns_pkt_authority(self)
+            return _ldns._ldns_pkt_authority(self)
             #parameters: const ldns_pkt *,
             #retvals: ldns_rr_list *
 
@@ -433,7 +483,7 @@ This simple example instances a resolver in order to resolve NS for nic.cz.
                    rr to push
                :returns: (bool) a boolean which is true when the rr was added
             """
-            return _ldns.ldns_pkt_push_rr(self,section,rr)
+            return _ldns._ldns_pkt_push_rr(self,section,rr)
             #parameters: ldns_pkt *,ldns_pkt_section,ldns_rr *,
             #retvals: bool
 
@@ -446,7 +496,7 @@ This simple example instances a resolver in order to resolve NS for nic.cz.
                    the rr_list to push
                :returns: (bool) a boolean which is true when the rr was added
             """
-            return _ldns.ldns_pkt_push_rr_list(self,section,list)
+            return _ldns._ldns_pkt_push_rr_list(self,section,list)
             #parameters: ldns_pkt *,ldns_pkt_section,ldns_rr_list *,
             #retvals: bool
 
@@ -482,7 +532,7 @@ This simple example instances a resolver in order to resolve NS for nic.cz.
                
                :returns: (ldns_rr_list \*) the section
             """
-            return _ldns.ldns_pkt_question(self)
+            return _ldns._ldns_pkt_question(self)
             #parameters: const ldns_pkt *,
             #retvals: ldns_rr_list *
 
index 045fae974b85520221039d899448ce252bdee939..aada36b32a52ed41a1455f077ee9da5223af7140 100644 (file)
 %newobject ldns_dname_new_frm_str;
 %newobject ldns_dname_new_frm_data;
 
+%newobject ldns_rdf_new;
+%newobject ldns_rdf_new_frm_str;
+%newobject ldns_rdf_new_frm_data;
+
 %delobject ldns_rdf_deep_free;
 %delobject ldns_rdf_free;
 
@@ -141,7 +145,7 @@ The data is a network ordered array of bytes, which size is specified by the (16
         def __init__(self):
             raise Exception("This class can't be created directly. Please use: ldns_rdf_new, ldns_rdf_new_frm_data, ldns_rdf_new_frm_str, ldns_rdf_new_frm_fp, ldns_rdf_new_frm_fp_l")
        
-        __swig_destroy__ = _ldns._ldns_rdf_free
+        __swig_destroy__ = _ldns._ldns_rdf_deep_free
 
         #LDNS_RDF_CONSTRUCTORS_#
         @staticmethod
index 64e8297883601807f66af4ab30c4b4d5f667f3dc..c3967e0e895f4c1238ff2a3b065e0bd60e5553e2 100644 (file)
@@ -48,6 +48,7 @@
 %ignore ldns_struct_rr::_rdata_fields;
 
 %newobject ldns_rr_clone;
+%newobject ldns_rr_new;
 %newobject ldns_rr_pop_rdf;
 %delobject ldns_rr_free;
 
@@ -828,6 +829,8 @@ The RR is the basic DNS element that contains actual data. This class allows to
 %newobject ldns_rr_list_pop_rr;
 %newobject ldns_rr_list_pop_rr_list;
 %newobject ldns_rr_list_pop_rrset;
+%newobject ldns_rr_list_rr;
+%newobject ldns_rr_list_new;
 %delobject ldns_rr_list_deep_free;
 %delobject ldns_rr_list_free;
 
@@ -846,8 +849,40 @@ void _ldns_rr_list_free(ldns_rr_list* r) {
 %rename(_ldns_rr_list_free) ldns_rr_list_free;
 #endif
 
-%exception ldns_rr_list_push_rr(ldns_rr_list *rr_list, const ldns_rr *rr) %{ $action if (result) Py_INCREF(obj1); %}
-%exception ldns_rr_list_push_rr_list(ldns_rr_list *rr_list, const ldns_rr_list *push_list) %{ $action if (result) Py_INCREF(obj1); %}
+/* clone data on push */
+
+%rename(__ldns_rr_list_push_rr) ldns_rr_list_push_rr;
+%inline %{
+void _ldns_rr_list_push_rr(ldns_rr_list* r, ldns_rr *rr) {
+   ldns_rr_list_push_rr(r, ldns_rr_clone(rr));
+}
+%}
+
+%rename(__ldns_rr_list_push_rr_list) ldns_rr_list_push_rr_list;
+%inline %{
+void _ldns_rr_list_push_rr_list(ldns_rr_list* r, ldns_rr_list *r2) {
+   ldns_rr_list_push_rr_list(r, ldns_rr_list_clone(r2));
+}
+%}
+
+%rename(__ldns_rr_list_cat) ldns_rr_list_cat;
+%inline %{
+void _ldns_rr_list_cat(ldns_rr_list* r, ldns_rr_list *r2) {
+   ldns_rr_list_cat(r, ldns_rr_list_clone(r2));
+}
+%}
+
+
+/* clone data on pull */
+
+%newobject _ldns_rr_list_rr;
+
+%rename(__ldns_rr_list_rr) ldns_rr_list_rr;
+%inline %{
+ldns_rr* _ldns_rr_list_rr(ldns_rr_list* r, int i) {
+   return ldns_rr_clone(ldns_rr_list_rr(r, i));
+}
+%}
 
 %newobject ldns_rr_list2str;
 
@@ -867,7 +902,7 @@ This class contains a list of RR's (see :class:`ldns.ldns_rr`).
             if not self.this:
                 raise Exception("Can't create new RR_LIST")
        
-        __swig_destroy__ = _ldns._ldns_rr_list_free
+        __swig_destroy__ = _ldns._ldns_rr_list_deep_free
 
         #LDNS_RR_LIST_CONSTRUCTORS_#
         @staticmethod
@@ -942,7 +977,7 @@ This class contains a list of RR's (see :class:`ldns.ldns_rr`).
                    the rightside
                :returns: (bool) a left with right concatenated to it
             """
-            return _ldns.ldns_rr_list_cat(self,right)
+            return _ldns._ldns_rr_list_cat(self,right)
             #parameters: ldns_rr_list *,ldns_rr_list *,
             #retvals: bool
 
@@ -991,7 +1026,10 @@ This class contains a list of RR's (see :class:`ldns.ldns_rr`).
                
                :returns: (ldns_rr \*) NULL if nothing to pop. Otherwise the popped RR
             """
-            return _ldns.ldns_rr_list_pop_rr(self)
+            rr = _ldns.ldns_rr_list_pop_rr(self)
+            #if hasattr(self, "_python_rr_refs") and rr in self._python_rr_refs:
+            #    self._python_rr_refs.remove(rr)
+            return rr
             #parameters: ldns_rr_list *,
             #retvals: ldns_rr *
 
@@ -1022,7 +1060,11 @@ This class contains a list of RR's (see :class:`ldns.ldns_rr`).
                    the rr to push
                :returns: (bool) false on error, otherwise true
             """
-            return _ldns.ldns_rr_list_push_rr(self,rr)
+            #if hasattr(self, "_python_rr_refs"):
+            #    self._python_rr_refs.add(rr)
+            #else:
+            #    self._python_rr_refs = set([rr])
+            return _ldns._ldns_rr_list_push_rr(self,rr)
             #parameters: ldns_rr_list *,const ldns_rr *,
             #retvals: bool
 
@@ -1033,7 +1075,7 @@ This class contains a list of RR's (see :class:`ldns.ldns_rr`).
                    the rr_list to push
                :returns: (bool) false on error, otherwise true
             """
-            return _ldns.ldns_rr_list_push_rr_list(self,push_list)
+            return _ldns._ldns_rr_list_push_rr_list(self,push_list)
             #parameters: ldns_rr_list *,const ldns_rr_list *,
             #retvals: bool
 
@@ -1044,7 +1086,7 @@ This class contains a list of RR's (see :class:`ldns.ldns_rr`).
                    return this rr
                :returns: (ldns_rr \*) the rr at position nr
             """
-            return _ldns.ldns_rr_list_rr(self,nr)
+            return _ldns._ldns_rr_list_rr(self,nr)
             #parameters: const ldns_rr_list *,size_t,
             #retvals: ldns_rr *