]> git.ipfire.org Git - thirdparty/git.git/commit - midx.c
midx: fix segfault with no packs and invalid preferred pack
authorPatrick Steinhardt <ps@pks.im>
Fri, 14 Apr 2023 06:01:31 +0000 (08:01 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 14 Apr 2023 17:27:51 +0000 (10:27 -0700)
commitceb96a160b05c3ec45c416851bac00b711418839
treef2063afd24bf25e6b4704b3abbbfdfba504185cb
parent73876f4861cd3d187a4682290ab75c9dccadbc56
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>
midx.c
t/t5319-multi-pack-index.sh