summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPo Lu <luangruo@yahoo.com>2023-10-29 12:59:45 +0800
committerPo Lu <luangruo@yahoo.com>2023-10-29 12:59:45 +0800
commit59a3edc3559057e6f0346e3f1b3b13e8ef3e1683 (patch)
treee39dd5ff3ea5fd4096035b7d71b6e4ebf138abf3
parent3624e9bd409075d4f78b240ebdb356f93fd9c3e4 (diff)
downloademacs-59a3edc3559057e6f0346e3f1b3b13e8ef3e1683.tar.gz
Avert a crash and file descriptor leak in yank-media
* java/org/gnu/emacs/EmacsNative.java (close): New declaration. * java/org/gnu/emacs/EmacsSdk11Clipboard.java (getClipboardData): Catch SecurityException and guarantee file descriptors are closed even if exceptions arise. * src/android.c (dup): Export another function.
-rw-r--r--java/org/gnu/emacs/EmacsNative.java3
-rw-r--r--java/org/gnu/emacs/EmacsSdk11Clipboard.java24
-rw-r--r--src/android.c8
3 files changed, 35 insertions, 0 deletions
diff --git a/java/org/gnu/emacs/EmacsNative.java b/java/org/gnu/emacs/EmacsNative.java
index 7d7e1e5d831..f15927bb3a7 100644
--- a/java/org/gnu/emacs/EmacsNative.java
+++ b/java/org/gnu/emacs/EmacsNative.java
@@ -39,6 +39,9 @@ public final class EmacsNative
/* Like `dup' in C. */
public static native int dup (int fd);
+ /* Like `close' in C. */
+ public static native int close (int fd);
+
/* Obtain the fingerprint of this build of Emacs. The fingerprint
can be used to determine the dump file name. */
public static native String getFingerprint ();
diff --git a/java/org/gnu/emacs/EmacsSdk11Clipboard.java b/java/org/gnu/emacs/EmacsSdk11Clipboard.java
index b8a43496b6d..b068a89831e 100644
--- a/java/org/gnu/emacs/EmacsSdk11Clipboard.java
+++ b/java/org/gnu/emacs/EmacsSdk11Clipboard.java
@@ -245,6 +245,8 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard
if (data == null || data.getItemCount () < 1)
return null;
+ fd = -1;
+
try
{
uri = data.getItemAt (0).getUri ();
@@ -267,12 +269,34 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard
/* Close the original offset. */
assetFd.close ();
}
+ catch (SecurityException e)
+ {
+ /* Guarantee a file descriptor duplicated or detached is
+ ultimately closed if an error arises. */
+
+ if (fd != -1)
+ EmacsNative.close (fd);
+
+ return null;
+ }
catch (FileNotFoundException e)
{
+ /* Guarantee a file descriptor duplicated or detached is
+ ultimately closed if an error arises. */
+
+ if (fd != -1)
+ EmacsNative.close (fd);
+
return null;
}
catch (IOException e)
{
+ /* Guarantee a file descriptor duplicated or detached is
+ ultimately closed if an error arises. */
+
+ if (fd != -1)
+ EmacsNative.close (fd);
+
return null;
}
diff --git a/src/android.c b/src/android.c
index 3344a773d5f..79f16568fd4 100644
--- a/src/android.c
+++ b/src/android.c
@@ -1260,6 +1260,14 @@ NATIVE_NAME (dup) (JNIEnv *env, jobject object, jint fd)
return dup (fd);
}
+JNIEXPORT jint JNICALL
+NATIVE_NAME (close) (JNIEnv *env, jobject object, jint fd)
+{
+ JNI_STACK_ALIGNMENT_PROLOGUE;
+
+ return close (fd);
+}
+
JNIEXPORT jstring JNICALL
NATIVE_NAME (getFingerprint) (JNIEnv *env, jobject object)
{