Compare

da9c6e7e04c3ee8ab4e5ae014615163f3449221b..6272f3bd174fcd1b394d8b5e05b2ba384bd9f63e

mktree: plug per-tree leak in --batch mode

In --batch mode "git mktree" reuses its entry buffer across trees,
resetting `used` to 0 after writing each tree.  It never frees the
`treeent` structures the previous tree appended, though, so once the
next tree overwrites those slots the earlier allocations are leaked.  A
single-tree invocation hides this, as the entries stay reachable through
the `entries` global until exit.

Free each entry when resetting the buffer, and free the buffer itself
before returning.

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren <newren@gmail.com> · 2026-08-29 07:00 UTC · committed by Junio C Hamano

Browse files
19 files changed, +269 −145
modified builtin/mktree.c
@@ -200,8 +200,11 @@ int cmd_mktree(int ac,
200 200 puts(oid_to_hex(&oid));
201 201 fflush(stdout);
202 202 }
203 + for (int i = 0; i < used; i++)
204 + free(entries[i]);
203 205 used=0; /* reset tree entry buffer for re-use in batch mode */
204 206 }
207 + free(entries);
205 208 strbuf_release(&sb);
206 209
207 210 return 0;
modified builtin/pack-objects.c
@@ -1759,7 +1759,7 @@ static int want_object_in_pack_mtime(const struct object_id *oid,
1759 1759 struct odb_source *source = the_repository->objects->sources->next;
1760 1760 for (; source; source = source->next) {
1761 1761 struct odb_source_files *files = odb_source_files_downcast(source);
1762 - if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0))
1762 + if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0, NULL))
1763 1763 return 0;
1764 1764 }
1765 1765 }
@@ -1786,7 +1786,7 @@ static int want_object_in_pack_mtime(const struct object_id *oid,
1786 1786 struct multi_pack_index *m = get_multi_pack_index(files->packed);
1787 1787 struct pack_entry e;
1788 1788
1789 - if (m && fill_midx_entry(m, oid, &e)) {
1789 + if (m && fill_midx_entry(m, oid, &e, NULL)) {
1790 1790 want = want_object_in_pack_one(e.p, oid, exclude, found_pack, found_offset, found_mtime);
1791 1791 if (want != -1)
1792 1792 return want;
@@ -4171,7 +4171,7 @@ static void add_cruft_object_entry(const struct object_id *oid, enum object_type
4171 4171
4172 4172 for (; !found && source; source = source->next) {
4173 4173 struct odb_source_files *files = odb_source_files_downcast(source);
4174 - if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0))
4174 + if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0, NULL))
4175 4175 found = 1;
4176 4176 }
4177 4177
@@ -4637,7 +4637,7 @@ static int force_object_loose(struct odb_source *source,
4637 4637
4638 4638 for (struct odb_source *s = source->odb->sources; s; s = s->next) {
4639 4639 struct odb_source_files *files = odb_source_files_downcast(s);
4640 - if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0))
4640 + if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0, NULL))
4641 4641 return 0;
4642 4642 }
4643 4643
modified contrib/completion/git-completion.zsh
@@ -227,7 +227,6 @@ __git_zsh_main ()
227 227 '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
228 228 '(-p --paginate)--no-pager[do not pipe git output into a pager]' \
229 229 '--git-dir=-[set the path to the repository]: :_directories' \
230 - '*-C[run as if git was started in <path>]: :_directories' \
231 230 '--bare[treat the repository as a bare repository]' \
232 231 '(- :)--version[prints the git suite version]' \
233 232 '--exec-path=-[path to where your core git programs are installed]:: :_directories' \
@@ -252,29 +251,7 @@ __git_zsh_main ()
252 251 done
253 252 ;;
254 253 (arg)
255 - local command="${words[1]}" __git_dir __git_cmd_idx
256 - local -a __git_C_args
257 - local -i i=2
258 -
259 - while (( i <= $#orig_words )); do
260 - case ${orig_words[i]} in
261 - -C)
262 - __git_C_args+=(-C ${orig_words[i+1]})
263 - (( i++ ))
264 - ;;
265 - -c|--git-dir|--work-tree|--namespace)
266 - (( i++ ))
267 - ;;
268 - -*)
269 - ;;
270 - *)
271 - break
272 - ;;
273 - esac
274 - (( i++ ))
275 - done
276 -
277 - __git_cmd_idx=$(( i - 1 ))
254 + local command="${words[1]}" __git_dir __git_cmd_idx=1
278 255
279 256 if (( $+opt_args[--bare] )); then
280 257 __git_dir='.'
modified midx.c
@@ -591,7 +591,8 @@ uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
591 591
592 592 int fill_midx_entry(struct multi_pack_index *m,
593 593 const struct object_id *oid,
594 - struct pack_entry *e)
594 + struct pack_entry *e,
595 + struct packed_git **bad_pack)
595 596 {
596 597 uint32_t pos;
597 598 uint32_t pack_int_id;
@@ -618,8 +619,11 @@ int fill_midx_entry(struct multi_pack_index *m,
618 619 return 0;
619 620
620 621 if (oidset_size(&p->bad_objects) &&
621 - oidset_contains(&p->bad_objects, oid))
622 + oidset_contains(&p->bad_objects, oid)) {
623 + if (bad_pack && !*bad_pack)
624 + *bad_pack = p;
622 625 return 0;
626 + }
623 627
624 628 e->offset = nth_midxed_offset(m, pos);
625 629 e->p = p;
@@ -1028,7 +1032,7 @@ int verify_midx_file(struct odb_source_packed *source, unsigned flags)
1028 1032
1029 1033 nth_midxed_object_oid(&oid, m, pairs[i].pos);
1030 1034
1031 - if (!fill_midx_entry(m, &oid, &e)) {
1035 + if (!fill_midx_entry(m, &oid, &e, NULL)) {
1032 1036 midx_report(_("failed to load pack entry for oid[%d] = %s"),
1033 1037 pairs[i].pos, oid_to_hex(&oid));
1034 1038 continue;
modified midx.h
@@ -117,7 +117,8 @@ uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos);
117 117 struct object_id *nth_midxed_object_oid(struct object_id *oid,
118 118 struct multi_pack_index *m,
119 119 uint32_t n);
120 - int fill_midx_entry(struct multi_pack_index *m, const struct object_id *oid, struct pack_entry *e);
120 + int fill_midx_entry(struct multi_pack_index *m, const struct object_id *oid,
121 + struct pack_entry *e, struct packed_git **bad_pack);
121 122 int midx_contains_pack(struct multi_pack_index *m,
122 123 const char *idx_or_pack_name);
123 124 int midx_layer_contains_pack(struct multi_pack_index *m,
modified odb.c
@@ -15,7 +15,6 @@
15 15 #include "object-name.h"
16 16 #include "odb.h"
17 17 #include "odb/source-inmemory.h"
18 - #include "packfile.h"
19 18 #include "path.h"
20 19 #include "promisor-remote.h"
21 20 #include "quote.h"
@@ -547,12 +546,15 @@ static int register_all_submodule_sources(struct object_database *odb)
547 546 return ret;
548 547 }
549 548
550 - static int do_oid_object_info_extended(struct object_database *odb,
551 - const struct object_id *oid,
552 - struct object_info *oi, unsigned flags)
549 + static enum odb_read_status do_oid_object_info_extended(struct object_database *odb,
550 + const struct object_id *oid,
551 + struct object_info *oi, unsigned flags)
553 552 {
553 + struct strbuf corrupt_err = STRBUF_INIT;
554 554 const struct object_id *real = oid;
555 + enum odb_read_status ret;
555 556 int already_retried = 0;
557 + bool corrupt = false;
556 558
557 559 if (flags & OBJECT_INFO_LOOKUP_REPLACE)
558 560 real = lookup_replace_object(odb->repo, oid);
@@ -560,7 +562,7 @@ static int do_oid_object_info_extended(struct object_database *odb,
560 562 if (is_null_oid(real))
561 563 return -1;
562 564
563 - if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags))
565 + if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags, NULL))
564 566 return 0;
565 567
566 568 odb_prepare_alternates(odb);
@@ -568,9 +570,14 @@ static int do_oid_object_info_extended(struct object_database *odb,
568 570 while (1) {
569 571 struct odb_source *source;
570 572
571 - for (source = odb->sources; source; source = source->next)
572 - if (!odb_source_read_object_info(source, real, oi, flags))
573 - return 0;
573 + for (source = odb->sources; source; source = source->next) {
574 + ret = odb_source_read_object_info(source, real, oi, flags,
575 + corrupt_err.len ? NULL : &corrupt_err);
576 + if (!ret)
577 + goto out;
578 + if (ret != ODB_READ_NOT_FOUND)
579 + corrupt = true;
580 + }
574 581
575 582 /*
576 583 * When the object hasn't been found we try a second read and
@@ -578,10 +585,15 @@ static int do_oid_object_info_extended(struct object_database *odb,
578 585 * caches or reload on-disk state.
579 586 */
580 587 if (!(flags & OBJECT_INFO_QUICK)) {
581 - for (source = odb->sources; source; source = source->next)
582 - if (!odb_source_read_object_info(source, real, oi,
583 - flags | OBJECT_INFO_SECOND_READ))
584 - return 0;
588 + for (source = odb->sources; source; source = source->next) {
589 + ret = odb_source_read_object_info(source, real, oi,
590 + flags | OBJECT_INFO_SECOND_READ,
591 + corrupt_err.len ? NULL : &corrupt_err);
592 + if (!ret)
593 + goto out;
594 + if (ret != ODB_READ_NOT_FOUND)
595 + corrupt = true;
596 + }
585 597 }
586 598
587 599 /*
@@ -604,16 +616,23 @@ static int do_oid_object_info_extended(struct object_database *odb,
604 616 }
605 617
606 618 if (flags & OBJECT_INFO_DIE_IF_CORRUPT) {
607 - const struct packed_git *p;
608 619 if ((flags & OBJECT_INFO_LOOKUP_REPLACE) && !oideq(real, oid))
609 620 die(_("replacement %s not found for %s"),
610 621 oid_to_hex(real), oid_to_hex(oid));
611 - if ((p = has_packed_and_bad(odb->repo, real)))
612 - die(_("packed object %s (stored in %s) is corrupt"),
613 - oid_to_hex(real), p->pack_name);
622 + if (corrupt) {
623 + if (corrupt_err.len)
624 + die("%s", corrupt_err.buf);
625 + die(_("object %s is corrupt"), oid_to_hex(real));
626 + }
614 627 }
615 - return -1;
628 +
629 + ret = corrupt ? ODB_READ_ERROR : ODB_READ_NOT_FOUND;
630 + goto out;
616 631 }
632 +
633 + out:
634 + strbuf_release(&corrupt_err);
635 + return ret;
617 636 }
618 637
619 638 static int oid_object_info_convert(struct repository *r,
@@ -696,12 +715,12 @@ static int oid_object_info_convert(struct repository *r,
696 715 return ret;
697 716 }
698 717
699 - int odb_read_object_info_extended(struct object_database *odb,
700 - const struct object_id *oid,
701 - struct object_info *oi,
702 - enum object_info_flags flags)
718 + enum odb_read_status odb_read_object_info_extended(struct object_database *odb,
719 + const struct object_id *oid,
720 + struct object_info *oi,
721 + enum object_info_flags flags)
703 722 {
704 - int ret;
723 + enum odb_read_status ret;
705 724
706 725 if (oid->algo && (hash_algo_by_ptr(odb->repo->hash_algo) != oid->algo))
707 726 return oid_object_info_convert(odb->repo, oid, oi, flags);
modified odb.h
@@ -435,14 +435,23 @@ enum object_info_flags {
435 435 OBJECT_INFO_FOR_PREFETCH = (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK),
436 436 };
437 437
438 + enum odb_read_status {
439 + /* The read was successful. */
440 + ODB_READ_OK = 0,
441 + /* The read resulted in a generic error. */
442 + ODB_READ_ERROR = -1,
443 + /* The object could not be found. */
444 + ODB_READ_NOT_FOUND = -2,
445 + };
446 +
438 447 /*
439 448 * Read object info from the object database and populate the `object_info`
440 449 * structure. Returns 0 on success, a negative error code otherwise.
441 450 */
442 - int odb_read_object_info_extended(struct object_database *odb,
443 - const struct object_id *oid,
444 - struct object_info *oi,
445 - enum object_info_flags flags);
451 + enum odb_read_status odb_read_object_info_extended(struct object_database *odb,
452 + const struct object_id *oid,
453 + struct object_info *oi,
454 + enum object_info_flags flags);
446 455
447 456 /*
448 457 * Read a subset of object info for the given object ID. Returns an `enum
modified odb/source-files.c
@@ -59,18 +59,35 @@ static void odb_source_files_prepare(struct odb_source *source,
59 59 odb_source_prepare(&files->packed->base, flags);
60 60 }
61 61
62 - static int odb_source_files_read_object_info(struct odb_source *source,
63 - const struct object_id *oid,
64 - struct object_info *oi,
65 - enum object_info_flags flags)
62 + static enum odb_read_status odb_source_files_read_object_info(struct odb_source *source,
63 + const struct object_id *oid,
64 + struct object_info *oi,
65 + enum object_info_flags flags,
66 + struct strbuf *errmsg)
66 67 {
67 68 struct odb_source_files *files = odb_source_files_downcast(source);
69 + enum odb_read_status ret_packed, ret_loose;
68 70
69 - if (!odb_source_read_object_info(&files->packed->base, oid, oi, flags) ||
70 - !odb_source_read_object_info(&files->loose->base, oid, oi, flags))
71 + ret_packed = odb_source_read_object_info(&files->packed->base, oid, oi,
72 + flags, errmsg);
73 + if (!ret_packed)
71 74 return 0;
72 75
73 - return -1;
76 + ret_loose = odb_source_read_object_info(&files->loose->base, oid, oi, flags,
77 + ret_packed == ODB_READ_NOT_FOUND ? errmsg : NULL);
78 + if (!ret_loose)
79 + return 0;
80 +
81 + /*
82 + * Reading the packed object may have failed even though the object
83 + * exists, for example because it is corrupt. Report this failure to
84 + * the caller in case neither of the sources was able to read the
85 + * object, and prefer the error of the packed source in case both
86 + * reads have failed.
87 + */
88 + if (ret_packed != ODB_READ_NOT_FOUND)
89 + return ret_packed;
90 + return ret_loose;
74 91 }
75 92
76 93 static int odb_source_files_read_object_stream(struct odb_read_stream **out,
modified odb/source-inmemory.c
@@ -56,17 +56,18 @@ static void populate_object_info(struct odb_source_inmemory *source,
56 56 oi->source_infop->source = &source->base;
57 57 }
58 58
59 - static int odb_source_inmemory_read_object_info(struct odb_source *source,
60 - const struct object_id *oid,
61 - struct object_info *oi,
62 - enum object_info_flags flags UNUSED)
59 + static enum odb_read_status odb_source_inmemory_read_object_info(struct odb_source *source,
60 + const struct object_id *oid,
61 + struct object_info *oi,
62 + enum object_info_flags flags UNUSED,
63 + struct strbuf *errmsg UNUSED)
63 64 {
64 65 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
65 66 const struct inmemory_object *object;
66 67
67 68 object = find_cached_object(inmemory, oid);
68 69 if (!object)
69 - return -1;
70 + return ODB_READ_NOT_FOUND;
70 71
71 72 populate_object_info(inmemory, oi, object);
72 73 return 0;
modified odb/source-loose.c
@@ -67,7 +67,8 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
67 67 const char *path,
68 68 const struct object_id *oid,
69 69 struct object_info *oi,
70 - enum object_info_flags flags)
70 + enum object_info_flags flags,
71 + struct strbuf *errmsg)
71 72 {
72 73 int ret;
73 74 int fd;
@@ -91,11 +92,16 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
91 92 struct stat st;
92 93
93 94 if ((!oi || (!oi->disk_sizep && !oi->mtimep)) && (flags & OBJECT_INFO_QUICK)) {
94 - ret = quick_has_loose(loose, oid) ? 0 : -1;
95 + ret = quick_has_loose(loose, oid) ? 0 : ODB_READ_NOT_FOUND;
95 96 goto out;
96 97 }
97 98
98 99 if (lstat(path, &st) < 0) {
100 + if (errno == ENOENT) {
101 + ret = ODB_READ_NOT_FOUND;
102 + goto out;
103 + }
104 +
99 105 ret = -1;
100 106 goto out;
101 107 }
@@ -113,9 +119,12 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
113 119
114 120 fd = git_open(path);
115 121 if (fd < 0) {
116 - if (errno != ENOENT)
117 - error_errno(_("unable to open loose object %s"), oid_to_hex(oid));
118 - ret = -1;
122 + if (errno == ENOENT) {
123 + ret = ODB_READ_NOT_FOUND;
124 + goto out;
125 + }
126 +
127 + ret = error_errno(_("unable to open loose object %s"), oid_to_hex(oid));
119 128 goto out;
120 129 }
121 130
@@ -155,7 +164,7 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
155 164
156 165 if (parse_loose_header(hdr, oi) < 0) {
157 166 ret = error(_("unable to parse %s header"), oid_to_hex(oid));
158 - goto corrupt;
167 + goto out;
159 168 }
160 169
161 170 if (*oi->typep < 0)
@@ -165,7 +174,7 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
165 174 *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid);
166 175 if (!*oi->contentp) {
167 176 ret = -1;
168 - goto corrupt;
177 + goto out;
169 178 }
170 179 }
171 180
@@ -173,21 +182,19 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
173 182 case ULHR_BAD:
174 183 ret = error(_("unable to unpack %s header"),
175 184 oid_to_hex(oid));
176 - goto corrupt;
185 + goto out;
177 186 case ULHR_TOO_LONG:
178 187 ret = error(_("header for %s too long, exceeds %d bytes"),
179 188 oid_to_hex(oid), MAX_HEADER_LEN);
180 - goto corrupt;
189 + goto out;
181 190 }
182 191
183 192 ret = 0;
184 193
185 - corrupt:
186 - if (ret && (flags & OBJECT_INFO_DIE_IF_CORRUPT))
187 - die(_("loose object %s (stored in %s) is corrupt"),
188 - oid_to_hex(oid), path);
189 -
190 194 out:
195 + if (ret && ret != ODB_READ_NOT_FOUND && errmsg)
196 + strbuf_addf(errmsg, _("loose object %s (stored in %s) is corrupt"),
197 + oid_to_hex(oid), path);
191 198 if (stream_to_end)
192 199 git_inflate_end(stream_to_end);
193 200 if (map)
@@ -206,10 +213,11 @@ out:
206 213 return ret;
207 214 }
208 215
209 - static int odb_source_loose_read_object_info(struct odb_source *source,
210 - const struct object_id *oid,
211 - struct object_info *oi,
212 - enum object_info_flags flags)
216 + static enum odb_read_status odb_source_loose_read_object_info(struct odb_source *source,
217 + const struct object_id *oid,
218 + struct object_info *oi,
219 + enum object_info_flags flags,
220 + struct strbuf *errmsg)
213 221 {
214 222 struct odb_source_loose *loose = odb_source_loose_downcast(source);
215 223 static struct strbuf buf = STRBUF_INIT;
@@ -221,10 +229,10 @@ static int odb_source_loose_read_object_info(struct odb_source *source,
221 229 * second time.
222 230 */
223 231 if (flags & OBJECT_INFO_SECOND_READ)
224 - return -1;
232 + return ODB_READ_NOT_FOUND;
225 233
226 234 odb_loose_path(loose, &buf, oid);
227 - return read_object_info_from_path(loose, buf.buf, oid, oi, flags);
235 + return read_object_info_from_path(loose, buf.buf, oid, oi, flags, errmsg);
228 236 }
229 237
230 238 /*
@@ -421,7 +429,7 @@ static int for_each_object_wrapper_cb(const struct object_id *oid,
421 429 if (data->request) {
422 430 struct object_info oi = *data->request;
423 431
424 - if (read_object_info_from_path(data->loose, path, oid, &oi, 0) < 0)
432 + if (read_object_info_from_path(data->loose, path, oid, &oi, 0, NULL) < 0)
425 433 return -1;
426 434
427 435 return data->cb(oid, &oi, data->cb_data);
@@ -439,7 +447,7 @@ static int for_each_prefixed_object_wrapper_cb(const struct object_id *oid,
439 447 struct object_info oi = *data->request;
440 448
441 449 if (odb_source_read_object_info(&data->loose->base,
442 - oid, &oi, 0) < 0)
450 + oid, &oi, 0, NULL) < 0)
443 451 return -1;
444 452
445 453 return data->cb(oid, &oi, data->cb_data);
modified odb/source-packed.c
@@ -2,7 +2,9 @@
2 2 #include "abspath.h"
3 3 #include "chdir-notify.h"
4 4 #include "dir.h"
5 + #include "gettext.h"
5 6 #include "git-zlib.h"
7 + #include "hex.h"
6 8 #include "list-objects-filter-options.h"
7 9 #include "mergesort.h"
8 10 #include "midx.h"
@@ -10,21 +12,23 @@
10 12 #include "odb/streaming.h"
11 13 #include "packfile.h"
12 14 #include "pack-bitmap.h"
15 + #include "strbuf.h"
13 16
14 17 static int find_pack_entry(struct odb_source_packed *store,
15 18 const struct object_id *oid,
16 - struct pack_entry *e)
19 + struct pack_entry *e,
20 + struct packed_git **bad_pack)
17 21 {
18 22 struct packfile_list_entry *l;
19 23
20 24 odb_source_prepare(&store->base, 0);
21 - if (store->midx && fill_midx_entry(store->midx, oid, e))
25 + if (store->midx && fill_midx_entry(store->midx, oid, e, bad_pack))
22 26 return 1;
23 27
24 28 for (l = store->packs.head; l; l = l->next) {
25 29 struct packed_git *p = l->pack;
26 30
27 - if (!p->multi_pack_index && packfile_fill_entry(p, oid, e)) {
31 + if (!p->multi_pack_index && packfile_fill_entry(p, oid, e, bad_pack)) {
28 32 if (!store->skip_mru_updates)
29 33 packfile_list_prepend(&store->packs, p);
30 34 return 1;
@@ -34,12 +38,14 @@ static int find_pack_entry(struct odb_source_packed *store,
34 38 return 0;
35 39 }
36 40
37 - static int odb_source_packed_read_object_info(struct odb_source *source,
38 - const struct object_id *oid,
39 - struct object_info *oi,
40 - enum object_info_flags flags)
41 + static enum odb_read_status odb_source_packed_read_object_info(struct odb_source *source,
42 + const struct object_id *oid,
43 + struct object_info *oi,
44 + enum object_info_flags flags,
45 + struct strbuf *errmsg)
41 46 {
42 47 struct odb_source_packed *packed = odb_source_packed_downcast(source);
48 + struct packed_git *bad_pack = NULL;
43 49 struct pack_entry e;
44 50 int ret;
45 51
@@ -51,23 +57,45 @@ static int odb_source_packed_read_object_info(struct odb_source *source,
51 57 if (flags & OBJECT_INFO_SECOND_READ)
52 58 odb_source_prepare(source, ODB_PREPARE_FLUSH_CACHES);
53 59
54 - if (!find_pack_entry(packed, oid, &e))
55 - return 1;
60 + if (!find_pack_entry(packed, oid, &e, &bad_pack)) {
61 + /*
62 + * The lookup may have failed because the object is known to be
63 + * corrupt in one of the packfiles. Report the object as
64 + * corrupt instead of missing in that case.
65 + */
66 + if (bad_pack) {
67 + ret = -1;
68 + goto out;
69 + }
70 +
71 + ret = ODB_READ_NOT_FOUND;
72 + goto out;
73 + }
56 74
57 75 /*
58 76 * We know that the caller doesn't actually need the
59 77 * information below, so return early.
60 78 */
61 - if (!oi)
62 - return 0;
79 + if (!oi) {
80 + ret = 0;
81 + goto out;
82 + }
63 83
64 84 ret = packed_object_info(packed, e.p, e.offset, oi);
65 85 if (ret < 0) {
86 + bad_pack = e.p;
66 87 mark_bad_packed_object(e.p, oid);
67 - return -1;
88 + goto out;
68 89 }
69 90
70 - return 0;
91 + ret = 0;
92 +
93 + out:
94 + if (ret < 0 && bad_pack && errmsg)
95 + strbuf_addf(errmsg, _("packed object %s (stored in %s) is corrupt"),
96 + oid_to_hex(oid), bad_pack->pack_name);
97 +
98 + return ret;
71 99 }
72 100
73 101 static int odb_source_packed_read_object_stream(struct odb_read_stream **out,
@@ -77,7 +105,7 @@ static int odb_source_packed_read_object_stream(struct odb_read_stream **out,
77 105 struct odb_source_packed *packed = odb_source_packed_downcast(source);
78 106 struct pack_entry e;
79 107
80 - if (!find_pack_entry(packed, oid, &e))
108 + if (!find_pack_entry(packed, oid, &e, NULL))
81 109 return -1;
82 110
83 111 return packfile_read_object_stream(out, oid, e.p, e.offset);
@@ -583,7 +611,7 @@ static int odb_source_packed_freshen_object(struct odb_source *source,
583 611 timesp = &times;
584 612 }
585 613
586 - if (!find_pack_entry(packed, oid, &e))
614 + if (!find_pack_entry(packed, oid, &e, NULL))
587 615 return 0;
588 616 if (e.p->is_cruft)
589 617 return 0;
modified odb/source.h
@@ -27,6 +27,7 @@ enum odb_source_type {
27 27
28 28 struct object_id;
29 29 struct odb_read_stream;
30 + struct strbuf;
30 31 struct strvec;
31 32
32 33 /*
@@ -110,13 +111,17 @@ struct odb_source {
110 111 * second read in case they know that the first read would have
111 112 * already surfaced the object without reloading any on-disk state.
112 113 *
113 - * The callback is expected to return a negative error code in case
114 - * reading the object has failed, 0 otherwise.
114 + * The callback is expected to return an `enum odb_read_status`. Please
115 + * refer to the individual values that can be returned. In case reading
116 + * the object has failed with a generic error and `errmsg` is non-NULL,
117 + * the callback is expected to populate it with a human-readable
118 + * message that describes the failure.
115 119 */
116 - int (*read_object_info)(struct odb_source *source,
117 - const struct object_id *oid,
118 - struct object_info *oi,
119 - enum object_info_flags flags);
120 + enum odb_read_status (*read_object_info)(struct odb_source *source,
121 + const struct object_id *oid,
122 + struct object_info *oi,
123 + enum object_info_flags flags,
124 + struct strbuf *errmsg);
120 125
121 126 /*
122 127 * This callback is expected to create a new read stream that can be
@@ -340,14 +345,19 @@ static inline void odb_source_prepare(struct odb_source *source,
340 345
341 346 /*
342 347 * Read an object from the object database source identified by its object ID.
343 - * Returns 0 on success, a negative error code otherwise.
348 + * Please refer to `enum odb_read_status` for the individual error codes.
349 + *
350 + * In case reading the object has failed with a generic error and `errmsg` is
351 + * non-NULL it will be populated with a human-readable message that describes
352 + * the failure.
344 353 */
345 - static inline int odb_source_read_object_info(struct odb_source *source,
346 - const struct object_id *oid,
347 - struct object_info *oi,
348 - enum object_info_flags flags)
354 + static inline enum odb_read_status odb_source_read_object_info(struct odb_source *source,
355 + const struct object_id *oid,
356 + struct object_info *oi,
357 + enum object_info_flags flags,
358 + struct strbuf *errmsg)
349 359 {
350 - return source->read_object_info(source, oid, oi, flags);
360 + return source->read_object_info(source, oid, oi, flags, errmsg);
351 361 }
352 362
353 363 /*
modified packfile.c
@@ -985,23 +985,6 @@ void mark_bad_packed_object(struct packed_git *p, const struct object_id *oid)
985 985 oidset_insert(&p->bad_objects, oid);
986 986 }
987 987
988 - const struct packed_git *has_packed_and_bad(struct repository *r,
989 - const struct object_id *oid)
990 - {
991 - struct odb_source *source;
992 -
993 - for (source = r->objects->sources; source; source = source->next) {
994 - struct odb_source_files *files = odb_source_files_downcast(source);
995 - struct packfile_list_entry *e;
996 -
997 - for (e = files->packed->packs.head; e; e = e->next)
998 - if (oidset_contains(&e->pack->bad_objects, oid))
999 - return e->pack;
1000 - }
1001 -
1002 - return NULL;
1003 - }
1004 -
1005 988 off_t get_delta_base(struct packed_git *p,
1006 989 struct pack_window **w_curs,
1007 990 off_t *curpos,
@@ -1859,13 +1842,17 @@ int is_pack_valid(struct packed_git *p)
1859 1842
1860 1843 int packfile_fill_entry(struct packed_git *p,
1861 1844 const struct object_id *oid,
1862 - struct pack_entry *e)
1845 + struct pack_entry *e,
1846 + struct packed_git **bad_pack)
1863 1847 {
1864 1848 off_t offset;
1865 1849
1866 1850 if (oidset_size(&p->bad_objects) &&
1867 - oidset_contains(&p->bad_objects, oid))
1851 + oidset_contains(&p->bad_objects, oid)) {
1852 + if (bad_pack && !*bad_pack)
1853 + *bad_pack = p;
1868 1854 return 0;
1855 + }
1869 1856
1870 1857 offset = find_pack_entry_one(oid, p);
1871 1858 if (!offset)
@@ -1941,7 +1928,7 @@ int has_object_pack(struct repository *r, const struct object_id *oid)
1941 1928 odb_prepare_alternates(r->objects);
1942 1929 for (source = r->objects->sources; source; source = source->next) {
1943 1930 struct odb_source_files *files = odb_source_files_downcast(source);
1944 - if (!odb_source_read_object_info(&files->packed->base, oid, NULL, 0))
1931 + if (!odb_source_read_object_info(&files->packed->base, oid, NULL, 0, NULL))
1945 1932 return 1;
1946 1933 }
1947 1934
@@ -1962,7 +1949,7 @@ int has_object_kept_pack(struct repository *r, const struct object_id *oid,
1962 1949
1963 1950 for (; *cache; cache++) {
1964 1951 struct packed_git *p = *cache;
1965 - if (packfile_fill_entry(p, oid, &e))
1952 + if (packfile_fill_entry(p, oid, &e, NULL))
1966 1953 return 1;
1967 1954 }
1968 1955 }
modified packfile.h
@@ -294,7 +294,8 @@ off_t find_pack_entry_one(const struct object_id *oid, struct packed_git *);
294 294
295 295 int packfile_fill_entry(struct packed_git *p,
296 296 const struct object_id *oid,
297 - struct pack_entry *e);
297 + struct pack_entry *e,
298 + struct packed_git **bad_pack);
298 299
299 300 int is_pack_valid(struct packed_git *);
300 301 void *unpack_entry(struct repository *r, struct packed_git *, off_t,
@@ -328,7 +329,6 @@ int packed_object_info_with_index_pos(struct odb_source_packed *source,
328 329 uint32_t *maybe_index_pos, struct object_info *oi);
329 330
330 331 void mark_bad_packed_object(struct packed_git *, const struct object_id *);
331 - const struct packed_git *has_packed_and_bad(struct repository *, const struct object_id *);
332 332
333 333 int has_object_pack(struct repository *r, const struct object_id *oid);
334 334 int has_object_kept_pack(struct repository *r, const struct object_id *oid,
modified replay.c
@@ -327,6 +327,13 @@ static struct commit *pick_regular_commit(struct repository *repo,
327 327 merge_opt->ancestor = NULL;
328 328 merge_opt->branch2 = NULL;
329 329
330 + if (result->clean < 0) {
331 + error(_("merge of %s onto %s failed"),
332 + oid_to_hex(&pickme->object.oid),
333 + oid_to_hex(&replayed_base->object.oid));
334 + return NULL;
335 + }
336 +
330 337 if (!result->clean)
331 338 return NULL;
332 339
modified t/helper/test-read-midx.c
@@ -82,7 +82,7 @@ static int read_midx_file(const char *object_dir, const char *checksum,
82 82 for (i = 0; i < m->num_objects; i++) {
83 83 nth_midxed_object_oid(&oid, m,
84 84 i + m->num_objects_in_base);
85 - fill_midx_entry(m, &oid, &e);
85 + fill_midx_entry(m, &oid, &e, NULL);
86 86
87 87 printf("%s %"PRIu64"\t%s\n",
88 88 oid_to_hex(&oid), e.offset, e.p->pack_name);
modified t/t1060-object-corruption.sh
@@ -145,4 +145,22 @@ test_expect_success 'partial clone of corrupted repository' '
145 145 test_must_fail git -C corrupt-partial checkout --force
146 146 '
147 147
148 + test_expect_success 'corrupted loose commit can be read from alternate' '
149 + git init repo-a &&
150 + tree=$(git -C repo-a write-tree) &&
151 + commit=$(git -C repo-a commit-tree $tree </dev/null) &&
152 +
153 + cp -r repo-a repo-b &&
154 + (
155 + cd repo-b &&
156 + echo ../../../repo-a/.git/objects >.git/objects/info/alternates &&
157 + corrupt_byte "$commit" 1
158 + ) &&
159 +
160 + git -C repo-a cat-file -p "$commit" >expect &&
161 + git -C repo-b cat-file -p "$commit" >actual 2>err &&
162 + test_cmp expect actual &&
163 + test_grep "inflate: data stream error" err
164 + '
165 +
148 166 test_done
modified t/t3650-replay-basics.sh
@@ -565,4 +565,38 @@ test_expect_success '--onto with --ref rejects multiple revision ranges' '
565 565 test_grep "cannot be used with multiple revision ranges" err
566 566 '
567 567
568 + test_expect_success 'replay fails without segfault when objects are missing' '
569 + test_when_finished "rm -fr unreadable" &&
570 + git init unreadable &&
571 + (
572 + cd unreadable &&
573 +
574 + test_write_lines l1 l2 l3 l4 l5 l6 l7 l8 >f &&
575 + git add f &&
576 + git commit -m base &&
577 + git branch base &&
578 +
579 + test_write_lines l1 l2 l3 l4 l5 l6 l7 CHANGED >f &&
580 + git commit -am side &&
581 + git branch side &&
582 +
583 + git switch -c onto base &&
584 + test_write_lines CHANGED l2 l3 l4 l5 l6 l7 l8 >f &&
585 + git commit -am onto &&
586 +
587 + # The replay works while every object is readable.
588 + git replay --onto onto base..side &&
589 +
590 + # Removing the onto tree makes parse_tree() fail during the
591 + # incore merge, driving clean < 0 with a NULL result tree.
592 + onto_tree=$(git rev-parse onto^{tree}) &&
593 + obj=$(test_oid_to_path "$onto_tree") &&
594 + mv .git/objects/${obj} saved-tree &&
595 +
596 + # Ensure replay gracefully handles the missing object
597 + test_must_fail git replay --onto onto base..side 2>err &&
598 + test_grep -e "Could not read" -e "collecting merge info failed" err
599 + )
600 + '
601 +
568 602 test_done
modified t/unit-tests/u-odb-inmemory.c
@@ -29,7 +29,7 @@ static void cl_assert_object_info(struct odb_source_inmemory *source,
29 29 .contentp = &actual_content,
30 30 };
31 31
32 - cl_must_pass(odb_source_read_object_info(&source->base, oid, &oi, 0));
32 + cl_must_pass(odb_source_read_object_info(&source->base, oid, &oi, 0, NULL));
33 33 cl_assert_equal_u(actual_size, strlen(expected_content));
34 34 cl_assert_equal_u(actual_type, expected_type);
35 35 cl_assert_equal_s((char *) actual_content, expected_content);
@@ -72,7 +72,8 @@ void test_odb_inmemory__read_missing_object(void)
72 72 const char *end;
73 73
74 74 cl_must_pass(parse_oid_hex_algop(RANDOM_OID, &oid, &end, repo.hash_algo));
75 - cl_must_fail(odb_source_read_object_info(&source->base, &oid, NULL, 0));
75 + cl_assert_equal_i(odb_source_read_object_info(&source->base, &oid, NULL, 0, NULL),
76 + ODB_READ_NOT_FOUND);
76 77
77 78 odb_source_free(&source->base);
78 79 }