{"thread":{"id":"66832","subject":"[PATCH] ref-filter: fix double free when an object is missing","startedAt":"2026-11-12T09:48:41Z","lastAt":"2026-11-12T23:13:43Z","messageCount":4,"participants":["Mustafa Bayar via B4 Relay","Karthik Nayak","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558283","messageId":"20261112-mb-ref-filter-missing-object-double-free-maint-v1-1-d6d56a3c8dbe@gitlab.com","threadId":"66832","inReplyTo":null,"subject":"[PATCH] ref-filter: fix double free when an object is missing","fromName":"Mustafa Bayar via B4 Relay","fromEmail":"devnull+mbayar.gitlab.com@kernel.org","sentAt":"2026-11-12T09:48:41Z","receivedAt":"2026-11-12T09:48:41Z","isPatch":true,"sender":{"key":"name:Mustafa Bayar","avatar":null},"body":"From: Mustafa Bayar <mbayar@gitlab.com>\n\nWhen a ref points to a missing object, git-for-each-ref(1) may abort\nwith a double free instead of reporting the missing object:\n\n    free(): double free detected in tcache 2\n\nget_object() frees `oi->content` but leaves the pointer unchanged. Since\nthe expand_data structures are reused across refs, a later call can\nstart with `content` pointing to a freed buffer. Then, if the object\nlookup in that call fails before it overwrites the pointer, the cleanup\npath frees the buffer a second time.\n\na29e2e8fe (ref-filter: parse objects on demand, 2025-10-23) introduced\nthis by making object lookup failures jump to the cleanup label.\nbea37f1d6 (ref-filter: fix stale parsed objects, 2025-11-04) later fixed\na related stale-state bug by resetting `maybe_object`.\n\nSimilar to `maybe_object`, reset `content` at the start of get_object().\nAdd a regression test that exercises this path.\n\nSigned-off-by: Mustafa Bayar <mbayar@gitlab.com>\n---\nThis is based on maint, since the bug first appeared in v2.53.0.\n\nWe ran into this on GitLab.com, where a repository with a tag pointing\nto a missing object made `git for-each-ref` abort instead of reporting\nthe missing object.\n\nA note on the test: the double free only happens when a valid annotated\ntag is processed before the broken ref, so that an earlier get_object()\ncall has already freed its content buffer. The test therefore names the\nrefs `annotated` and `missing`, which sort in that order.\n---\n ref-filter.c                   |  1 +\n t/t6301-for-each-ref-errors.sh | 15 +++++++++++++++\n 2 files changed, 16 insertions(+)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex bdf54f6f..6c4df521 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -2349,6 +2349,7 @@ static int get_object(struct ref_array_item *ref, int deref,\n \tint ret;\n \n \toi->maybe_object = NULL;\n+\toi->content = NULL;\n \n \tif (oi->info.contentp) {\n \t\t/* We need to know that to use parse_object_buffer properly */\ndiff --git a/t/t6301-for-each-ref-errors.sh b/t/t6301-for-each-ref-errors.sh\nindex 72b27c8b..e955f66a 100755\n--- a/t/t6301-for-each-ref-errors.sh\n+++ b/t/t6301-for-each-ref-errors.sh\n@@ -52,6 +52,21 @@ test_expect_success 'Missing objects are reported correctly' '\n \ttest_must_be_empty brief-err\n '\n \n+test_expect_success 'missing objects after a tag are reported correctly' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit --no-tag initial &&\n+\t\tgit tag -a -m annotated annotated &&\n+\t\ttest-tool ref-store main update-ref msg refs/tags/missing \"$MISSING\" \"$ZERO_OID\" REF_SKIP_OID_VERIFICATION &&\n+\t\tgit rev-parse HEAD >expect &&\n+\t\ttest_must_fail git for-each-ref --format=\"%(*objectname)\" refs/tags/ >actual 2>err &&\n+\t\ttest_cmp expect actual &&\n+\t\ttest_grep \"fatal: missing object $MISSING for refs/tags/missing\" err\n+\t)\n+'\n+\n test_expect_success 'missing ancestors are reported by contains filters' '\n \ttest_when_finished \"git update-ref -d refs/heads/missing-parent\" &&\n \t{\n\n---\nbase-commit: 03b41523c576f26ad34085378717a0c1b287470f\nchange-id: 20261112-mb-ref-filter-missing-object-double-free-maint-9eadbfe11e71\n\n\n\n"},{"id":"558294","messageId":"CAOLa=ZQuT5+x-Rvxcv1CSYka3aGG2nV_hPFRDTYJEGgjbLhJwA@mail.gmail.com","threadId":"66832","inReplyTo":"20261112-mb-ref-filter-missing-object-double-free-maint-v1-1-d6d56a3c8dbe@gitlab.com","subject":"Re: [PATCH] ref-filter: fix double free when an object is missing","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-11-12T13:44:30Z","receivedAt":"2026-11-12T13:44:30Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Mustafa Bayar via B4 Relay <devnull+mbayar.gitlab.com@kernel.org>\nwrites:\n\n> From: Mustafa Bayar <mbayar@gitlab.com>\n>\n> When a ref points to a missing object, git-for-each-ref(1) may abort\n> with a double free instead of reporting the missing object:\n>\n>     free(): double free detected in tcache 2\n>\n> get_object() frees `oi->content` but leaves the pointer unchanged. Since\n> the expand_data structures are reused across refs, a later call can\n> start with `content` pointing to a freed buffer. Then, if the object\n> lookup in that call fails before it overwrites the pointer, the cleanup\n> path frees the buffer a second time.\n>\n> a29e2e8fe (ref-filter: parse objects on demand, 2025-10-23) introduced\n> this by making object lookup failures jump to the cleanup label.\n> bea37f1d6 (ref-filter: fix stale parsed objects, 2025-11-04) later fixed\n> a related stale-state bug by resetting `maybe_object`.\n>\n> Similar to `maybe_object`, reset `content` at the start of get_object().\n> Add a regression test that exercises this path.\n>\n\nI had only reviewed this patch internally and we did some iterations\nthere [1], so mostly just rubber-stamping here that it looks good to me.\n\nThanks for working on this!\n\n[snip]\n\n[1]: https://gitlab.com/gitlab-org/git/-/merge_requests/737\n"},{"id":"558343","messageId":"xmqqqzgphjyb.fsf@gitster.g","threadId":"66832","inReplyTo":"CAOLa=ZQuT5+x-Rvxcv1CSYka3aGG2nV_hPFRDTYJEGgjbLhJwA@mail.gmail.com","subject":"Re: [PATCH] ref-filter: fix double free when an object is missing","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-12T20:29:48Z","receivedAt":"2026-11-12T20:29:48Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n>> Similar to `maybe_object`, reset `content` at the start of get_object().\n>> Add a regression test that exercises this path.\n>\n> I had only reviewed this patch internally and we did some iterations\n> there [1], so mostly just rubber-stamping here that it looks good to me.\n>\n> Thanks for working on this!\n\n\nI take the above with \"only\" -> \"already\".  Thanks for writing and\nreviewing.\n\nWill queue and mark the topic ready for 'next'.  The patch is very\nwell written, together with historical reference and all.\n\nThanks.\n\n\n"},{"id":"558362","messageId":"CAOLa=ZRnYo0CBw5acvu5m4bLjUZgeLv+Vf61kbnuN34eMHQr9w@mail.gmail.com","threadId":"66832","inReplyTo":"xmqqqzgphjyb.fsf@gitster.g","subject":"Re: [PATCH] ref-filter: fix double free when an object is missing","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-11-12T23:13:43Z","receivedAt":"2026-11-12T23:13:43Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>>> Similar to `maybe_object`, reset `content` at the start of get_object().\n>>> Add a regression test that exercises this path.\n>>\n>> I had only reviewed this patch internally and we did some iterations\n>> there [1], so mostly just rubber-stamping here that it looks good to me.\n>>\n>> Thanks for working on this!\n>\n>\n> I take the above with \"only\" -> \"already\".  Thanks for writing and\n> reviewing.\n>\n\nSigh! that's what I meant :)\n\n> Will queue and mark the topic ready for 'next'.  The patch is very\n> well written, together with historical reference and all.\n>\n> Thanks.\n"}]}