From: Paul Eggert Date: Fri, 2 Aug 2024 07:27:40 +0000 (-0700) Subject: Don’t assume mode_t fits in unsigned long X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a78af4b95e53603a47e9d7971fc37f5d06cf14d8;p=thirdparty%2Ftar.git 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. --- diff --git a/src/system.c b/src/system.c index 29aab4ea..99cfa403 100644 --- a/src/system.c +++ b/src/system.c @@ -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 (); }