{"thread":{"id":"66574","subject":"[PATCH] bundle: do not free uninitialized memory on bad arguments","startedAt":"2026-10-18T14:21:29Z","lastAt":"2026-10-19T16:07:28Z","messageCount":6,"participants":["Olof Fairbrook","Patrick Steinhardt","Harald Nordgren","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"555348","messageId":"20261018142130.2701449-1-olof.fairbrook@gmail.com","threadId":"66574","inReplyTo":null,"subject":"[PATCH] bundle: do not free uninitialized memory on bad arguments","fromName":"Olof Fairbrook","fromEmail":"olof.fairbrook@gmail.com","sentAt":"2026-10-18T14:21:29Z","receivedAt":"2026-10-18T14:21:29Z","isPatch":true,"sender":{"key":"olof.fairbrook@gmail.com","avatar":null},"body":"\"git bundle create\" gives up with an error when setup_revisions()\nleaves an argument it does not understand, as in\n\n  $ git bundle create private.bundle main --no-such-option\n  error: unrecognized argument: --no-such-option\n\nSince 11ee9a75e7 (bundle: plug leaks in `create_bundle()`,\n2024-06-11), that error jumps to the cleanup at the end of\ncreate_bundle(), which clears \"revs_copy.pending\".  But \"revs_copy\"\nis only filled from \"revs\" further down, so the cleanup reads\nuninitialized stack memory and may free a garbage pointer.  valgrind\nreports \"Conditional jump or move depends on uninitialised value(s)\"\nin object_array_clear() and in free(); whether the command crashes\ndepends on what happens to be on the stack.\n\nGive “revs_copy” its value where it is declared, and add a test that\nfails under valgrind without this fix.\n\nSigned-off-by: Olof Fairbrook <olof.fairbrook@gmail.com>\n---\n bundle.c                | 2 +-\n t/t5607-clone-bundle.sh | 7 +++++++\n 2 files changed, 8 insertions(+), 1 deletion(-)\n\ndiff --git a/bundle.c b/bundle.c\nindex f55a521b2a..aad5f251ca 100644\n--- a/bundle.c\n+++ b/bundle.c\n@@ -492,7 +492,7 @@ int create_bundle(struct repository *r, const char *path,\n \tint bundle_fd = -1;\n \tint bundle_to_stdout;\n \tint ref_count = 0;\n-\tstruct rev_info revs, revs_copy;\n+\tstruct rev_info revs, revs_copy = { 0 };\n \tint min_version = 2;\n \tstruct bundle_prerequisites_info bpi;\n \tint ret;\ndiff --git a/t/t5607-clone-bundle.sh b/t/t5607-clone-bundle.sh\nindex d709bea753..c39db5a955 100755\n--- a/t/t5607-clone-bundle.sh\n+++ b/t/t5607-clone-bundle.sh\n@@ -127,6 +127,13 @@ test_expect_success 'failed bundle creation does not leave cruft' '\n \ttest_path_is_missing fail.bundle.lock\n '\n \n+test_expect_success 'an unknown argument after the revisions is an error' '\n+\ttest_must_fail git bundle create unknown.bundle main --no-such-option \\\n+\t\t2>err &&\n+\ttest_grep \"unrecognized argument: --no-such-option\" err &&\n+\ttest_path_is_missing unknown.bundle\n+'\n+\n test_expect_success 'fetch SHA-1 from bundle' '\n \ttest_create_repo foo &&\n \ttest_commit -C foo x &&\n\nbase-commit: a018953688f1b10bddf91bff8747068f5f4746a4\n-- \n2.43.0\n\n\n"},{"id":"555388","messageId":"atW95CyeiQCGtIMX@pks.im","threadId":"66574","inReplyTo":"20261018142130.2701449-1-olof.fairbrook@gmail.com","subject":"Re: [PATCH] bundle: do not free uninitialized memory on bad arguments","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-19T06:51:16Z","receivedAt":"2026-10-19T06:51:16Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Sun, Oct 18, 2026 at 04:21:29PM +0200, Olof Fairbrook wrote:\n> \"git bundle create\" gives up with an error when setup_revisions()\n> leaves an argument it does not understand, as in\n> \n>   $ git bundle create private.bundle main --no-such-option\n>   error: unrecognized argument: --no-such-option\n> \n> Since 11ee9a75e7 (bundle: plug leaks in `create_bundle()`,\n> 2024-06-11), that error jumps to the cleanup at the end of\n> create_bundle(), which clears \"revs_copy.pending\".  But \"revs_copy\"\n> is only filled from \"revs\" further down, so the cleanup reads\n> uninitialized stack memory and may free a garbage pointer.  valgrind\n> reports \"Conditional jump or move depends on uninitialised value(s)\"\n> in object_array_clear() and in free(); whether the command crashes\n> depends on what happens to be on the stack.\n\nNit: that last sentence is a bit on the verbose side, as it doesn't\nreally give any more useful information. I'd just drop it.\n\n> diff --git a/bundle.c b/bundle.c\n> index f55a521b2a..aad5f251ca 100644\n> --- a/bundle.c\n> +++ b/bundle.c\n> @@ -492,7 +492,7 @@ int create_bundle(struct repository *r, const char *path,\n>  \tint bundle_fd = -1;\n>  \tint bundle_to_stdout;\n>  \tint ref_count = 0;\n> -\tstruct rev_info revs, revs_copy;\n> +\tstruct rev_info revs, revs_copy = { 0 };\n>  \tint min_version = 2;\n>  \tstruct bundle_prerequisites_info bpi;\n>  \tint ret;\n\nRight. And for those wondering: we also release `revs` and `lock` on\nexit, but we always call `setup_revisions(&revs)` before any `goto out`\nand `lock` is initialized with `LOCK_INIT`. So both of those are fine.\n\n> diff --git a/t/t5607-clone-bundle.sh b/t/t5607-clone-bundle.sh\n> index d709bea753..c39db5a955 100755\n> --- a/t/t5607-clone-bundle.sh\n> +++ b/t/t5607-clone-bundle.sh\n> @@ -127,6 +127,13 @@ test_expect_success 'failed bundle creation does not leave cruft' '\n>  \ttest_path_is_missing fail.bundle.lock\n>  '\n>  \n> +test_expect_success 'an unknown argument after the revisions is an error' '\n> +\ttest_must_fail git bundle create unknown.bundle main --no-such-option \\\n> +\t\t2>err &&\n\nWrapping only for the redirect is a bit funny, but oh, well.\n\n> +\ttest_grep \"unrecognized argument: --no-such-option\" err &&\n> +\ttest_path_is_missing unknown.bundle\n> +'\n> +\n>  test_expect_success 'fetch SHA-1 from bundle' '\n>  \ttest_create_repo foo &&\n>  \ttest_commit -C foo x &&\n\nI think overall this looks good enough already, and the nits I have\nraised do not really need to be addressed.\n\nThanks!\n\nPatrick\n\n"},{"id":"555406","messageId":"CAHwyqnWxr3ZhPpM0WPCnJh-SHdGDZNE0ZJwA1jHhd+6-f0aEcw@mail.gmail.com","threadId":"66574","inReplyTo":"20261018142130.2701449-1-olof.fairbrook@gmail.com","subject":"Re: [PATCH] bundle: do not free uninitialized memory on bad arguments","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-19T09:40:40Z","receivedAt":"2026-10-19T09:40:40Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> -       struct rev_info revs, revs_copy;\n> +       struct rev_info revs, revs_copy = { 0 };\n\nI'm not familiar with this code, but the fix seems simple enough and\nhas a test. Looks good.\n\n\nHarald\n\n"},{"id":"555407","messageId":"c9d2099e-4eab-6be2-10a3-c30f33c88b8b@gmail.com","threadId":"66574","inReplyTo":"atW95CyeiQCGtIMX@pks.im","subject":"Re: [PATCH] bundle: do not free uninitialized memory on bad arguments","fromName":"Olof Fairbrook","fromEmail":"olof.fairbrook@gmail.com","sentAt":"2026-10-19T10:25:12Z","receivedAt":"2026-10-19T10:25:12Z","isPatch":true,"sender":{"key":"olof.fairbrook@gmail.com","avatar":null},"body":"\n\nOn Mon, 19 Oct 2026, Patrick Steinhardt wrote:\n\n>> in object_array_clear() and in free(); whether the command crashes\n>> depends on what happens to be on the stack.\n>\n> Nit: that last sentence is a bit on the verbose side, as it doesn't\n> really give any more useful information. I'd just drop it.\n\nYes, agreed. Just me over explaining. Will keep that in mind for future\nmessages on the list.\n\n\n>>  \ttest_commit -C foo x &&\n>\n> I think overall this looks good enough already, and the nits I have\n> raised do not really need to be addressed.\n>\n> Thanks!\n>\n> Patrick\n>\nThank you!\nOlof\n\n"},{"id":"555408","messageId":"947bafea-8f2b-9865-0dcf-c5de1c7853f9@gmail.com","threadId":"66574","inReplyTo":"CAHwyqnWxr3ZhPpM0WPCnJh-SHdGDZNE0ZJwA1jHhd+6-f0aEcw@mail.gmail.com","subject":"Re: [PATCH] bundle: do not free uninitialized memory on bad arguments","fromName":"Olof Fairbrook","fromEmail":"olof.fairbrook@gmail.com","sentAt":"2026-10-19T10:35:14Z","receivedAt":"2026-10-19T10:35:14Z","isPatch":true,"sender":{"key":"olof.fairbrook@gmail.com","avatar":null},"body":"\n\nOn Mon, 19 Oct 2026, Harald Nordgren wrote:\n\n>> -       struct rev_info revs, revs_copy;\n>> +       struct rev_info revs, revs_copy = { 0 };\n>\n> I'm not familiar with this code, but the fix seems simple enough and\n> has a test. Looks good.\n>\n>\n> Harald\n>\n\nThanks for the review!\nOlof\n\n"},{"id":"555433","messageId":"xmqq7bjd65dr.fsf@gitster.g","threadId":"66574","inReplyTo":"20261018142130.2701449-1-olof.fairbrook@gmail.com","subject":"Re: [PATCH] bundle: do not free uninitialized memory on bad arguments","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-19T16:07:28Z","receivedAt":"2026-10-19T16:07:28Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Olof Fairbrook <olof.fairbrook@gmail.com> writes:\n\n> -\tstruct rev_info revs, revs_copy;\n> +\tstruct rev_info revs, revs_copy = { 0 };\n\nObviously a sane and sensible thing to do.\n\nWill queue.  Thanks.\n\n"}]}