From a78af4b95e53603a47e9d7971fc37f5d06cf14d8 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 2 Aug 2024 00:27:40 -0700 Subject: [PATCH] =?utf8?q?Don=E2=80=99t=20assume=20mode=5Ft=20fits=20in=20?= =?utf8?q?unsigned=20long?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * 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 | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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 (); } -- 2.47.3