emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

feature/android f8a2619d981 2/7: More fixes to JNI error checking


From: Po Lu
Subject: feature/android f8a2619d981 2/7: More fixes to JNI error checking
Date: Wed, 1 Mar 2023 03:00:45 -0500 (EST)

branch: feature/android
commit f8a2619d981b7ba578378e592b878f08f29e4ba9
Author: Po Lu <luangruo@yahoo.com>
Commit: Po Lu <luangruo@yahoo.com>

    More fixes to JNI error checking
    
    * src/android.c (android_query_tree, android_get_geometry)
    (android_translate_coordinates, android_query_battery):
    Correctly handle result of GetTArrayElements.
    (android_exception_check_nonnull): New function.
    * src/android.h:
    * src/androidselect.c (Fandroid_get_clipboard): Likewise.
---
 src/android.c       | 27 +++++++++++++++++++++++++--
 src/android.h       |  1 +
 src/androidselect.c |  2 +-
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/android.c b/src/android.c
index 001fa867d9c..3bccaab041a 100644
--- a/src/android.c
+++ b/src/android.c
@@ -4370,6 +4370,8 @@ android_query_tree (android_window handle, android_window 
*root_return,
   shorts
     = (*android_java_env)->GetShortArrayElements (android_java_env, array,
                                                  NULL);
+  android_exception_check_nonnull (shorts, array);
+
   for (i = 1; i < nelements; ++i)
     children[i] = shorts[i];
 
@@ -4422,6 +4424,7 @@ android_get_geometry (android_window handle,
     = (*android_java_env)->GetIntArrayElements (android_java_env,
                                                window_geometry,
                                                NULL);
+  android_exception_check_nonnull (ints, window_geometry);
 
   *x_return = ints[0];
   *y_return = ints[1];
@@ -4477,7 +4480,7 @@ android_translate_coordinates (android_window src, int x,
   /* Obtain the coordinates from the array.  */
   ints = (*android_java_env)->GetIntArrayElements (android_java_env,
                                                   coordinates, NULL);
-  android_exception_check_1 (coordinates);
+  android_exception_check_nonnull (ints, coordinates);
 
   *root_x = ints[0];
   *root_y = ints[1];
@@ -5302,6 +5305,26 @@ android_exception_check_2 (jobject object, jobject 
object1)
     }
 }
 
+/* Check for JNI problems based on the value of OBJECT.
+
+   Signal out of memory if OBJECT is NULL.  OBJECT1 means the
+   same as in `android_exception_check_1'.
+
+   This function is useful when checking for errors from JNI
+   functions that do not set exceptions on failure, such as
+   `GetIntArrayElements'.  */
+
+void
+android_exception_check_nonnull (void *object, jobject object1)
+{
+  if (object)
+    return;
+
+  if (object1)
+    ANDROID_DELETE_LOCAL_REF (object1);
+
+  memory_full (0);
+}
 
 
 
@@ -5656,7 +5679,7 @@ android_query_battery (struct android_battery_state 
*status)
 
   longs = (*android_java_env)->GetLongArrayElements (android_java_env,
                                                     array, NULL);
-  android_exception_check_1 (array);
+  android_exception_check_nonnull (longs, array);
 
   status->capacity = longs[0];
   status->charge_counter = longs[1];
diff --git a/src/android.h b/src/android.h
index 65389f84e6a..806be4f9954 100644
--- a/src/android.h
+++ b/src/android.h
@@ -86,6 +86,7 @@ extern jstring android_build_jstring (const char *);
 extern void android_exception_check (void);
 extern void android_exception_check_1 (jobject);
 extern void android_exception_check_2 (jobject, jobject);
+extern void android_exception_check_nonnull (void *, jobject);
 
 extern void android_get_keysym_name (int, char *, size_t);
 extern void android_wait_event (void);
diff --git a/src/androidselect.c b/src/androidselect.c
index 2d8f14bb90d..3947ab99166 100644
--- a/src/androidselect.c
+++ b/src/androidselect.c
@@ -178,7 +178,7 @@ Alternatively, return nil if the clipboard is empty.  */)
                                                bytes);
   data = (*android_java_env)->GetByteArrayElements (android_java_env,
                                                    bytes, NULL);
-  android_exception_check_1 (bytes);
+  android_exception_check_nonnull (data, bytes);
 
   string = make_unibyte_string ((char *) data, length);
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]