pack-redundant: fix crash when one packfile in repo

Command `git pack-redundant --all` will crash if there is only one packfile in the repository. This is because, if there is only one packfile in local_packs, `cmp_local_packs` will do nothing and will leave `pl->unique_objects` as uninitialized. Also add testcases for repository with no packfile and one packfile in t5323. Reported-by: Daniel C. Klauer <daniel.c.klauer@web.de> Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jiang Xin committed Dec 16, 2020 at 20:57 UTC 0696232390d237b64f970e538177ecfd979020d0
2 files changed +39 -4
builtin/pack-redundant.c
+6
@@ -473,6 +473,12 @@ static void cmp_local_packs(void)
473 {
474 struct pack_list *subset, *pl = local_packs;
475
476 + /* only one packfile */
477 + if (!pl->next) {
478 + llist_init(&pl->unique_objects);
479 + return;
480 + }
481 +
482 while ((subset = pl)) {
483 while ((subset = subset->next))
484 cmp_two_packs(pl, subset);
t/t5323-pack-redundant.sh
+33 -4
@@ -112,19 +112,28 @@ test_expect_success 'setup master repo' '
112 create_commits_in "$master_repo" A B C D E F G H I J K L M N O P Q R
113 '
114
115 +test_expect_success 'master: pack-redundant works with no packfile' '
116 + (
117 + cd "$master_repo" &&
118 + cat >expect <<-EOF &&
119 + fatal: Zero packs found!
120 + EOF
121 + test_must_fail git pack-redundant --all >actual 2>&1 &&
122 + test_cmp expect actual
123 + )
124 +'
125 +
126 #############################################################################
127 # Chart of packs and objects for this test case
128 #
129 # | T A B C D E F G H I J K L M N O P Q R
130 # ----+--------------------------------------
131 # P1 | x x x x x x x x
121 -# P2 | x x x x x x x
122 -# P3 | x x x x x x
132 # ----+--------------------------------------
124 -# ALL | x x x x x x x x x x x x x x x
133 +# ALL | x x x x x x x x
134 #
135 #############################################################################
127 -test_expect_success 'master: no redundant for pack 1, 2, 3' '
136 +test_expect_success 'master: pack-redundant works with one packfile' '
137 create_pack_in "$master_repo" P1 <<-EOF &&
138 $T
139 $A
@@ -135,6 +144,26 @@ test_expect_success 'master: no redundant for pack 1, 2, 3' '
144 $F
145 $R
146 EOF
147 + (
148 + cd "$master_repo" &&
149 + git pack-redundant --all >out &&
150 + test_must_be_empty out
151 + )
152 +'
153 +
154 +#############################################################################
155 +# Chart of packs and objects for this test case
156 +#
157 +# | T A B C D E F G H I J K L M N O P Q R
158 +# ----+--------------------------------------
159 +# P1 | x x x x x x x x
160 +# P2 | x x x x x x x
161 +# P3 | x x x x x x
162 +# ----+--------------------------------------
163 +# ALL | x x x x x x x x x x x x x x x
164 +#
165 +#############################################################################
166 +test_expect_success 'master: no redundant for pack 1, 2, 3' '
167 create_pack_in "$master_repo" P2 <<-EOF &&
168 $B
169 $C