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