From 7225d11156ccbb4cf5b37f20ab2caf34220ad051 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 12 Jun 2024 09:40:44 +0200 Subject: [PATCH] JNI: don't inherit declared types from JObject JObjectBase has a protected destructor, but JObject doesn't, so inheriting from it might result in UB. Declare the Type as an alias to JObject instead. Specialize the traits for the TypeTag, and add a partial specialization of the traits for JObject. Adjust the return type of the named constructors of JObject. Change-Id: Ibe74c3cd3b3a2a81779117dd2d228684c365a845 Reviewed-by: Assam Boudjelthia (cherry picked from commit 691f6b5b0c9dc69f8d019abc53747e7a6bbf6ef8) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qjniobject.h | 22 ++++++++++++++++++---- src/corelib/kernel/qjnitypes.h | 10 ++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/corelib/kernel/qjniobject.h b/src/corelib/kernel/qjniobject.h index af6c54e5aac..45f84afd0d6 100644 --- a/src/corelib/kernel/qjniobject.h +++ b/src/corelib/kernel/qjniobject.h @@ -720,7 +720,7 @@ public: ~JObject() = default; template, bool> = true + , std::enable_if_t, JObject>, bool> = true , IfValidSignatureTypes = true > explicit JObject(Arg && arg, Args &&...args) @@ -729,10 +729,19 @@ public: {} // named constructors avoid ambiguities - static Type fromJObject(jobject object) { return Type(object); } + static JObject fromJObject(jobject object) + { + return JObject(object); + } template - static Type construct(Args &&...args) { return Type(std::forward(args)...); } - static Type fromLocalRef(jobject lref) { return Type(QJniObject::fromLocalRef(lref)); } + static JObject construct(Args &&...args) + { + return JObject(std::forward(args)...); + } + static JObject fromLocalRef(jobject lref) + { + return JObject(QJniObject::fromLocalRef(lref)); + } static bool registerNativeMethods(std::initializer_list methods) { @@ -809,6 +818,11 @@ private: { return lhs.m_object == rhs.m_object; } Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(JObject); }; + +template struct Traits> { + static constexpr auto signature() { return Traits::signature(); } + static constexpr auto className() { return Traits::className(); } +}; } // This cannot be included earlier as QJniArray is a QJniObject subclass, but it diff --git a/src/corelib/kernel/qjnitypes.h b/src/corelib/kernel/qjnitypes.h index 033445be6a7..5ad63badcd9 100644 --- a/src/corelib/kernel/qjnitypes.h +++ b/src/corelib/kernel/qjnitypes.h @@ -14,17 +14,15 @@ QT_BEGIN_NAMESPACE // QT_TECH_PREVIEW_API #define Q_DECLARE_JNI_TYPE_HELPER(Type) \ namespace QtJniTypes { \ -struct Type : JObject \ -{ \ - using JObject::JObject; \ -}; \ +struct Type##Tag { explicit Type##Tag() = default; }; \ +using Type = JObject; \ } \ // QT_TECH_PREVIEW_API #define Q_DECLARE_JNI_TYPE(Type, Signature) \ Q_DECLARE_JNI_TYPE_HELPER(Type) \ template<> \ -struct QtJniTypes::Traits { \ +struct QtJniTypes::Traits { \ static constexpr auto signature() \ { \ constexpr QtJniTypes::CTString sig(Signature); \ @@ -39,7 +37,7 @@ struct QtJniTypes::Traits { \ #define Q_DECLARE_JNI_CLASS(Type, Signature) \ Q_DECLARE_JNI_TYPE_HELPER(Type) \ template<> \ -struct QtJniTypes::Traits { \ +struct QtJniTypes::Traits { \ static constexpr auto className() \ { \ return QtJniTypes::CTString(Signature); \