diff options
| author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2025-01-08 12:46:07 +0100 |
|---|---|---|
| committer | Marc Mutz <marc.mutz@qt.io> | 2025-01-17 21:54:56 +0000 |
| commit | 9d35561f6f6be125369e6020ce95a73b4aa6b51f (patch) | |
| tree | cee36ee4ec2ad830e76531a2cde0b7f25dece8fd /src | |
| parent | a57d5b1fd60b6b6848ef8ad9db237941229d7a23 (diff) | |
JNI: handle narrowing before creating a new Java string
Emit a warning if the string we get is too large for Java, and cast
the size to jsize before calling JNI.
For consistency, replace the Q_ASSERT in QJniArray's size check to use
the same logic.
Addresses header review comment; code was not handling this case before
it got moved into the inline helper either.
Pick-to: 6.9
Change-Id: I00d68509be8b5f7304dda2e824fa0ced0f8f8d48
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src')
| -rw-r--r-- | src/corelib/kernel/qjniarray.h | 10 | ||||
| -rw-r--r-- | src/corelib/kernel/qjnitypes_impl.h | 8 |
2 files changed, 12 insertions, 6 deletions
diff --git a/src/corelib/kernel/qjniarray.h b/src/corelib/kernel/qjniarray.h index c924f662ee4..6f710390385 100644 --- a/src/corelib/kernel/qjniarray.h +++ b/src/corelib/kernel/qjniarray.h @@ -11,8 +11,9 @@ #include <QtCore/qjniobject.h> #include <iterator> -#include <utility> +#include <QtCore/q26numeric.h> #include <QtCore/q20type_traits.h> +#include <QtCore/q20utility.h> #if defined(Q_QDOC) using jsize = qint32; @@ -479,9 +480,8 @@ public: template <typename Container, if_compatible_source_container<Container> = true> static auto fromContainer(Container &&container) { - Q_ASSERT_X(size_t(std::size(container)) <= size_t((std::numeric_limits<size_type>::max)()), - "QJniArray::fromContainer", "Container is too large for a Java array"); - + if (!q20::in_range<size_type>(std::size(container))) + qWarning("QJniArray::fromContainer: Container is too large for Java and will be truncated!"); using ElementType = typename std::remove_reference_t<Container>::value_type; if constexpr (std::is_base_of_v<std::remove_pointer_t<jobject>, std::remove_pointer_t<ElementType>>) { @@ -914,7 +914,7 @@ auto QJniArrayBase::makeObjectArray(List &&list) return ResultType(); JNIEnv *env = QJniEnvironment::getJniEnv(); - const size_type length = size_type(std::size(list)); + const size_type length = q26::saturate_cast<size_type>(std::size(list)); // this assumes that all objects in the list have the same class jclass elementClass = nullptr; diff --git a/src/corelib/kernel/qjnitypes_impl.h b/src/corelib/kernel/qjnitypes_impl.h index a5ce38d1a8c..6e1ba45db83 100644 --- a/src/corelib/kernel/qjnitypes_impl.h +++ b/src/corelib/kernel/qjnitypes_impl.h @@ -5,7 +5,10 @@ #define QJNITYPES_IMPL_H #include <QtCore/qstring.h> + +#include <QtCore/q26numeric.h> #include <QtCore/q20type_traits.h> +#include <QtCore/q20utility.h> #if defined(Q_QDOC) || defined(Q_OS_ANDROID) #include <jni.h> @@ -19,7 +22,10 @@ namespace Detail { static inline jstring fromQString(const QString &string, JNIEnv *env) { - return env->NewString(reinterpret_cast<const jchar*>(string.constData()), string.length()); + if (!q20::in_range<jsize>(string.size())) + qWarning("String is too large for a Java string and will be truncated"); + const jsize length = q26::saturate_cast<jsize>(string.size()); + return env->NewString(reinterpret_cast<const jchar*>(string.constData()), length); } static inline QString toQString(jstring string, JNIEnv *env) |
