b50c854
From aaf691dbc85295aeb09e1ea4081089a24dc28759 Mon Sep 17 00:00:00 2001
b50c854
From: Pavlina Moravcova Varekova <pmoravco@redhat.com>
b50c854
Date: Tue, 21 Feb 2017 11:37:20 +0100
b50c854
Subject: [PATCH] Fix number of references on spec_Type (#114)
b50c854
b50c854
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.
b50c854
---
b50c854
 python/spec-py.c | 14 +++++++++++---
b50c854
 python/spec-py.h |  2 +-
b50c854
 2 files changed, 12 insertions(+), 4 deletions(-)
b50c854
b50c854
diff --git a/python/spec-py.c b/python/spec-py.c
b50c854
index f710f5c..753afba 100644
b50c854
--- a/python/spec-py.c
b50c854
+++ b/python/spec-py.c
b50c854
@@ -45,8 +45,14 @@ struct specPkgObject_s {
b50c854
     PyObject_HEAD
b50c854
     /*type specific fields */
b50c854
     rpmSpecPkg pkg;
b50c854
+    specObject *source_spec;
b50c854
 };
b50c854
 
b50c854
+static void specPkg_dealloc(specPkgObject * s)
b50c854
+{
b50c854
+    Py_DECREF(s->source_spec);
b50c854
+}
b50c854
+
b50c854
 static PyObject *pkgGetSection(rpmSpecPkg pkg, int section)
b50c854
 {
b50c854
     char *sect = rpmSpecPkgGetSection(pkg, section);
b50c854
@@ -95,7 +101,7 @@ PyTypeObject specPkg_Type = {
b50c854
 	"rpm.specpkg",			/* tp_name */
b50c854
 	sizeof(specPkgObject),		/* tp_size */
b50c854
 	0,				/* tp_itemsize */
b50c854
-	0, 				/* tp_dealloc */
b50c854
+	(destructor) specPkg_dealloc, 	/* tp_dealloc */
b50c854
 	0,				/* tp_print */
b50c854
 	0, 				/* tp_getattr */
b50c854
 	0,				/* tp_setattr */
b50c854
@@ -227,7 +233,7 @@ static PyObject * spec_get_packages(specObject *s, void *closure)
b50c854
     iter = rpmSpecPkgIterInit(s->spec);
b50c854
 
b50c854
     while ((pkg = rpmSpecPkgIterNext(iter)) != NULL) {
b50c854
-	PyObject *po = specPkg_Wrap(&specPkg_Type, pkg);
b50c854
+	PyObject *po = specPkg_Wrap(&specPkg_Type, pkg, s);
b50c854
         if (!po) {
b50c854
             rpmSpecPkgIterFree(iter);
b50c854
             Py_DECREF(pkgList);
b50c854
@@ -350,12 +356,14 @@ spec_Wrap(PyTypeObject *subtype, rpmSpec spec)
b50c854
     return (PyObject *) s;
b50c854
 }
b50c854
 
b50c854
-PyObject * specPkg_Wrap(PyTypeObject *subtype, rpmSpecPkg pkg) 
b50c854
+PyObject * specPkg_Wrap(PyTypeObject *subtype, rpmSpecPkg pkg, specObject *source)
b50c854
 {
b50c854
     specPkgObject * s = (specPkgObject *)subtype->tp_alloc(subtype, 0);
b50c854
     if (s == NULL) return NULL;
b50c854
 
b50c854
     s->pkg = pkg;
b50c854
+    s->source_spec = source;
b50c854
+    Py_INCREF(s->source_spec);
b50c854
     return (PyObject *) s;
b50c854
 }
b50c854
 
b50c854
diff --git a/python/spec-py.h b/python/spec-py.h
b50c854
index 558fbf2..65b8dc3 100644
b50c854
--- a/python/spec-py.h
b50c854
+++ b/python/spec-py.h
b50c854
@@ -13,6 +13,6 @@ extern PyTypeObject specPkg_Type;
b50c854
 #define specPkgObject_Check(v)	((v)->ob_type == &specPkg_Type)
b50c854
 
b50c854
 PyObject * spec_Wrap(PyTypeObject *subtype, rpmSpec spec);
b50c854
-PyObject * specPkg_Wrap(PyTypeObject *subtype, rpmSpecPkg pkg);
b50c854
+PyObject * specPkg_Wrap(PyTypeObject *subtype, rpmSpecPkg pkg, specObject *source);
b50c854
 
b50c854
 #endif /* RPMPYTHON_SPEC */
b50c854
-- 
b50c854
2.7.4
b50c854