]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
ls: port to wider off_t, uid_t, gid_t
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 26 Jul 2021 07:26:32 +0000 (00:26 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 26 Jul 2021 07:59:37 +0000 (00:59 -0700)
* 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

index efb87e405d81e0ae584734cc0c60019eb404c7a9..e442118ece44bb8f491037479896c8967266e803 100644 (file)
--- 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.  */