From efc7ad5498ff03de8a444a8c1d880f949aaf4d43 Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Wed, 31 Jul 2019 16:34:28 -0300 Subject: Fix crash when accessing a invalid property in the smart pointer Sbk_*_getattro receives an utf8 string and we need to convert it to ascii string before use it on PyErr_Format. Change-Id: Ie3cf5286c9eb6b01f86347b00d523837ca41de32 Reviewed-by: Cristian Maureira-Fredes --- sources/shiboken2/generator/shiboken2/cppgenerator.cpp | 2 +- sources/shiboken2/tests/samplebinding/objecttype_test.py | 8 ++++++++ sources/shiboken2/tests/smartbinding/smart_pointer_test.py | 12 ++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index e36b6edc3..68bcfe508 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -5387,7 +5387,7 @@ void CppGenerator::writeGetattroFunction(QTextStream &s, GeneratorContext &conte s << INDENT << "PyTypeObject *tp = Py_TYPE(self);" << endl; s << INDENT << "PyErr_Format(PyExc_AttributeError," << endl; s << INDENT << " \"'%.50s' object has no attribute '%.400s'\"," << endl; - s << INDENT << " tp->tp_name, PyBytes_AS_STRING(name));" << endl; + s << INDENT << " tp->tp_name, Shiboken::String::toCString(name));" << endl; s << INDENT << "return nullptr;" << endl; } s << INDENT << "} else {" << endl; diff --git a/sources/shiboken2/tests/samplebinding/objecttype_test.py b/sources/shiboken2/tests/samplebinding/objecttype_test.py index bda14c69c..f1a06c2b1 100644 --- a/sources/shiboken2/tests/samplebinding/objecttype_test.py +++ b/sources/shiboken2/tests/samplebinding/objecttype_test.py @@ -118,5 +118,13 @@ class ObjectTypeTest(unittest.TestCase): self.assertLess(abs(before - after), 5) + def testInvalidProperty(self): + o = ObjectType() + try: + o.typo + self.assertFail() + except AttributeError as error: + self.assertEqual(error.args[0], "'sample.ObjectType' object has no attribute 'typo'") + if __name__ == '__main__': unittest.main() diff --git a/sources/shiboken2/tests/smartbinding/smart_pointer_test.py b/sources/shiboken2/tests/smartbinding/smart_pointer_test.py index e07856e61..e1883c7cc 100644 --- a/sources/shiboken2/tests/smartbinding/smart_pointer_test.py +++ b/sources/shiboken2/tests/smartbinding/smart_pointer_test.py @@ -175,5 +175,17 @@ class SmartPointerTests(unittest.TestCase): self.assertEqual(len(ptrToObjList), 0) self.assertEqual(objCount(), 1) + def testInvalidParameter(self): + # Create Obj. + o = Obj() + # Create a shared pointer to an Obj together with an Obj. + ptrToObj = o.giveSharedPtrToObj() + try: + ptrToObj.typo + self.assertFail() + except AttributeError as error: + self.assertEqual(error.args[0], "'smart.SharedPtr_Obj' object has no attribute 'typo'") + + if __name__ == '__main__': unittest.main() -- cgit v1.2.3