string-list API: change "nr" and "alloc" to "size_t"
Change the "nr" and "alloc" members of "struct string_list" to use
"size_t" instead of "nr". On some platforms the size of an "unsigned
int" will be smaller than a "size_t", e.g. a 32 bit unsigned v.s. 64
bit unsigned. As "struct string_list" is a generic API we use in a lot
of places this might cause overflows.
As one example: code in "refs.c" keeps track of the number of refs
with a "size_t", and auxiliary code in builtin/remote.c in
get_ref_states() appends those to a "struct string_list".
While we're at it split the "nr" and "alloc" in string-list.h across
two lines, which is the case for most such struct member
declarations (e.g. in "strbuf.h" and "strvec.h").
Changing e.g. "int i" to "size_t i" in run_and_feed_hook() isn't
strictly necessary, and there are a lot more cases where we'll use a
local "int", "unsigned int" etc. variable derived from the "nr" in the
"struct string_list". But in that case as well as
add_wrapped_shortlog_msg() in builtin/shortlog.c we need to adjust the
printf format referring to "nr" anyway, so let's also change the other
variables referring to it.
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committedMar 7, 2022 at 16:27 UTC99d60545f87445d7050999b826fc4cd49e69376c
9 files changed+35-31
builtin/receive-pack.c
+5-4
index d10aeb7e78..fc948a27c4 100644--- a/builtin/receive-pack.c+++ b/builtin/receive-pack.c@@ -813,13 +813,14 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, proc.trace2_hook_name = hook_name; if (feed_state->push_options) {- int i;+ size_t i; for (i = 0; i < feed_state->push_options->nr; i++) strvec_pushf(&proc.env_array,- "GIT_PUSH_OPTION_%d=%s", i,+ "GIT_PUSH_OPTION_%"PRIuMAX"=%s",+ (uintmax_t)i, feed_state->push_options->items[i].string);- strvec_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT=%d",- feed_state->push_options->nr);+ strvec_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT=%"PRIuMAX"",+ (uintmax_t)feed_state->push_options->nr); } else strvec_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT");
index a0bb687b0f..7608701a51 100644--- a/bundle.c+++ b/bundle.c@@ -255,18 +255,18 @@ int verify_bundle(struct repository *r, r = &header->references; printf_ln(Q_("The bundle contains this ref:",- "The bundle contains these %d refs:",+ "The bundle contains these %"PRIuMAX" refs:", r->nr),- r->nr);+ (uintmax_t)r->nr); list_refs(r, 0, NULL); r = &header->prerequisites; if (!r->nr) { printf_ln(_("The bundle records a complete history.")); } else { printf_ln(Q_("The bundle requires this ref:",- "The bundle requires these %d refs:",+ "The bundle requires these %"PRIuMAX" refs:", r->nr),- r->nr);+ (uintmax_t)r->nr); list_refs(r, 0, NULL); } }
commit-graph.c
+3-3
index 265c010122..e7731db8f4 100644--- a/commit-graph.c+++ b/commit-graph.c@@ -1690,10 +1690,10 @@ static int fill_oids_from_packs(struct write_commit_graph_context *ctx, dirlen = packname.len; if (ctx->report_progress) { strbuf_addf(&progress_title,- Q_("Finding commits for commit graph in %d pack",- "Finding commits for commit graph in %d packs",+ Q_("Finding commits for commit graph in %"PRIuMAX" pack",+ "Finding commits for commit graph in %"PRIuMAX" packs", pack_indexes->nr),- pack_indexes->nr);+ (uintmax_t)pack_indexes->nr); ctx->progress = start_delayed_progress(progress_title.buf, 0); ctx->progress_done = 0; }