]> git.eshelyaron.com Git - emacs.git/commitdiff
Avert a crash and file descriptor leak in yank-media
authorPo Lu <luangruo@yahoo.com>
Sun, 29 Oct 2023 04:59:45 +0000 (12:59 +0800)
committerPo Lu <luangruo@yahoo.com>
Sun, 29 Oct 2023 04:59:45 +0000 (12:59 +0800)
* 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.

java/org/gnu/emacs/EmacsNative.java
java/org/gnu/emacs/EmacsSdk11Clipboard.java
src/android.c

index 7d7e1e5d831f38ea4dfe7615fd77033d2f912866..f15927bb3a7c20a5cd654b77aaa210389c13dd82 100644 (file)
@@ -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 ();
index b8a43496b6d6ea15681e5433bb5de538f2881f2f..b068a89831e8ba66c58c1ef0022424397470931c 100644 (file)
@@ -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;
       }
 
index 3344a773d5f8b09ff47ade00d5ad3e08eefc2f53..79f16568fd4ae71742928f0832626b5ee44f0aea 100644 (file)
@@ -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)
 {