[pygobject] properties: fix a crash when setting a str value containing surrogates. Fixes #169



commit 48d54a3dc0a5df02ca7622da5c97f32fde504f91
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Mon Mar 19 13:52:21 2018 +0100

    properties: fix a crash when setting a str value containing surrogates. Fixes #169
    
    In Python 3 decoding unicode fails if it contains surrogates. Handle that case.
    This ususally happens with filenames where undecodable bytes get converted to surrogates.

 gi/pygi-value.c          |  4 ++++
 gi/pygobject-object.c    |  4 ++--
 tests/test_properties.py | 12 +++++++++++-
 3 files changed, 17 insertions(+), 3 deletions(-)
---
diff --git a/gi/pygi-value.c b/gi/pygi-value.c
index 87d397b3..814bb024 100644
--- a/gi/pygi-value.c
+++ b/gi/pygi-value.c
@@ -500,6 +500,10 @@ pyg_value_from_pyobject_with_error(GValue *value, PyObject *obj)
                 g_value_set_string(value, PyString_AsString(tmp_str));
 #else
                 tmp = PyUnicode_AsUTF8String(tmp_str);
+                if (tmp == NULL) {
+                    Py_DECREF (tmp_str);
+                    return -1;
+                }
                 g_value_set_string(value, PyBytes_AsString(tmp));
                 Py_DECREF(tmp);
 #endif
diff --git a/gi/pygobject-object.c b/gi/pygobject-object.c
index 304b4277..ff15492b 100644
--- a/gi/pygobject-object.c
+++ b/gi/pygobject-object.c
@@ -332,9 +332,9 @@ set_property_from_pspec(GObject *obj,
 
     g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE(pspec));
     if (pyg_param_gvalue_from_pyobject(&value, pvalue, pspec) < 0) {
-        PyObject *pvalue_str = PyObject_Str(pvalue);
+        PyObject *pvalue_str = PyObject_Repr(pvalue);
        PyErr_Format(PyExc_TypeError,
-                    "could not convert '%s' to type '%s' when setting property '%s.%s'",
+                    "could not convert %s to type '%s' when setting property '%s.%s'",
                     PYGLIB_PyUnicode_AsString(pvalue_str),
                     g_type_name(G_PARAM_SPEC_VALUE_TYPE(pspec)),
                     G_OBJECT_TYPE_NAME(obj),
diff --git a/tests/test_properties.py b/tests/test_properties.py
index 268ee93a..622fdb22 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -10,6 +10,8 @@ import types
 import unittest
 import tempfile
 
+import pytest
+
 from gi.repository import GObject
 from gi.repository.GObject import ParamFlags, GType, new
 from gi.repository.GObject import \
@@ -29,7 +31,7 @@ from gi.repository import GIMarshallingTests
 from gi.repository import Regress
 from gi import _propertyhelper as propertyhelper
 
-from .compathelper import _long
+from .compathelper import _long, PY3
 from .helper import capture_glib_warnings, capture_output
 
 
@@ -178,6 +180,14 @@ class TestPropertyObject(unittest.TestCase):
         obj.props.normal = unicode_utf8
         self.assertEqual(obj.props.normal, test_utf8)
 
+    def test_utf8_lone_surrogate(self):
+        obj = PropertyObject()
+        if PY3:
+            with pytest.raises(TypeError):
+                obj.set_property('construct', '\ud83d')
+        else:
+            obj.set_property('construct', '\ud83d')
+
     def test_int_to_str(self):
         obj = new(PropertyObject, construct_only=1)
         self.assertEqual(obj.props.construct_only, '1')


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