5523e9
From 40326b5724b0cd55a21b2d86eeef344e4826f863 Mon Sep 17 00:00:00 2001
5523e9
From: Florian Festi <ffesti@redhat.com>
5523e9
Date: Thu, 20 Oct 2016 16:06:06 +0200
5523e9
Subject: [PATCH] Do not call headerLink() in hdr_Wrap()
5523e9
5523e9
as headers often already have an ref count of 1.
5523e9
Add headerLink() only where it is necessary.
5523e9
Plugs memory leaks in Python binding
5523e9
Resolves: rhbz:#1358467
5523e9
---
5523e9
 python/header-py.c | 4 ++--
5523e9
 python/rpmmi-py.c  | 2 ++
5523e9
 python/rpmts-py.c  | 1 -
5523e9
 3 files changed, 4 insertions(+), 3 deletions(-)
5523e9
5523e9
diff --git a/python/header-py.c b/python/header-py.c
5523e9
index 63167d9..5d98f89 100644
5523e9
--- a/python/header-py.c
5523e9
+++ b/python/header-py.c
5523e9
@@ -394,6 +394,7 @@ static PyObject *hdr_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
5523e9
 	h = headerNew();
5523e9
     } else if (CAPSULE_CHECK(obj)) {
5523e9
 	h = CAPSULE_EXTRACT(obj, "rpm._C_Header");
5523e9
+	headerLink(h);
5523e9
     } else if (hdrObject_Check(obj)) {
5523e9
 	h = headerCopy(((hdrObject*) obj)->h);
5523e9
     } else if (PyBytes_Check(obj)) {
5523e9
@@ -778,8 +779,7 @@ PyObject * hdr_Wrap(PyTypeObject *subtype, Header h)
5523e9
 {
5523e9
     hdrObject * hdr = (hdrObject *)subtype->tp_alloc(subtype, 0);
5523e9
     if (hdr == NULL) return NULL;
5523e9
-
5523e9
-    hdr->h = headerLink(h);
5523e9
+    hdr->h = h;
5523e9
     return (PyObject *) hdr;
5523e9
 }
5523e9
 
5523e9
diff --git a/python/rpmmi-py.c b/python/rpmmi-py.c
5523e9
index 0e27575..379cafb 100644
5523e9
--- a/python/rpmmi-py.c
5523e9
+++ b/python/rpmmi-py.c
5523e9
@@ -1,6 +1,7 @@
5523e9
 #include "rpmsystem-py.h"
5523e9
 
5523e9
 #include <rpm/rpmdb.h>
5523e9
+#include <rpm/header.h>
5523e9
 
5523e9
 #include "rpmmi-py.h"
5523e9
 #include "header-py.h"
5523e9
@@ -74,6 +75,7 @@ rpmmi_iternext(rpmmiObject * s)
5523e9
 	s->mi = rpmdbFreeIterator(s->mi);
5523e9
 	return NULL;
5523e9
     }
5523e9
+    headerLink(h);
5523e9
     return hdr_Wrap(&hdr_Type, h);
5523e9
 }
5523e9
 
5523e9
diff --git a/python/rpmts-py.c b/python/rpmts-py.c
5523e9
index 13951df..f05371c 100644
5523e9
--- a/python/rpmts-py.c
5523e9
+++ b/python/rpmts-py.c
5523e9
@@ -384,7 +384,6 @@ rpmts_HdrFromFdno(rpmtsObject * s, PyObject *arg)
5523e9
 
5523e9
     if (rpmrc == RPMRC_OK) {
5523e9
 	ho = hdr_Wrap(&hdr_Type, h);
5523e9
-	h = headerFree(h); /* ref held by python object */
5523e9
     } else {
5523e9
 	Py_INCREF(Py_None);
5523e9
 	ho = Py_None;
5523e9
-- 
5523e9
2.9.3
5523e9