From 9016163226e575e0d57dbd3b99f645bb29899ad3 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 20 Jun 2020 08:18:30 +0200 Subject: [PATCH 15/21] python: return NULL if set_list_of_xml_nodes fails (#44287) --- bindings/python/lang.py | 4 ++-- bindings/python/wrapper_top.c | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/bindings/python/lang.py b/bindings/python/lang.py index 22b1afae..50b31db7 100644 --- a/bindings/python/lang.py +++ b/bindings/python/lang.py @@ -820,7 +820,7 @@ register_constants(PyObject *d) if is_cstring(el_type): print_(' RETURN_IF_FAIL(set_list_of_strings(&this->%s, cvt_value));' % name, file=fd) elif is_xml_node(el_type): - print_(' set_list_of_xml_nodes(&this->%s, cvt_value);' % name, file=fd) + print_(' RETURN_IF_FAIL(set_list_of_xml_nodes(&this->%s, cvt_value));' % name, file=fd) elif is_object(el_type): print_(' set_list_of_pygobject(&this->%s, cvt_value);' % name, file=fd) else: @@ -983,7 +983,7 @@ register_constants(PyObject *d) if is_cstring(qualifier): print_(' EXIT_IF_FAIL(set_list_of_strings(&%s, cvt_%s));' % (arg[1], arg[1]), file=fd) elif is_xml_node(qualifier): - print_(' set_list_of_xml_nodes(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd) + print_(' EXIT_IF_FAIL(set_list_of_xml_nodes(&%s, cvt_%s));' % (arg[1], arg[1]), file=fd) elif isinstance(qualifier, str) and qualifier.startswith('Lasso'): print_(' set_list_of_pygobject(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd) else: diff --git a/bindings/python/wrapper_top.c b/bindings/python/wrapper_top.c index 7d26dd8c..6687acce 100644 --- a/bindings/python/wrapper_top.c +++ b/bindings/python/wrapper_top.c @@ -92,7 +92,7 @@ G_GNUC_UNUSED static PyObject* PyGObjectPtr_New(GObject *obj); G_GNUC_UNUSED static int set_hashtable_of_pygobject(GHashTable *a_hash, PyObject *dict); G_GNUC_UNUSED static int set_hashtable_of_strings(GHashTable *a_hash, PyObject *dict); G_GNUC_UNUSED static int set_list_of_strings(GList **a_list, PyObject *seq); -G_GNUC_UNUSED static void set_list_of_xml_nodes(GList **a_list, PyObject *seq); +G_GNUC_UNUSED static int set_list_of_xml_nodes(GList **a_list, PyObject *seq); G_GNUC_UNUSED static void set_list_of_pygobject(GList **a_list, PyObject *seq); G_GNUC_UNUSED static PyObject *get_list_of_strings(const GList *a_list); G_GNUC_UNUSED static PyObject *get_list_of_xml_nodes(const GList *a_list); @@ -361,12 +361,16 @@ failure: /** Set the GList* pointer, pointed by a_list, to a pointer on a new GList * created by converting the python seq into a GList of xmlNode*. */ -static void +int set_list_of_xml_nodes(GList **a_list, PyObject *seq) { GList *list = NULL; - int l = 0,i; + int l = 0, i; - lasso_return_if_fail(valid_seq(seq)); + if (! valid_seq(seq)) { + PyErr_SetString(PyExc_TypeError, + "value should be a tuple of strings"); + return 0; + } if (seq != Py_None) { l = PySequence_Length(seq); } @@ -379,13 +383,19 @@ set_list_of_xml_nodes(GList **a_list, PyObject *seq) { goto failure; } item_node = get_xml_node_from_pystring(item); + if (! item_node) { + PyErr_SetString(PyExc_TypeError, + "values should be valid XML fragments"); + goto failure; + } list = g_list_append(list, item_node); } free_list(a_list, (GFunc)xmlFreeNode); *a_list = list; - return; + return 1; failure: free_list(&list, (GFunc)xmlFreeNode); + return 0; } /** Set the GList* pointer, pointed by a_list, to a pointer on a new GList -- 2.26.2