Don’t assume mode_t fits in unsigned long

* 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.
This commit is contained in:
Paul Eggert
2024-08-02 00:27:40 -07:00
parent c26111742a
commit a78af4b95e

View 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 ();
}