]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Prefer int to idx_t for some small sizes
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 1 Nov 2024 17:37:39 +0000 (10:37 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 2 Nov 2024 06:47:23 +0000 (23:47 -0700)
* src/create.c (max_octal_val, to_octal, tar_copy_str)
(tar_name_copy_str, to_base256, to_chars_subst, to_chars)
(gid_to_chars, major_to_chars, minor_to_chars, mode_to_chars)
(off_to_chars, time_to_chars, uid_to_chars, string_to_chars)
(split_long_name, write_ustar_long_name, simple_finish_header):
* src/list.c (from_header, gid_from_header, major_from_header)
(minor_from_header, mode_from_header, off_from_header)
(time_from_header, uid_from_header):
Prefer int to idx_t where either will do because the buffer sizes
are known to be small, as this can be a performance win on 32-bit
platforms.  Also, in a few cases the values were negative, whereas
idx_t is supposed to be nonnegative.

src/common.h
src/create.c
src/list.c

index 0d5e7d6c104e446cea5e097f6b23cb1eda4afe1c..29aaf9dd6f7fec407d465b76897265abc450bff1 100644 (file)
@@ -529,8 +529,8 @@ enum exclusion_tag_type check_exclusion_tags (struct tar_stat_info const *st,
 #define OFF_TO_CHARS(val, where) off_to_chars (val, where, sizeof (where))
 #define TIME_TO_CHARS(val, where) time_to_chars (val, where, sizeof (where))
 
-bool off_to_chars (off_t off, char *buf, idx_t size);
-bool time_to_chars (time_t t, char *buf, idx_t size);
+bool off_to_chars (off_t off, char *buf, int size);
+bool time_to_chars (time_t t, char *buf, int size);
 
 /* Module diffarch.c.  */
 
@@ -616,7 +616,7 @@ void transform_stat_info (int typeflag, struct tar_stat_info *stat_info);
 char const *tartime (struct timespec t, bool full_time);
 
 #define OFF_FROM_HEADER(where) off_from_header (where, sizeof (where))
-off_t off_from_header (const char *buf, idx_t size);
+off_t off_from_header (const char *buf, int size);
 
 void list_archive (void);
 void test_archive_label (void);
index e948d13ee7bda7ad1b504b19180d14865fea186b..5b7b91ae8eea4ceccc56021cbad16fba563ed33c 100644 (file)
@@ -132,7 +132,7 @@ max_val_with_digits (int digits, int bits_per_digit)
 /* The maximum uintmax_t value that can be represented with octal
    digits and a trailing NUL in a buffer of size BUFSIZE.  */
 static uintmax_t
-max_octal_val (idx_t bufsize)
+max_octal_val (int bufsize)
 {
   return max_val_with_digits (bufsize - 1, LG_8);
 }
@@ -142,10 +142,10 @@ max_octal_val (idx_t bufsize)
    The result is undefined if SIZE is 0 or if VALUE is too large to fit.  */
 
 static void
-to_octal (uintmax_t value, char *where, idx_t size)
+to_octal (uintmax_t value, char *where, int size)
 {
   uintmax_t v = value;
-  idx_t i = size;
+  int i = size;
 
   do
     {
@@ -159,9 +159,9 @@ to_octal (uintmax_t value, char *where, idx_t size)
    NUL unless SRC is LEN or more bytes long.  */
 
 static void
-tar_copy_str (char *dst, const char *src, idx_t len)
+tar_copy_str (char *dst, const char *src, int len)
 {
-  for (idx_t i = 0; i < len; i++)
+  for (int i = 0; i < len; i++)
     if (! (dst[i] = src[i]))
       break;
 }
@@ -170,11 +170,11 @@ tar_copy_str (char *dst, const char *src, idx_t len)
    is OLDGNU format */
 
 static void
-tar_name_copy_str (char *dst, const char *src, idx_t len)
+tar_name_copy_str (char *dst, const char *src, int len)
 {
   tar_copy_str (dst, src, len);
   if (archive_format == OLDGNU_FORMAT)
-    dst[len-1] = 0;
+    dst[len - 1] = 0;
 }
 
 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
@@ -184,12 +184,12 @@ tar_name_copy_str (char *dst, const char *src, idx_t len)
    fit.  */
 
 static void
-to_base256 (bool negative, uintmax_t value, char *where, idx_t size)
+to_base256 (bool negative, uintmax_t value, char *where, int size)
 {
   uintmax_t v = value;
   uintmax_t sign_bits = - negative;
   uintmax_t propagated_sign_bits = sign_bits << (UINTMAX_WIDTH - LG_256);
-  idx_t i = size;
+  int i = size;
 
   do
     {
@@ -209,14 +209,14 @@ to_base256 (bool negative, uintmax_t value, char *where, idx_t size)
 #define GNAME_TO_CHARS(name, buf) string_to_chars (name, buf, sizeof (buf))
 
 static bool
-to_chars (bool negative, uintmax_t value, idx_t valsize,
+to_chars (bool negative, uintmax_t value, int valsize,
          uintmax_t (*substitute) (bool *),
-         char *where, idx_t size, const char *type);
+         char *where, int size, const char *type);
 
 static bool
-to_chars_subst (bool negative, bool gnu_format, uintmax_t value, idx_t valsize,
+to_chars_subst (bool negative, bool gnu_format, uintmax_t value, int valsize,
                uintmax_t (*substitute) (bool *),
-               char *where, idx_t size, const char *type)
+               char *where, int size, const char *type)
 {
   uintmax_t maxval = (gnu_format
                      ? max_val_with_digits (size - 1, LG_256)
@@ -267,9 +267,9 @@ to_chars_subst (bool negative, bool gnu_format, uintmax_t value, idx_t valsize,
    substitute value is negative.  */
 
 static bool
-to_chars (bool negative, uintmax_t value, idx_t valsize,
+to_chars (bool negative, uintmax_t value, int valsize,
          uintmax_t (*substitute) (bool *),
-         char *where, idx_t size, const char *type)
+         char *where, int size, const char *type)
 {
   bool gnu_format = (archive_format == GNU_FORMAT
                     || archive_format == OLDGNU_FORMAT);
@@ -340,25 +340,25 @@ gid_substitute (bool *negative)
 }
 
 static bool
-gid_to_chars (gid_t v, char *p, idx_t s)
+gid_to_chars (gid_t v, char *p, int s)
 {
   return to_chars (v < 0, v, sizeof v, gid_substitute, p, s, "gid_t");
 }
 
 static bool
-major_to_chars (major_t v, char *p, idx_t s)
+major_to_chars (major_t v, char *p, int s)
 {
   return to_chars (v < 0, v, sizeof v, 0, p, s, "major_t");
 }
 
 static bool
-minor_to_chars (minor_t v, char *p, idx_t s)
+minor_to_chars (minor_t v, char *p, int s)
 {
   return to_chars (v < 0, v, sizeof v, 0, p, s, "minor_t");
 }
 
 static bool
-mode_to_chars (mode_t v, char *p, idx_t s)
+mode_to_chars (mode_t v, char *p, int s)
 {
   /* In the common case where the internal and external mode bits are the same,
      and we are not using POSIX or GNU format,
@@ -398,13 +398,13 @@ mode_to_chars (mode_t v, char *p, idx_t s)
 }
 
 bool
-off_to_chars (off_t v, char *p, idx_t s)
+off_to_chars (off_t v, char *p, int s)
 {
   return to_chars (v < 0, v, sizeof v, 0, p, s, "off_t");
 }
 
 bool
-time_to_chars (time_t v, char *p, idx_t s)
+time_to_chars (time_t v, char *p, int s)
 {
   return to_chars (v < 0, v, sizeof v, 0, p, s, "time_t");
 }
@@ -426,13 +426,13 @@ uid_substitute (bool *negative)
 }
 
 static bool
-uid_to_chars (uid_t v, char *p, idx_t s)
+uid_to_chars (uid_t v, char *p, int s)
 {
   return to_chars (v < 0, v, sizeof v, uid_substitute, p, s, "uid_t");
 }
 
 static void
-string_to_chars (char const *str, char *p, idx_t s)
+string_to_chars (char const *str, char *p, int s)
 {
   tar_copy_str (p, str, s);
   p[s - 1] = '\0';
@@ -552,10 +552,10 @@ write_gnu_long_link (struct tar_stat_info *st, const char *p, char type)
   set_next_block_after (header + (size - 1) / BLOCKSIZE);
 }
 
-static idx_t
+static int
 split_long_name (const char *name, idx_t length)
 {
-  idx_t i;
+  int i;
 
   if (length > PREFIX_FIELD_SIZE + 1)
     length = PREFIX_FIELD_SIZE + 1;
@@ -580,8 +580,9 @@ write_ustar_long_name (const char *name)
       return NULL;
     }
 
-  idx_t i = split_long_name (name, length), nlen;
-  if (i == 0 || (nlen = length - i - 1) > NAME_FIELD_SIZE || nlen == 0)
+  int i = split_long_name (name, length);
+  idx_t nlen = length - i - 1;
+  if (i == 0 || ! (0 < nlen && nlen <= NAME_FIELD_SIZE))
     {
       paxerror (0, _("%s: file name is too long (cannot be split); not dumped"),
                quotearg_colon (name));
@@ -952,7 +953,7 @@ simple_finish_header (union block *header)
 
   int sum = 0;
   char *p = header->buffer;
-  for (idx_t i = sizeof *header; i-- != 0; )
+  for (int i = sizeof *header; i-- != 0; )
     /* We can't use unsigned char here because of old compilers, e.g. V7.  */
     sum += 0xFF & *p++;
 
index 4eecd293f0ba13d7e8cb461e079dcf651e91b1b2..3fe27dc078f0f3ef0b3fbca1c1a5a50f1bc0cabd 100644 (file)
@@ -42,13 +42,13 @@ static union block *recent_global_header; /* Recent global header block */
 #define TIME_FROM_HEADER(where) time_from_header (where, sizeof (where))
 #define UID_FROM_HEADER(where) uid_from_header (where, sizeof (where))
 
-static gid_t gid_from_header (const char *buf, idx_t size);
-static major_t major_from_header (const char *buf, idx_t size);
-static minor_t minor_from_header (const char *buf, idx_t size);
-static mode_t mode_from_header (const char *buf, idx_t size, bool *hbits);
-static time_t time_from_header (const char *buf, idx_t size);
-static uid_t uid_from_header (const char *buf, idx_t size);
-static intmax_t from_header (const char *, idx_t, const char *,
+static gid_t gid_from_header (const char *buf, int size);
+static major_t major_from_header (const char *buf, int size);
+static minor_t minor_from_header (const char *buf, int size);
+static mode_t mode_from_header (const char *buf, int size, bool *hbits);
+static time_t time_from_header (const char *buf, int size);
+static uid_t uid_from_header (const char *buf, int size);
+static intmax_t from_header (const char *, int, const char *,
                             intmax_t, uintmax_t, bool, bool);
 
 /* Table of base-64 digit values + 1, indexed by unsigned chars.
@@ -722,7 +722,7 @@ decode_header (union block *header, struct tar_stat_info *stat_info,
    numbers instead of the other GNU extensions.  Return -1 on error,
    diagnosing the error if TYPE is nonnull and if !SILENT.  */
 static intmax_t
-from_header (char const *where0, idx_t digs, char const *type,
+from_header (char const *where0, int digs, char const *type,
             intmax_t minval, uintmax_t maxval,
             bool octal_only, bool silent)
 {
@@ -943,7 +943,7 @@ from_header (char const *where0, idx_t digs, char const *type,
 }
 
 static gid_t
-gid_from_header (const char *p, idx_t s)
+gid_from_header (const char *p, int s)
 {
   return from_header (p, s, "gid_t",
                      TYPE_MINIMUM (gid_t), TYPE_MAXIMUM (gid_t),
@@ -951,7 +951,7 @@ gid_from_header (const char *p, idx_t s)
 }
 
 static major_t
-major_from_header (const char *p, idx_t s)
+major_from_header (const char *p, int s)
 {
   return from_header (p, s, "major_t",
                      TYPE_MINIMUM (major_t), TYPE_MAXIMUM (major_t),
@@ -959,7 +959,7 @@ major_from_header (const char *p, idx_t s)
 }
 
 static minor_t
-minor_from_header (const char *p, idx_t s)
+minor_from_header (const char *p, int s)
 {
   return from_header (p, s, "minor_t",
                      TYPE_MINIMUM (minor_t), TYPE_MAXIMUM (minor_t),
@@ -969,7 +969,7 @@ minor_from_header (const char *p, idx_t s)
 /* Convert P to the file mode, as understood by tar.
    Set *HBITS if there are any unrecognized bits.  */
 static mode_t
-mode_from_header (const char *p, idx_t s, bool *hbits)
+mode_from_header (const char *p, int s, bool *hbits)
 {
   intmax_t u = from_header (p, s, "mode_t",
                            INTMAX_MIN, UINTMAX_MAX,
@@ -991,7 +991,7 @@ mode_from_header (const char *p, idx_t s, bool *hbits)
 }
 
 off_t
-off_from_header (const char *p, idx_t s)
+off_from_header (const char *p, int s)
 {
   /* Negative offsets are not allowed in tar files, so invoke
      from_header with minimum value 0, not TYPE_MINIMUM (off_t).  */
@@ -1001,7 +1001,7 @@ off_from_header (const char *p, idx_t s)
 }
 
 static time_t
-time_from_header (const char *p, idx_t s)
+time_from_header (const char *p, int s)
 {
   return from_header (p, s, "time_t",
                      TYPE_MINIMUM (time_t), TYPE_MAXIMUM (time_t),
@@ -1009,7 +1009,7 @@ time_from_header (const char *p, idx_t s)
 }
 
 static uid_t
-uid_from_header (const char *p, idx_t s)
+uid_from_header (const char *p, int s)
 {
   return from_header (p, s, "uid_t",
                      TYPE_MINIMUM (uid_t), TYPE_MAXIMUM (uid_t),