michal-grzedzicki / rpms / rpm

Forked from rpms/rpm 4 months ago
Clone
Blob Blame History Raw
From 40326b5724b0cd55a21b2d86eeef344e4826f863 Mon Sep 17 00:00:00 2001
From: Florian Festi <ffesti@redhat.com>
Date: Thu, 20 Oct 2016 16:06:06 +0200
Subject: [PATCH] Do not call headerLink() in hdr_Wrap()

as headers often already have an ref count of 1.
Add headerLink() only where it is necessary.
Plugs memory leaks in Python binding
Resolves: rhbz:#1358467
---
 python/header-py.c | 4 ++--
 python/rpmmi-py.c  | 2 ++
 python/rpmts-py.c  | 1 -
 3 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/python/header-py.c b/python/header-py.c
index 63167d9..5d98f89 100644
--- a/python/header-py.c
+++ b/python/header-py.c
@@ -394,6 +394,7 @@ static PyObject *hdr_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
 	h = headerNew();
     } else if (CAPSULE_CHECK(obj)) {
 	h = CAPSULE_EXTRACT(obj, "rpm._C_Header");
+	headerLink(h);
     } else if (hdrObject_Check(obj)) {
 	h = headerCopy(((hdrObject*) obj)->h);
     } else if (PyBytes_Check(obj)) {
@@ -778,8 +779,7 @@ PyObject * hdr_Wrap(PyTypeObject *subtype, Header h)
 {
     hdrObject * hdr = (hdrObject *)subtype->tp_alloc(subtype, 0);
     if (hdr == NULL) return NULL;
-
-    hdr->h = headerLink(h);
+    hdr->h = h;
     return (PyObject *) hdr;
 }
 
diff --git a/python/rpmmi-py.c b/python/rpmmi-py.c
index 0e27575..379cafb 100644
--- a/python/rpmmi-py.c
+++ b/python/rpmmi-py.c
@@ -1,6 +1,7 @@
 #include "rpmsystem-py.h"
 
 #include <rpm/rpmdb.h>
+#include <rpm/header.h>
 
 #include "rpmmi-py.h"
 #include "header-py.h"
@@ -74,6 +75,7 @@ rpmmi_iternext(rpmmiObject * s)
 	s->mi = rpmdbFreeIterator(s->mi);
 	return NULL;
     }
+    headerLink(h);
     return hdr_Wrap(&hdr_Type, h);
 }
 
diff --git a/python/rpmts-py.c b/python/rpmts-py.c
index 13951df..f05371c 100644
--- a/python/rpmts-py.c
+++ b/python/rpmts-py.c
@@ -384,7 +384,6 @@ rpmts_HdrFromFdno(rpmtsObject * s, PyObject *arg)
 
     if (rpmrc == RPMRC_OK) {
 	ho = hdr_Wrap(&hdr_Type, h);
-	h = headerFree(h); /* ref held by python object */
     } else {
 	Py_INCREF(Py_None);
 	ho = Py_None;
-- 
2.9.3