From b65a7b1780ec34d1c3ac6cb717189325f9e8e238 Mon Sep 17 00:00:00 2001 From: Jan Buenker Date: Thu, 27 Jun 2024 13:06:28 +0200 Subject: [PATCH] Make JNI string conversions behave like ObjC string conversions Return empty string if conversion fails --- support-lib/jni/djinni_support.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/support-lib/jni/djinni_support.cpp b/support-lib/jni/djinni_support.cpp index 77facc6c..91bc6f66 100644 --- a/support-lib/jni/djinni_support.cpp +++ b/support-lib/jni/djinni_support.cpp @@ -439,7 +439,12 @@ using WcharConverter = std::wstring_convert, char16_t>; jstring jniStringFromWString(JNIEnv * env, const std::wstring & str) { - std::string u16 = WcharConverter{}.to_bytes(str); + std::string u16{}; + try { + u16 = WcharConverter{}.to_bytes(str); + } catch (...) { + // Conversion error - empty string will be returned + } jstring res = env->NewString(reinterpret_cast(u16.data()), u16.size()/sizeof(jchar)); DJINNI_ASSERT(res, env); return res; @@ -457,7 +462,12 @@ std::wstring jniWStringFromString(JNIEnv * env, const jstring jstr) { } jstring jniStringFromUTF8(JNIEnv * env, const std::string & str) { - std::u16string u16 = Utf8Converter{}.from_bytes(str); + std::u16string u16{}; + try { + u16 = Utf8Converter{}.from_bytes(str); + } catch (...) { + // Conversion error - empty string will be returned + } jstring res = env->NewString(reinterpret_cast(u16.data()), u16.size()); DJINNI_ASSERT(res, env); return res;