]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Avoid casts in tar_checksum
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 13 Aug 2024 00:52:09 +0000 (17:52 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 15 Aug 2024 06:25:46 +0000 (23:25 -0700)
* src/list.c (tar_checksum, from_header):
Recode to avoid casts.

src/list.c

index 6133b0c7051efc8d52c37591208f6ef50247da56..49e8e987b726051a192be65d4070e8baf08d0456 100644 (file)
@@ -337,18 +337,13 @@ list_archive (void)
 enum read_header
 tar_checksum (union block *header, bool silent)
 {
-  size_t i;
   int unsigned_sum = 0;                /* the POSIX one :-) */
   int signed_sum = 0;          /* the Sun one :-( */
-  int recorded_sum;
-  int parsed_sum;
-  char *p;
 
-  p = header->buffer;
-  for (i = sizeof *header; i-- != 0;)
+  for (int i = 0; i < sizeof *header; i++)
     {
-      unsigned_sum += (unsigned char) *p;
-      signed_sum += (signed char) (*p++);
+      unsigned char uc = header->buffer[i]; unsigned_sum += uc;
+      signed   char sc = header->buffer[i];   signed_sum += sc;
     }
 
   if (unsigned_sum == 0)
@@ -356,22 +351,20 @@ tar_checksum (union block *header, bool silent)
 
   /* Adjust checksum to count the "chksum" field as blanks.  */
 
-  for (i = sizeof header->header.chksum; i-- != 0;)
+  for (int i = 0; i < sizeof header->header.chksum; i++)
     {
-      unsigned_sum -= (unsigned char) header->header.chksum[i];
-      signed_sum -= (signed char) (header->header.chksum[i]);
+      unsigned char uc = header->header.chksum[i]; unsigned_sum -= uc;
+      signed   char sc = header->header.chksum[i];   signed_sum -= sc;
     }
   unsigned_sum += ' ' * sizeof header->header.chksum;
-  signed_sum += ' ' * sizeof header->header.chksum;
+  signed_sum   += ' ' * sizeof header->header.chksum;
 
-  parsed_sum = from_header (header->header.chksum,
-                           sizeof header->header.chksum, 0,
-                           0, INT_MAX, true, silent);
-  if (parsed_sum < 0)
+  int recorded_sum = from_header (header->header.chksum,
+                                 sizeof header->header.chksum, 0,
+                                 0, INT_MAX, true, silent);
+  if (recorded_sum < 0)
     return HEADER_FAILURE;
 
-  recorded_sum = parsed_sum;
-
   if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
     return HEADER_FAILURE;
 
@@ -879,7 +872,8 @@ from_header (char const *where0, size_t digs, char const *type,
       value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
       for (;;)
        {
-         value = (value << LG_256) + (unsigned char) *where++;
+         unsigned char uc = *where++;
+         value = (value << LG_256) + uc;
          if (where == lim)
            break;
          if (((value << LG_256 >> LG_256) | topbits) != value)