From 39673b6d4cffe6315d2a572c3741777b3f6a3d28 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 26 Jul 2021 00:26:32 -0700 Subject: [PATCH 6/7] ls: port to wider off_t, uid_t, gid_t * src/ls.c (dired_pos): Now off_t, not size_t, since it counts output file offsets. (dired_dump_obstack): This obstack's file offsets are now off_t, not size_t. (format_user_or_group, format_user_or_group_width): ID arg is now uintmax_t, not unsigned long, since uid_t and gid_t values might exceed ULONG_MAX. (format_user_or_group_width): Use snprintf with NULL instead of sprintf with a discarded buffer. This avoids a stack buffer, and so should be safer. --- src/ls.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ls.c b/src/ls.c index efb87e405..e442118ec 100644 --- a/src/ls.c +++ b/src/ls.c @@ -971,7 +971,7 @@ enum { MIN_COLUMN_WIDTH = 3 }; for each byte of output generated by this program so that the beginning and ending indices (in that output) of every file name can be recorded and later output themselves. */ -static size_t dired_pos; +static off_t dired_pos; static void dired_outbyte (char c) @@ -1076,10 +1076,13 @@ dired_dump_obstack (char const *prefix, struct obstack *os) n_pos = obstack_object_size (os) / sizeof (dired_pos); if (n_pos > 0) { - size_t *pos = (size_t *) obstack_finish (os); + off_t *pos = obstack_finish (os); fputs (prefix, stdout); for (size_t i = 0; i < n_pos; i++) - printf (" %lu", (unsigned long int) pos[i]); + { + intmax_t p = pos[i]; + printf (" %"PRIdMAX, p); + } putchar ('\n'); } } @@ -4187,7 +4190,7 @@ long_time_expected_width (void) print width of WIDTH columns. */ static void -format_user_or_group (char const *name, unsigned long int id, int width) +format_user_or_group (char const *name, uintmax_t id, int width) { if (name) { @@ -4200,7 +4203,7 @@ format_user_or_group (char const *name, unsigned long int id, int width) while (pad--); } else - dired_pos += printf ("%*lu ", width, id); + dired_pos += printf ("%*"PRIuMAX" ", width, id); } /* Print the name or id of the user with id U, using a print width of @@ -4225,7 +4228,7 @@ format_group (gid_t g, int width, bool stat_ok) /* Return the number of columns that format_user_or_group will print. */ static int -format_user_or_group_width (char const *name, unsigned long int id) +format_user_or_group_width (char const *name, uintmax_t id) { if (name) { @@ -4233,10 +4236,7 @@ format_user_or_group_width (char const *name, unsigned long int id) return MAX (0, len); } else - { - char buf[INT_BUFSIZE_BOUND (id)]; - return sprintf (buf, "%lu", id); - } + return snprintf (NULL, 0, "%"PRIuMAX, id); } /* Return the number of columns that format_user will print. */ -- 2.30.2