diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-06-29 08:34:56 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-06-29 14:11:51 +0000 |
commit | 10763c7a0c97b6b51ddd4fb8255b90a7897e22ff (patch) | |
tree | be71cd0eaf9bf588697f8bd316a1d0137880cbfc /sources | |
parent | 32d4799927bd69d6a6cababdf7fc88131d900656 (diff) |
PySide6: Fix crashes in QObject.findChild()
For all objects encountered in findChild(), a wrapper
was created just to be able to retrieve its type object
and check against it. As there is a name lookup involved,
it happens that non-QObject wrappers are created for this
(in the specific case, an enum wrapper), which leads to
crashes later on.
Refactor the code, extracting a helper function to PySide
which finds the best-matching type object for a QObject
to check on that. Rearrange the check so that the name is
checked first.
Fixes: PYSIDE-1609
Change-Id: I026854201f3f6eca9e5905105127f0a4b4588fa9
Reviewed-by: Christian Tismer <tismer@stackless.com>
(cherry picked from commit 98aef951920641dcdf4c217a0c51626cdf82caf8)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'sources')
-rw-r--r-- | sources/pyside6/PySide6/glue/qtcore.cpp | 45 | ||||
-rw-r--r-- | sources/pyside6/libpyside/pyside.cpp | 16 | ||||
-rw-r--r-- | sources/pyside6/libpyside/pyside.h | 5 |
3 files changed, 47 insertions, 19 deletions
diff --git a/sources/pyside6/PySide6/glue/qtcore.cpp b/sources/pyside6/PySide6/glue/qtcore.cpp index d98ab21be..cf38fbc94 100644 --- a/sources/pyside6/PySide6/glue/qtcore.cpp +++ b/sources/pyside6/PySide6/glue/qtcore.cpp @@ -815,15 +815,32 @@ qRegisterMetaType<QList<int> >("QList<int>"); // @snippet qobject-metaobject // @snippet qobject-findchild-1 +static bool _findChildTypeMatch(const QObject *child, PyTypeObject *desiredType) +{ + auto *pyChildType = PySide::getTypeForQObject(child); + return pyChildType != nullptr && PyType_IsSubtype(pyChildType, desiredType); +} + +static inline bool _findChildrenComparator(const QObject *child, + const QRegularExpression &name) +{ + return name.match(child->objectName()).hasMatch(); +} + +static inline bool _findChildrenComparator(const QObject *child, + const QString &name) +{ + return name.isNull() || name == child->objectName(); +} + static QObject *_findChildHelper(const QObject *parent, const QString &name, PyTypeObject *desiredType, Qt::FindChildOptions options) { for (auto *child : parent->children()) { - Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QObject *](child)); - if (PyType_IsSubtype(Py_TYPE(pyChild), desiredType) - && (name.isNull() || name == child->objectName())) { - return child; + if (_findChildrenComparator(child, name) + && _findChildTypeMatch(child, desiredType)) { + return child; } } @@ -837,25 +854,17 @@ static QObject *_findChildHelper(const QObject *parent, const QString &name, return nullptr; } -static inline bool _findChildrenComparator(const QObject *&child, const QRegularExpression &name) -{ - return name.match(child->objectName()).hasMatch(); -} - -static inline bool _findChildrenComparator(const QObject *&child, const QString &name) -{ - return name.isNull() || name == child->objectName(); -} - -template<typename T> +template<typename T> // QString/QRegularExpression static void _findChildrenHelper(const QObject *parent, const T& name, PyTypeObject *desiredType, Qt::FindChildOptions options, PyObject *result) { for (const auto *child : parent->children()) { - Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QObject *](child)); - if (PyType_IsSubtype(Py_TYPE(pyChild), desiredType) && _findChildrenComparator(child, name)) - PyList_Append(result, pyChild); + if (_findChildrenComparator(child, name) && + _findChildTypeMatch(child, desiredType)) { + Shiboken::AutoDecRef pyChild(%CONVERTTOPYTHON[QObject *](child)); + PyList_Append(result, pyChild.object()); + } if (options.testFlag(Qt::FindChildrenRecursively)) _findChildrenHelper(child, name, desiredType, options, result); } diff --git a/sources/pyside6/libpyside/pyside.cpp b/sources/pyside6/libpyside/pyside.cpp index a78e4e9eb..07a9fbef3 100644 --- a/sources/pyside6/libpyside/pyside.cpp +++ b/sources/pyside6/libpyside/pyside.cpp @@ -421,7 +421,7 @@ static const char invalidatePropertyName[] = "_PySideInvalidatePtr"; // PYSIDE-1214, when creating new wrappers for classes inheriting QObject but // not exposed to Python, try to find the best-matching (most-derived) Qt // class by walking up the meta objects. -static const char *typeName(QObject *cppSelf) +static const char *typeName(const QObject *cppSelf) { const char *typeName = typeid(*cppSelf).name(); if (!Shiboken::Conversions::getConverter(typeName)) { @@ -436,6 +436,20 @@ static const char *typeName(QObject *cppSelf) return typeName; } +PyTypeObject *getTypeForQObject(const QObject *cppSelf) +{ + // First check if there are any instances of Python implementations + // inheriting a PySide class. + auto *existing = Shiboken::BindingManager::instance().retrieveWrapper(cppSelf); + if (existing != nullptr) + return reinterpret_cast<PyObject *>(existing)->ob_type; + // Find the best match (will return a PySide type) + auto *sbkObjectType = Shiboken::ObjectType::typeForTypeName(typeName(cppSelf)); + if (sbkObjectType != nullptr) + return reinterpret_cast<PyTypeObject *>(sbkObjectType); + return nullptr; +} + PyObject *getWrapperForQObject(QObject *cppSelf, SbkObjectType *sbk_type) { PyObject *pyOut = reinterpret_cast<PyObject *>(Shiboken::BindingManager::instance().retrieveWrapper(cppSelf)); diff --git a/sources/pyside6/libpyside/pyside.h b/sources/pyside6/libpyside/pyside.h index 3c4ae92b2..077c75691 100644 --- a/sources/pyside6/libpyside/pyside.h +++ b/sources/pyside6/libpyside/pyside.h @@ -140,6 +140,11 @@ PYSIDE_API void setNextQObjectMemoryAddr(void *addr); PYSIDE_API PyObject *getWrapperForQObject(QObject *cppSelf, SbkObjectType *sbk_type); +/// Return the best-matching type for a QObject (Helper for QObject.findType()) +/// \param cppSelf QObject instance +/// \return type object +PYSIDE_API PyTypeObject *getTypeForQObject(const QObject *cppSelf); + #ifdef PYSIDE_QML_SUPPORT // Used by QtQuick module to notify QtQml that custom QtQuick items can be registered. using QuickRegisterItemFunction = |