diff options
author | Renato Araujo Oliveira Filho <renato.araujo@kdab.com> | 2019-07-31 16:34:28 -0300 |
---|---|---|
committer | Renato Araujo Oliveira Filho <renato.araujo@kdab.com> | 2019-08-07 16:34:47 -0300 |
commit | efc7ad5498ff03de8a444a8c1d880f949aaf4d43 (patch) | |
tree | d5ee27861fcd65786aa813b06ae0dce8d642e7cf | |
parent | 68b2245519570314a983baa7cda850ace13f7f81 (diff) |
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 <cristian.maureira-fredes@qt.io>
3 files changed, 21 insertions, 1 deletions
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() |