diff options
author | Patrick Steinhardt <ps@pks.im> | 2023-04-14 08:01:31 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2023-04-14 10:27:51 -0700 |
commit | ceb96a160b05c3ec45c416851bac00b711418839 (patch) | |
tree | f2063afd24bf25e6b4704b3abbbfdfba504185cb /midx.c | |
parent | 73876f4861cd3d187a4682290ab75c9dccadbc56 (diff) | |
download | git-ceb96a160b05c3ec45c416851bac00b711418839.tar.gz |
midx: fix segfault with no packs and invalid preferred pack
When asked to write a multi-pack-index the user can specify a preferred
pack that is used as a tie breaker when multiple packs contain the same
objects. When this packfile cannot be found, we just pick the first pack
that is getting tracked by the newly written multi-pack-index as a
fallback.
Picking the fallback can fail in the case where we're asked to write a
multi-pack-index with no packfiles at all: picking the fallback value
will cause a segfault as we blindly index into the array of packfiles,
which would be empty.
Fix this bug by resetting the preferred packfile index to `-1` before
searching for the preferred pack. This fixes the segfault as we already
check for whether the index is `> - 1`. If it is not, we simply don't
pick a preferred packfile at all.
Helped-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'midx.c')
-rw-r--r-- | midx.c | 6 |
1 files changed, 3 insertions, 3 deletions
@@ -1326,17 +1326,17 @@ static int write_midx_internal(const char *object_dir, } if (preferred_pack_name) { - int found = 0; + ctx.preferred_pack_idx = -1; + for (i = 0; i < ctx.nr; i++) { if (!cmp_idx_or_pack_name(preferred_pack_name, ctx.info[i].pack_name)) { ctx.preferred_pack_idx = i; - found = 1; break; } } - if (!found) + if (ctx.preferred_pack_idx == -1) warning(_("unknown preferred pack: '%s'"), preferred_pack_name); } else if (ctx.nr && |