From c7fc1f11f0718e5675a0f4ef59c1b97661272273 Mon Sep 17 00:00:00 2001 From: Graham Percival Date: Fri, 28 Jan 2022 11:26:59 -0800 Subject: [PATCH 1/2] network_sleep: add sanity check for compilers Compilers and static analysis tools can't work out that sp is always non-NULL when we use it, due to the combination of: for (h = 0; h < sleepers_getsize(sleepers); h++) { sp = *sleepers_get(sleepers, h); ... } if (h == sleepers_getsize(sleepers)) { ... } /* Use sp. */ Reported by: gcc, clang, clang scan-build --- lib/network/tsnetwork_sleep.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/network/tsnetwork_sleep.c b/lib/network/tsnetwork_sleep.c index 72f73b6f..c66736d1 100644 --- a/lib/network/tsnetwork_sleep.c +++ b/lib/network/tsnetwork_sleep.c @@ -1,3 +1,4 @@ +#include #include #include @@ -41,7 +42,7 @@ network_sleep(struct timeval * timeo, network_callback * callback, void * cookie) { struct sleeper s; - struct sleeper * sp = NULL; /* Silence bogus gcc warning. */ + struct sleeper * sp = NULL; /* Silence bogus compiler warnings. */ size_t h; /* Initialize array if required. */ @@ -83,6 +84,12 @@ network_sleep(struct timeval * timeo, /* Append the record. */ if (sleepers_append(sleepers, &sp, 1)) goto err1; + } else { + /*- + * If (h != sleepers_getsize()), then sp was set in the + * earlier 'for' loop, but compilers don't realize it. + */ + assert(sp != NULL); } /* Register the timer event. */ From ba62ff9fc2142cc579a34ee08a6bf91cbc5c00e0 Mon Sep 17 00:00:00 2001 From: Graham Percival Date: Fri, 28 Jan 2022 11:27:00 -0800 Subject: [PATCH 2/2] Indicate that some variables are not read later in the function archive_read_format_tar_read_advance() is not part of libarchive (it's a tarsnap-specific addition). Reported by: clang scan-build --- libarchive/archive_read_support_format_tar.c | 1 + tar/multitape/multitape_metadata.c | 2 ++ tar/multitape/multitape_metaindex.c | 4 ++++ tests/valgrind/potential-memleaks.c | 2 ++ 4 files changed, 9 insertions(+) diff --git a/libarchive/archive_read_support_format_tar.c b/libarchive/archive_read_support_format_tar.c index c30057ea..6c86d832 100644 --- a/libarchive/archive_read_support_format_tar.c +++ b/libarchive/archive_read_support_format_tar.c @@ -538,6 +538,7 @@ archive_read_format_tar_read_advance(struct archive_read *a, off_t offset) if (tar->entry_padding >= offset) { tar->entry_padding -= offset; offset = 0; + (void)offset; /* not used beyond this point. */ } else { archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, "read_advance beyond end of entry"); diff --git a/tar/multitape/multitape_metadata.c b/tar/multitape/multitape_metadata.c index e5469c2b..4018f826 100644 --- a/tar/multitape/multitape_metadata.c +++ b/tar/multitape/multitape_metadata.c @@ -269,6 +269,8 @@ multitape_metadata_dec(struct tapemetadata * mdat, uint8_t * buf, buflen -= 256; p += 256; + (void)p; /* not used beyond this point. */ + /* We should be at the end of the metadata now. */ if (buflen != 0) goto bad2; diff --git a/tar/multitape/multitape_metaindex.c b/tar/multitape/multitape_metaindex.c index 2e2a1937..67ca5c00 100644 --- a/tar/multitape/multitape_metaindex.c +++ b/tar/multitape/multitape_metaindex.c @@ -106,6 +106,8 @@ multitape_metaindex_put(STORAGE_W * S, CHUNKS_W * C, memcpy(p, mind->tindex, mind->tindexlen); p += mind->tindexlen; + (void)p; /* not used beyond this point. */ + /* Compute hash of tape name. */ if (crypto_hash_data(CRYPTO_KEY_HMAC_NAME, (uint8_t *)mdat->name, strlen(mdat->name), hbuf)) @@ -273,6 +275,8 @@ multitape_metaindex_get(STORAGE_R * S, CHUNKS_S * C, if (buflen != 0) goto corrupt3; + (void)buf; /* not used beyond this point. */ + /* Free metaindex buffer. */ free(mbuf); diff --git a/tests/valgrind/potential-memleaks.c b/tests/valgrind/potential-memleaks.c index 7c5c9440..678b9a01 100644 --- a/tests/valgrind/potential-memleaks.c +++ b/tests/valgrind/potential-memleaks.c @@ -56,6 +56,8 @@ pl_freebsd_getpwuid(void) exit(1); } + (void)pwd; /* not used beyond this point. */ + /* POSIX says that we *shall not* free `pwd`. */ }