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. Change-Id: I00d68509be8b5f7304dda2e824fa0ced0f8f8d48 Reviewed-by: Marc Mutz <marc.mutz@qt.io> (cherry picked from commit 9d35561f6f6be125369e6020ce95a73b4aa6b51f) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
fdf4738be3
commit
15a1b718e6
@ -11,8 +11,9 @@
|
|||||||
#include <QtCore/qjniobject.h>
|
#include <QtCore/qjniobject.h>
|
||||||
|
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <utility>
|
#include <QtCore/q26numeric.h>
|
||||||
#include <QtCore/q20type_traits.h>
|
#include <QtCore/q20type_traits.h>
|
||||||
|
#include <QtCore/q20utility.h>
|
||||||
|
|
||||||
#if defined(Q_QDOC)
|
#if defined(Q_QDOC)
|
||||||
using jsize = qint32;
|
using jsize = qint32;
|
||||||
@ -479,9 +480,8 @@ public:
|
|||||||
template <typename Container, if_compatible_source_container<Container> = true>
|
template <typename Container, if_compatible_source_container<Container> = true>
|
||||||
static auto fromContainer(Container &&container)
|
static auto fromContainer(Container &&container)
|
||||||
{
|
{
|
||||||
Q_ASSERT_X(size_t(std::size(container)) <= size_t((std::numeric_limits<size_type>::max)()),
|
if (!q20::in_range<size_type>(std::size(container)))
|
||||||
"QJniArray::fromContainer", "Container is too large for a Java array");
|
qWarning("QJniArray::fromContainer: Container is too large for Java and will be truncated!");
|
||||||
|
|
||||||
using ElementType = typename std::remove_reference_t<Container>::value_type;
|
using ElementType = typename std::remove_reference_t<Container>::value_type;
|
||||||
if constexpr (std::is_base_of_v<std::remove_pointer_t<jobject>,
|
if constexpr (std::is_base_of_v<std::remove_pointer_t<jobject>,
|
||||||
std::remove_pointer_t<ElementType>>) {
|
std::remove_pointer_t<ElementType>>) {
|
||||||
@ -915,7 +915,7 @@ auto QJniArrayBase::makeObjectArray(List &&list)
|
|||||||
return ResultType();
|
return ResultType();
|
||||||
|
|
||||||
JNIEnv *env = QJniEnvironment::getJniEnv();
|
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
|
// this assumes that all objects in the list have the same class
|
||||||
jclass elementClass = nullptr;
|
jclass elementClass = nullptr;
|
||||||
|
@ -5,7 +5,10 @@
|
|||||||
#define QJNITYPES_IMPL_H
|
#define QJNITYPES_IMPL_H
|
||||||
|
|
||||||
#include <QtCore/qstring.h>
|
#include <QtCore/qstring.h>
|
||||||
|
|
||||||
|
#include <QtCore/q26numeric.h>
|
||||||
#include <QtCore/q20type_traits.h>
|
#include <QtCore/q20type_traits.h>
|
||||||
|
#include <QtCore/q20utility.h>
|
||||||
|
|
||||||
#if defined(Q_QDOC) || defined(Q_OS_ANDROID)
|
#if defined(Q_QDOC) || defined(Q_OS_ANDROID)
|
||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
@ -19,7 +22,10 @@ namespace Detail
|
|||||||
{
|
{
|
||||||
static inline jstring fromQString(const QString &string, JNIEnv *env)
|
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)
|
static inline QString toQString(jstring string, JNIEnv *env)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user