[pygobject] GValue: add overflow checking for py -> gint; forward marshaling exceptions



commit 4ad6899bdca6fbd0fb7e88cd16b49367f7f41267
Author: Christoph Reiter <creiter src gnome org>
Date:   Mon Mar 27 06:41:19 2017 +0200

    GValue: add overflow checking for py -> gint; forward marshaling exceptions
    
    https://bugzilla.gnome.org/show_bug.cgi?id=769789

 gi/pygi-struct-marshal.c |    3 +--
 gi/pygi-value.c          |   11 ++++++++++-
 tests/test_gi.py         |    7 ++++++-
 3 files changed, 17 insertions(+), 4 deletions(-)
---
diff --git a/gi/pygi-struct-marshal.c b/gi/pygi-struct-marshal.c
index a4276a4..60d2585 100644
--- a/gi/pygi-struct-marshal.c
+++ b/gi/pygi-struct-marshal.c
@@ -125,9 +125,8 @@ pygi_arg_gvalue_from_py_marshal (PyObject *py_arg,
     } else {
         value = g_slice_new0 (GValue);
         g_value_init (value, object_type);
-        if (pyg_value_from_pyobject (value, py_arg) < 0) {
+        if (pyg_value_from_pyobject_with_error (value, py_arg) < 0) {
             g_slice_free (GValue, value);
-            PyErr_SetString (PyExc_RuntimeError, "PyObject conversion to GValue failed");
             return FALSE;
         }
     }
diff --git a/gi/pygi-value.c b/gi/pygi-value.c
index 6e3eb09..3d81e7b 100644
--- a/gi/pygi-value.c
+++ b/gi/pygi-value.c
@@ -386,8 +386,17 @@ pyg_value_from_pyobject_with_error(GValue *value, PyObject *obj)
         g_value_set_boolean(value, PyObject_IsTrue(obj));
         break;
     case G_TYPE_INT:
-        g_value_set_int(value, PYGLIB_PyLong_AsLong(obj));
+    {
+        glong val = PYGLIB_PyLong_AsLong(obj);
+        if (val == -1 && PyErr_Occurred ())
+            return -1;
+        if (val > G_MAXINT || val < G_MININT) {
+            PyErr_SetString(PyExc_OverflowError, "out of range for int property");
+            return -1;
+        }
+        g_value_set_int(value, (gint)val);
         break;
+    }
     case G_TYPE_UINT:
     {
         if (PYGLIB_PyLong_Check(obj)) {
diff --git a/tests/test_gi.py b/tests/test_gi.py
index 16ed076..ffad4ad 100644
--- a/tests/test_gi.py
+++ b/tests/test_gi.py
@@ -1478,9 +1478,14 @@ class TestGValue(unittest.TestCase):
 
     def test_gvalue_flat_array_in_item_marshal_failure(self):
         # Tests the failure to marshal 2^256 to a GValue mid-way through the array marshaling.
-        self.assertRaises(RuntimeError, GIMarshallingTests.gvalue_flat_array,
+        self.assertRaises(OverflowError, GIMarshallingTests.gvalue_flat_array,
                           [42, 2 ** 256, True])
 
+        self.assertRaises(OverflowError, GIMarshallingTests.gvalue_flat_array,
+                          [GLib.MAXINT + 1, "42", True])
+        self.assertRaises(OverflowError, GIMarshallingTests.gvalue_flat_array,
+                          [GLib.MININT - 1, "42", True])
+
     def test_gvalue_flat_array_out(self):
         values = GIMarshallingTests.return_gvalue_flat_array()
         self.assertEqual(values, [42, '42', True])


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]