From 358a098d35d2036c10634e0f12d5c5812641352f Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 6 Dec 2020 21:32:58 +0100
Subject: Fortified the Python plugin interface loading.

---
 plugins/pychrysalide/plugin.c | 58 ++++++++++++++++++++++++++-----------------
 1 file changed, 35 insertions(+), 23 deletions(-)

diff --git a/plugins/pychrysalide/plugin.c b/plugins/pychrysalide/plugin.c
index 0fd6c26..495cecf 100644
--- a/plugins/pychrysalide/plugin.c
+++ b/plugins/pychrysalide/plugin.c
@@ -317,25 +317,28 @@ static int py_plugin_module_init(PyObject *self, PyObject *args, PyObject *kwds)
     iface = calloc(1, sizeof(plugin_interface));
     plugin->interface = iface;
 
-#define LOAD_PYTHON_IFACE(attr)                                                                 \
-    do                                                                                          \
-    {                                                                                           \
-        if (PyObject_HasAttrString(self, "_" #attr))                                            \
-        {                                                                                       \
-            value = PyObject_GetAttrString(self, "_" #attr);                                    \
-            if (value != NULL)                                                                  \
-            {                                                                                   \
-                if (PyUnicode_Check(value))                                                     \
-                    iface->attr = strdup(PyUnicode_AsUTF8(value));                              \
-                Py_DECREF(value);                                                               \
-            }                                                                                   \
-        }                                                                                       \
-        if (iface->attr == NULL)                                                                \
-        {                                                                                       \
-            PyErr_SetString(PyExc_TypeError, _("A '_" #attr "' class attributes is missing.")); \
-            return -1;                                                                          \
-        }                                                                                       \
-    }                                                                                           \
+#define LOAD_PYTHON_IFACE(attr)                                                                         \
+    do                                                                                                  \
+    {                                                                                                   \
+        value = PyObject_GetAttrString(self, "_" #attr);                                                \
+        if (value == NULL)                                                                              \
+        {                                                                                               \
+            PyErr_SetString(PyExc_TypeError, _("A '_" #attr "' class attributes is missing."));         \
+            return -1;                                                                                  \
+        }                                                                                               \
+        if (PyUnicode_Check(value))                                                                     \
+        {                                                                                               \
+            iface->attr = strdup(PyUnicode_AsUTF8(value));                                              \
+            Py_DECREF(value);                                                                           \
+        }                                                                                               \
+        else                                                                                            \
+        {                                                                                               \
+            Py_DECREF(value);                                                                           \
+            PyErr_SetString(PyExc_TypeError, _("The '_" #attr "' class attributes must be a string.")); \
+            return -1;                                                                                  \
+        }                                                                                               \
+        assert(iface->attr != NULL);                                                                    \
+    }                                                                                                   \
     while (0);
 
     LOAD_PYTHON_IFACE(name);
@@ -349,12 +352,18 @@ static int py_plugin_module_init(PyObject *self, PyObject *args, PyObject *kwds)
     iface->required[0] = "PyChrysalide";
     iface->required_count = 1;
 
-    if (PyObject_HasAttrString(self, "_actions"))
-        value = PyObject_GetAttrString(self, "_actions");
+    value = PyObject_GetAttrString(self, "_actions");
 
-    else
+    if (value == NULL)
+    {
+        PyErr_SetString(PyExc_TypeError, _("An '_actions' class attributes is missing."));
+        return -1;
+    }
+
+    if (!PyTuple_Check(value))
     {
-        PyErr_SetString(PyExc_TypeError, _("A '_actions' class attributes is missing."));
+        Py_DECREF(value);
+        PyErr_SetString(PyExc_TypeError, _("The '_actions' class attributes must be a tuple."));
         return -1;
     }
 
@@ -367,6 +376,7 @@ static int py_plugin_module_init(PyObject *self, PyObject *args, PyObject *kwds)
 
         if (!PyLong_Check(action))
         {
+            Py_DECREF(value);
             PyErr_SetString(PyExc_TypeError, _("invalid type for plugin action."));
             return -1;
         }
@@ -375,6 +385,8 @@ static int py_plugin_module_init(PyObject *self, PyObject *args, PyObject *kwds)
 
     }
 
+    Py_DECREF(value);
+
     return 0;
 
 }
-- 
cgit v0.11.2-87-g4458