diff --git a/internal/backends/android-activity/javahelper.rs b/internal/backends/android-activity/javahelper.rs index 4141a8e1e2e..153607eb1ec 100644 --- a/internal/backends/android-activity/javahelper.rs +++ b/internal/backends/android-activity/javahelper.rs @@ -337,7 +337,7 @@ impl JavaHelper { .l() .map(|l| env.auto_local(l))?; // Safety: `get_clipboard` returns a non-null Java string object. - let string = unsafe { jni_get_string(j_string.as_ref(), env) }?.into(); + let string = jni_get_string(j_string.as_ref(), env)?.into(); Ok(string) }) } @@ -354,7 +354,7 @@ extern "system" fn Java_SlintAndroidJavaHelper_updateText( preedit_end: jint, ) { // Safety: `SlintEditable.toString()` returns a non-null Java string object. - let Ok(java_str) = (unsafe { jni_get_string(&text, &mut env) }) else { return }; + let Ok(java_str) = jni_get_string(&text, &mut env) else { return }; let decoded: std::borrow::Cow = (&java_str).into(); let text = SharedString::from(decoded.as_ref()); @@ -527,11 +527,21 @@ extern "system" fn Java_SlintAndroidJavaHelper_popupMenuAction( .unwrap() } -/// workaround before is merged. -unsafe fn jni_get_string<'e, 'a>( +/// Workaround before is merged. +fn jni_get_string<'e, 'a>( obj: &'a JObject<'a>, env: &mut JNIEnv<'e>, ) -> Result, jni::errors::Error> { + use jni::errors::{Error::*, JniError}; + + let string_class = env.find_class("java/lang/String")?; + let obj_class = env.get_object_class(obj)?; + let obj_class = env.auto_local(obj_class); + if !env.is_assignable_from(string_class, obj_class)? { + return Err(JniCall(JniError::InvalidArguments)); + } + let j_string: &jni::objects::JString<'_> = obj.into(); + // SAFETY: We check that the passed in Object is actually a java.lang.String unsafe { env.get_string_unchecked(j_string) } }