From d21e84d1e72988bd55d43ea991dd365b7bd713cf Mon Sep 17 00:00:00 2001 From: Po Lu Date: Sun, 13 Aug 2023 09:10:07 +0800 Subject: [PATCH] Employ careadlinkat in getProcName * src/android.c (android_proc_name): Delete args BUFFER and SIZE. Return buffer allocated by careadlinkat. (NATIVE_NAME): Adjust correspondingly. --- src/android.c | 61 +++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/src/android.c b/src/android.c index b5b4359dcd3..70779f8ccae 100644 --- a/src/android.c +++ b/src/android.c @@ -18,31 +18,28 @@ You should have received a copy of the GNU General Public License along with GNU Emacs. If not, see . */ #include +#include +#include +#include +#include #include -#include -#include +#include +#include +#include #include -#include -#include -#include -#include #include -#include +#include +#include +#include #include -#include -#include -#include - -#include -#include +#include #include +#include +#include /* Old NDK versions lack MIN and MAX. */ #include -#include -#include - #include "android.h" #include "androidgui.h" @@ -1157,24 +1154,23 @@ android_get_home_directory (void) } /* Return the name of the file behind a file descriptor FD by reading - /proc/self/fd/. Place the name in BUFFER, which should be able to - hold size bytes. Value is 0 upon success, and 1 upon failure. */ + /proc/self/fd/. Value is allocated memory holding the file name + upon success, and 0 upon failure. */ -static int -android_proc_name (int fd, char *buffer, size_t size) +static char * +android_proc_name (int fd) { char format[sizeof "/proc/self/fd/" + INT_STRLEN_BOUND (int)]; - ssize_t read; + static struct allocator allocator = { + /* Fill the allocator with C library malloc functions. xmalloc + and so aren't thread safe. */ + malloc, realloc, free, NULL, + }; sprintf (format, "/proc/self/fd/%d", fd); - read = readlink (format, buffer, size - 1); - - if (read == -1) - return 1; - - buffer[read] = '\0'; - return 0; + return careadlinkat (AT_FDCWD, format, NULL, 0, + &allocator, readlinkat); } /* Try to guarantee the existence of the `lib' directory within the @@ -1465,11 +1461,12 @@ NATIVE_NAME (getProcName) (JNIEnv *env, jobject object, jint fd) { JNI_STACK_ALIGNMENT_PROLOGUE; - char buffer[PATH_MAX + 1]; + char *buffer; size_t length; jbyteArray array; - if (android_proc_name (fd, buffer, PATH_MAX + 1)) + buffer = android_proc_name (fd); + if (!buffer) return NULL; /* Return a byte array, as Java strings cannot always encode file @@ -1477,11 +1474,13 @@ NATIVE_NAME (getProcName) (JNIEnv *env, jobject object, jint fd) length = strlen (buffer); array = (*env)->NewByteArray (env, length); if (!array) - return NULL; + goto finish; (*env)->SetByteArrayRegion (env, array, 0, length, (jbyte *) buffer); + finish: + free (buffer); return array; } -- 2.39.2