]> Savannah Git Hosting - gnulib.git/commitdiff
immutable: Fix initialization failure on Android.
authorBruno Haible <bruno@clisp.org>
Tue, 10 Jan 2023 16:44:04 +0000 (17:44 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 10 Jan 2023 16:44:04 +0000 (17:44 +0100)
* lib/immutable.c: Include "pathmax.h".
(do_init_mmap_file): Use $TMPDIR rather than /tmp, if it is safe.
* modules/immutable (Depends-on): Add pathmax.

ChangeLog
lib/immutable.c
modules/immutable

index ab4a8f3a9fe5a16063093b7517ef96b291716b62..673da843c151c73c039e70168bd71902b7942cfe 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-01-10  Bruno Haible  <bruno@clisp.org>
+
+       immutable: Fix initialization failure on Android.
+       * lib/immutable.c: Include "pathmax.h".
+       (do_init_mmap_file): Use $TMPDIR rather than /tmp, if it is safe.
+       * modules/immutable (Depends-on): Add pathmax.
+
 2023-01-10  Bruno Haible  <bruno@clisp.org>
 
        Fix some test failures on Android ≥ 11.
index d5fb09b7d65eee88377f50a4f9721cc7140f606e..5881fc2252812ab787483e5d57ee8c1f9a2be8ba 100644 (file)
@@ -70,6 +70,12 @@ extern
 #  include <unistd.h>
 #  include <fcntl.h>
 
+/* Get PATH_MAX.  */
+#  include "pathmax.h"
+#  ifndef PATH_MAX
+#   define PATH_MAX 4096
+#  endif
+
 #  include "glthread/lock.h"
 
 # endif
@@ -116,14 +122,22 @@ static long file_length;
 static void
 do_init_mmap_file (void)
 {
-  char filename[100];
-  sprintf (filename, "%s/glimmdata-%d-%ld", "/tmp", getpid (), random ());
+  /* Use TMPDIR, except if it is too long.  */
+  const char *tmpdir = getenv ("TMPDIR");
+  if (tmpdir == NULL || strlen (tmpdir) > PATH_MAX)
+    tmpdir = "/tmp";
+  /* Now strlen (tmpdir) <= PATH_MAX.  */
+
+  char filename[PATH_MAX + 1 + 41 + 1];
+  sprintf (filename, "%s/glimmdata-%d-%ld", tmpdir, getpid (), random ());
+
   file_fd = open (filename, O_CREAT | O_TRUNC | O_RDWR | O_CLOEXEC, 0700);
   if (file_fd < 0)
     {
       fprintf (stderr, "glimm: Cannot open %s!\n", filename);
       abort ();
     }
+
   /* Remove the file from the file system as soon as possible, to make
      sure there is no leftover after this process terminates or crashes.  */
   unlink (filename);
index fef376ac0792a7d5b30d03c659016ea6ab157f96..0b9bc3a1613bd065b353ac99f47f50eae6cce7d7 100644 (file)
@@ -11,6 +11,7 @@ m4/nocrash.m4
 
 Depends-on:
 stdint
+pathmax
 open
 ssfmalloc