commit ccf6dd5ee6c1143dcf745ddc604e192389882a38 from: Stefan Sperling date: Sat Dec 19 14:09:38 2020 UTC avoid signed vs unsigned comparisons in fetch.c ssizeof() macro idea courtesy of millert@ ok millert@ commit - c156c7a4f456c171f9e458793a2baa06389f8e1e commit + ccf6dd5ee6c1143dcf745ddc604e192389882a38 blob - 91dc86ae797191eff2211c2555318120b1dcbbff blob + 0aab76f5fc3a5dc2a5b75368480b9f3e9e512f59 --- lib/fetch.c +++ lib/fetch.c @@ -65,6 +65,10 @@ #ifndef nitems #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) +#endif + +#ifndef ssizeof +#define ssizeof(_x) ((ssize_t)(sizeof(_x))) #endif #ifndef MIN @@ -129,7 +133,7 @@ dial_ssh(pid_t *fetchpid, int *fetchfd, const char *ho dup2(pfd[0], 0); dup2(pfd[0], 1); n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction); - if (n < 0 || n >= sizeof(cmd)) + if (n < 0 || n >= ssizeof(cmd)) err(1, "snprintf"); if (execv(GOT_FETCH_PATH_SSH, argv) == -1) err(1, "execl"); @@ -669,18 +673,18 @@ got_fetch_pack(struct got_object_id **pack_hash, struc free(*pack_hash); *pack_hash = NULL; goto done; - } else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) { + } else if (packfile_size < ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) { err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file"); goto done; } else { ssize_t n; - n = read(packfd, &pack_hdr, sizeof(pack_hdr)); + n = read(packfd, &pack_hdr, ssizeof(pack_hdr)); if (n == -1) { err = got_error_from_errno("read"); goto done; } - if (n != sizeof(pack_hdr)) { + if (n != ssizeof(pack_hdr)) { err = got_error(GOT_ERR_IO); goto done; } @@ -696,11 +700,11 @@ got_fetch_pack(struct got_object_id **pack_hash, struc } nobj = be32toh(pack_hdr.nobjects); if (nobj == 0 && - packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) + packfile_size > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) return got_error_msg(GOT_ERR_BAD_PACKFILE, "bad pack file with zero objects"); if (nobj != 0 && - packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) + packfile_size <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) return got_error_msg(GOT_ERR_BAD_PACKFILE, "empty pack file with non-zero object count"); }