Blob Blame History Raw
From aaf691dbc85295aeb09e1ea4081089a24dc28759 Mon Sep 17 00:00:00 2001
From: Pavlina Moravcova Varekova <pmoravco@redhat.com>
Date: Tue, 21 Feb 2017 11:37:20 +0100
Subject: [PATCH] Fix number of references on spec_Type (#114)

After creating a specPkg from a spec file we must increase spec file reference counter. Otherwise spec file may be accidentally deallocated and usage of SpecPkg can cause an error.
---
 python/spec-py.c | 14 +++++++++++---
 python/spec-py.h |  2 +-
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/python/spec-py.c b/python/spec-py.c
index f710f5c..753afba 100644
--- a/python/spec-py.c
+++ b/python/spec-py.c
@@ -45,8 +45,14 @@ struct specPkgObject_s {
     PyObject_HEAD
     /*type specific fields */
     rpmSpecPkg pkg;
+    specObject *source_spec;
 };
 
+static void specPkg_dealloc(specPkgObject * s)
+{
+    Py_DECREF(s->source_spec);
+}
+
 static PyObject *pkgGetSection(rpmSpecPkg pkg, int section)
 {
     char *sect = rpmSpecPkgGetSection(pkg, section);
@@ -95,7 +101,7 @@ PyTypeObject specPkg_Type = {
 	"rpm.specpkg",			/* tp_name */
 	sizeof(specPkgObject),		/* tp_size */
 	0,				/* tp_itemsize */
-	0, 				/* tp_dealloc */
+	(destructor) specPkg_dealloc, 	/* tp_dealloc */
 	0,				/* tp_print */
 	0, 				/* tp_getattr */
 	0,				/* tp_setattr */
@@ -227,7 +233,7 @@ static PyObject * spec_get_packages(specObject *s, void *closure)
     iter = rpmSpecPkgIterInit(s->spec);
 
     while ((pkg = rpmSpecPkgIterNext(iter)) != NULL) {
-	PyObject *po = specPkg_Wrap(&specPkg_Type, pkg);
+	PyObject *po = specPkg_Wrap(&specPkg_Type, pkg, s);
         if (!po) {
             rpmSpecPkgIterFree(iter);
             Py_DECREF(pkgList);
@@ -350,12 +356,14 @@ spec_Wrap(PyTypeObject *subtype, rpmSpec spec)
     return (PyObject *) s;
 }
 
-PyObject * specPkg_Wrap(PyTypeObject *subtype, rpmSpecPkg pkg) 
+PyObject * specPkg_Wrap(PyTypeObject *subtype, rpmSpecPkg pkg, specObject *source)
 {
     specPkgObject * s = (specPkgObject *)subtype->tp_alloc(subtype, 0);
     if (s == NULL) return NULL;
 
     s->pkg = pkg;
+    s->source_spec = source;
+    Py_INCREF(s->source_spec);
     return (PyObject *) s;
 }
 
diff --git a/python/spec-py.h b/python/spec-py.h
index 558fbf2..65b8dc3 100644
--- a/python/spec-py.h
+++ b/python/spec-py.h
@@ -13,6 +13,6 @@ extern PyTypeObject specPkg_Type;
 #define specPkgObject_Check(v)	((v)->ob_type == &specPkg_Type)
 
 PyObject * spec_Wrap(PyTypeObject *subtype, rpmSpec spec);
-PyObject * specPkg_Wrap(PyTypeObject *subtype, rpmSpecPkg pkg);
+PyObject * specPkg_Wrap(PyTypeObject *subtype, rpmSpecPkg pkg, specObject *source);
 
 #endif /* RPMPYTHON_SPEC */
-- 
2.7.4