From b6483b8a47fcc1f262751cecf32ca69b43df6a0d Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sun, 1 Sep 2024 14:09:07 +0300 Subject: [PATCH] tar: avoid casts in tar_checksum. * (tar_checksum): recode to avoid casts. * (tar_from_header): likewise. Sync with GNU tar 1521d3dae01f91606c639eb745ea565ef723e38d. Signed-off-by: Andrew Borodin --- src/vfs/tar/tar-internal.c | 5 ++++- src/vfs/tar/tar.c | 29 ++++++++++++++++------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/vfs/tar/tar-internal.c b/src/vfs/tar/tar-internal.c index 8ac3efb54..7ab124262 100644 --- a/src/vfs/tar/tar-internal.c +++ b/src/vfs/tar/tar-internal.c @@ -464,7 +464,10 @@ tar_from_header (const char *where0, size_t digs, char const *type, intmax_t min while (TRUE) { - value = (value << LG_256) + (unsigned char) *where++; + unsigned char uc; + + uc = *where++; + value = (value << LG_256) + uc; if (where == lim) break; diff --git a/src/vfs/tar/tar.c b/src/vfs/tar/tar.c index 0b3e54977..9d2c6f0de 100644 --- a/src/vfs/tar/tar.c +++ b/src/vfs/tar/tar.c @@ -327,39 +327,42 @@ tar_available_space_after (const union block *pointer) static read_header tar_checksum (const union block *header) { - size_t i; + int i; int unsigned_sum = 0; /* the POSIX one :-) */ int signed_sum = 0; /* the Sun one :-( */ int recorded_sum; - int parsed_sum; - const char *p = header->buffer; - for (i = sizeof (*header); i-- != 0;) + for (i = 0; i < sizeof (*header); i++) { - unsigned_sum += (unsigned char) *p; - signed_sum += (signed char) (*p++); + unsigned char uc = header->buffer[i]; + signed char sc = header->buffer[i]; + + unsigned_sum += uc; + signed_sum += sc; } if (unsigned_sum == 0) return HEADER_ZERO_BLOCK; /* Adjust checksum to count the "chksum" field as blanks. */ - for (i = sizeof (header->header.chksum); i-- != 0;) + for (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]; + signed char sc = header->header.chksum[i]; + + unsigned_sum -= uc; + signed_sum -= sc; } unsigned_sum += ' ' * sizeof (header->header.chksum); signed_sum += ' ' * sizeof (header->header.chksum); - parsed_sum = + recorded_sum = tar_from_header (header->header.chksum, sizeof (header->header.chksum), NULL, 0, INT_MAX, TRUE); - if (parsed_sum < 0) - return HEADER_FAILURE; - recorded_sum = parsed_sum; + if (recorded_sum < 0) + return HEADER_FAILURE; if (unsigned_sum != recorded_sum && signed_sum != recorded_sum) return HEADER_FAILURE;