]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Don’t assume mode_t fits in unsigned long
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 2 Aug 2024 07:27:40 +0000 (00:27 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 4 Aug 2024 08:41:43 +0000 (01:41 -0700)
* src/system.c (oct_to_env): Don’t assume mode_t fits in unsigned
long.  Do not output excess leading 1 bits.  When the mode is
zero, generate "0" rather than "00".  Use sprintf instead of
snprintf, since the output won’t be truncated; in general we don’t
use snprintf unless we want output to be truncated and truncation
is typically not GNU style.

src/system.c

index 29aab4ea1375c63f6f2234f701561b6791a56c80..99cfa4035a50c6131df8376021d76474e704ceb1 100644 (file)
@@ -677,11 +677,13 @@ time_to_env (char const *envar, struct timespec t)
 }
 
 static void
-oct_to_env (char const *envar, unsigned long num)
+oct_to_env (char const *envar, mode_t m)
 {
-  char buf[1+1+(sizeof(unsigned long)*CHAR_BIT+2)/3];
-
-  snprintf (buf, sizeof buf, "0%lo", num);
+  char buf[sizeof "0" + (UINTMAX_WIDTH + 2) / 3];
+  uintmax_t um = m;
+  if (EXPR_SIGNED (m) && sizeof m < sizeof um)
+    um &= ~ (UINTMAX_MAX << TYPE_WIDTH (m));
+  sprintf (buf, "%#"PRIoMAX, um);
   if (setenv (envar, buf, 1) != 0)
     xalloc_die ();
 }