From 1521d3dae01f91606c639eb745ea565ef723e38d Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 12 Aug 2024 17:52:09 -0700 Subject: [PATCH] Avoid casts in tar_checksum * src/list.c (tar_checksum, from_header): Recode to avoid casts. --- src/list.c | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/list.c b/src/list.c index 6133b0c7..49e8e987 100644 --- a/src/list.c +++ b/src/list.c @@ -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) -- 2.47.2