From 180c7698e2a102388ff1d9881b1fc0a73aab3df9 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 26 Oct 2023 18:38:51 +0200 Subject: [PATCH] JNI: remove implicit conversion of declared QtJniTypes to jobject It's dangerous, as it results in implicit conversion to void * or bool, while hiding the object life time implications. Such objects are QJniObject subclasses, so make sure we don't end up in the catch-all assignment operator when assigning. Constrain that operator to types that are convertible to jobject. Change-Id: If31761c00a678490eeb0b2b038e89e78d8a51747 Reviewed-by: Qt CI Bot Reviewed-by: Assam Boudjelthia Reviewed-by: Ivan Solovev --- src/corelib/kernel/qjniobject.h | 3 ++- src/corelib/kernel/qjnitypes.h | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/corelib/kernel/qjniobject.h b/src/corelib/kernel/qjniobject.h index 1f6433d44e..b088ae1c63 100644 --- a/src/corelib/kernel/qjniobject.h +++ b/src/corelib/kernel/qjniobject.h @@ -534,7 +534,8 @@ public: // This function takes ownership of the jobject and releases the local ref. before returning. static QJniObject fromLocalRef(jobject lref); - template + template , bool> = true> QJniObject &operator=(T obj) { assign(static_cast(obj)); diff --git a/src/corelib/kernel/qjnitypes.h b/src/corelib/kernel/qjnitypes.h index dfde8c7244..c03e92314e 100644 --- a/src/corelib/kernel/qjnitypes.h +++ b/src/corelib/kernel/qjnitypes.h @@ -21,8 +21,6 @@ template struct Object : QJniObject { using Class = Type; - operator jobject() const noexcept { return object(); } - Q_IMPLICIT Object(jobject object) : QJniObject(object) {} Q_IMPLICIT Object(const QJniObject &object) : QJniObject(object) {} Q_IMPLICIT Object(QJniObject &&object) : QJniObject(std::move(object)) {}