From c99d8d0a086efb6073417b8c9104fc30a98e72db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Tue, 24 Jan 2023 16:05:20 +0100 Subject: [PATCH] Avoid comparisons of different signs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Comparisons if different signedness can result in unexpected results. Add casts to ensure operants are of the same type. gettime.c: In function 'gettime': gettime.c:58:26: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'time_t' {aka 'long int'} [-Wsign-compare] 58 | } else if (epoch > fallback) { | ^ Cast to time_t, since epoch is less than ULONG_MAX at this point. idmapping.c: In function 'write_mapping': idmapping.c:202:48: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare] 202 | if ((written <= 0) || (written >= (bufsize - (pos - buf)))) { | ^~ newgidmap.c: In function ‘main’: newgidmap.c:178:40: warning: comparison of integer expressions of different signedness: ‘int’ and ‘long unsigned int’ [-Wsign-compare] 178 | if ((written <= 0) || (written >= sizeof(proc_dir_name))) { | ^~ newuidmap.c: In function ‘main’: newuidmap.c:107:40: warning: comparison of integer expressions of different signedness: ‘int’ and ‘long unsigned int’ [-Wsign-compare] 107 | if ((written <= 0) || (written >= sizeof(proc_dir_name))) { | ^~ --- libmisc/gettime.c | 2 +- libmisc/idmapping.c | 2 +- src/newgidmap.c | 2 +- src/newuidmap.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libmisc/gettime.c b/libmisc/gettime.c index b2884022..5bdc71c1 100644 --- a/libmisc/gettime.c +++ b/libmisc/gettime.c @@ -55,7 +55,7 @@ fprintf (shadow_logfd, _("Environment variable $SOURCE_DATE_EPOCH: value must be smaller than or equal to %lu but was found to be: %llu\n"), ULONG_MAX, epoch); - } else if (epoch > fallback) { + } else if ((time_t)epoch > fallback) { fprintf (shadow_logfd, _("Environment variable $SOURCE_DATE_EPOCH: value must be smaller than or equal to the current time (%lu) but was found to be: %llu\n"), fallback, epoch); diff --git a/libmisc/idmapping.c b/libmisc/idmapping.c index 30eb89fd..64985f82 100644 --- a/libmisc/idmapping.c +++ b/libmisc/idmapping.c @@ -199,7 +199,7 @@ void write_mapping(int proc_dir_fd, int ranges, const struct map_range *mappings mapping->upper, mapping->lower, mapping->count); - if ((written <= 0) || (written >= (bufsize - (pos - buf)))) { + if ((written <= 0) || ((size_t)written >= (bufsize - (pos - buf)))) { fprintf(log_get_logfd(), _("%s: snprintf failed!\n"), log_get_progname()); exit(EXIT_FAILURE); } diff --git a/src/newgidmap.c b/src/newgidmap.c index 5b42431b..01d0fe90 100644 --- a/src/newgidmap.c +++ b/src/newgidmap.c @@ -175,7 +175,7 @@ int main(int argc, char **argv) /* max string length is 6 + 10 + 1 + 1 = 18, allocate 32 bytes */ written = snprintf(proc_dir_name, sizeof(proc_dir_name), "/proc/%u/", target); - if ((written <= 0) || (written >= sizeof(proc_dir_name))) { + if ((written <= 0) || ((size_t)written >= sizeof(proc_dir_name))) { fprintf(stderr, "%s: snprintf of proc path failed: %s\n", Prog, strerror(errno)); } diff --git a/src/newuidmap.c b/src/newuidmap.c index 546856a2..e8798409 100644 --- a/src/newuidmap.c +++ b/src/newuidmap.c @@ -104,7 +104,7 @@ int main(int argc, char **argv) /* max string length is 6 + 10 + 1 + 1 = 18, allocate 32 bytes */ written = snprintf(proc_dir_name, sizeof(proc_dir_name), "/proc/%u/", target); - if ((written <= 0) || (written >= sizeof(proc_dir_name))) { + if ((written <= 0) || ((size_t)written >= sizeof(proc_dir_name))) { fprintf(stderr, "%s: snprintf of proc path failed: %s\n", Prog, strerror(errno)); }