{"thread":{"id":"56125","subject":"[PATCH] multi-pack-index: fix potential segfault without sub-command","startedAt":"2021-07-19T17:37:43Z","lastAt":"2021-07-23T07:34:30Z","messageCount":4,"participants":["Taylor Blau","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"430521","messageId":"8c0bb3e0dc121bd68f7014000fbb60b28750a0fe.1626715096.git.me@ttaylorr.com","threadId":"56125","inReplyTo":null,"subject":"[PATCH] multi-pack-index: fix potential segfault without sub-command","fromName":"Taylor Blau","fromEmail":"me@ttaylorr.com","sentAt":"2021-07-19T17:18:49Z","receivedAt":"2021-07-19T17:37:43Z","isPatch":true,"sender":{"key":"me@ttaylorr.com","avatar":"https://avatars.githubusercontent.com/u/301000140?v=4"},"body":"Since cd57bc41bb (builtin/multi-pack-index.c: display usage on\nunrecognized command, 2021-03-30) we have used a \"usage\" label to avoid\nhaving two separate callers of usage_with_options (one when no arguments\nare given, and another for unrecognized sub-commands).\n\nBut the first caller has been broken since cd57bc41bb, since it will\nhappily jump to usage without arguments, and then pass argv[0] to the\n\"unrecognized subcommand\" error.\n\nMany compilers will save us from a segfault here, but the end result is\nugly, since it mentions an unrecognized subcommand when we didn't even\npass one, and (on GCC) includes \"(null)\" in its output.\n\nMove the \"usage\" label down past the error about unrecognized\nsubcommands so that it is only triggered when it should be. While we're\nat it, bulk up our test coverage in this area, too.\n\nSigned-off-by: Taylor Blau <me@ttaylorr.com>\n---\nNoticed this while I was reading code in a similar area while reviewing\none of Ævar's series.\n\n builtin/multi-pack-index.c  | 2 +-\n t/t5319-multi-pack-index.sh | 5 +++++\n 2 files changed, 6 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c\nindex 5d3ea445fd..8ff0dee2ec 100644\n--- a/builtin/multi-pack-index.c\n+++ b/builtin/multi-pack-index.c\n@@ -176,8 +176,8 @@ int cmd_multi_pack_index(int argc, const char **argv,\n \telse if (!strcmp(argv[0], \"expire\"))\n \t\treturn cmd_multi_pack_index_expire(argc, argv);\n \telse {\n-usage:\n \t\terror(_(\"unrecognized subcommand: %s\"), argv[0]);\n+usage:\n \t\tusage_with_options(builtin_multi_pack_index_usage,\n \t\t\t\t   builtin_multi_pack_index_options);\n \t}\ndiff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh\nindex 5641d158df..dab7123b3a 100755\n--- a/t/t5319-multi-pack-index.sh\n+++ b/t/t5319-multi-pack-index.sh\n@@ -824,4 +824,9 @@ test_expect_success 'load reverse index when missing .idx, .pack' '\n \t)\n '\n\n+test_expect_success 'usage shown without sub-command' '\n+\ttest_expect_code 129 git multi-pack-index 2>err &&\n+\t! test_i18ngrep \"unrecognized subcommand\" err\n+'\n+\n test_done\n--\n2.31.1.163.ga65ce7f831\n"},{"id":"430744","messageId":"YPfWkzRtQKthOgZx@coredump.intra.peff.net","threadId":"56125","inReplyTo":"8c0bb3e0dc121bd68f7014000fbb60b28750a0fe.1626715096.git.me@ttaylorr.com","subject":"Re: [PATCH] multi-pack-index: fix potential segfault without sub-command","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2021-07-21T08:10:59Z","receivedAt":"2021-07-21T08:13:57Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Jul 19, 2021 at 01:18:49PM -0400, Taylor Blau wrote:\n\n> Since cd57bc41bb (builtin/multi-pack-index.c: display usage on\n> unrecognized command, 2021-03-30) we have used a \"usage\" label to avoid\n> having two separate callers of usage_with_options (one when no arguments\n> are given, and another for unrecognized sub-commands).\n> \n> But the first caller has been broken since cd57bc41bb, since it will\n> happily jump to usage without arguments, and then pass argv[0] to the\n> \"unrecognized subcommand\" error.\n> \n> Many compilers will save us from a segfault here, but the end result is\n> ugly, since it mentions an unrecognized subcommand when we didn't even\n> pass one, and (on GCC) includes \"(null)\" in its output.\n> \n> Move the \"usage\" label down past the error about unrecognized\n> subcommands so that it is only triggered when it should be. While we're\n> at it, bulk up our test coverage in this area, too.\n\nGood find. The code change seems obviously correct.\n\n> +test_expect_success 'usage shown without sub-command' '\n> +\ttest_expect_code 129 git multi-pack-index 2>err &&\n> +\t! test_i18ngrep \"unrecognized subcommand\" err\n> +'\n\nI think we're avoiding test_i18ngrep in new code these days.\n\n-Peff\n"},{"id":"430774","messageId":"12804130427fd71e88ce10aed283c1f9cdd330ef.1626886330.git.me@ttaylorr.com","threadId":"56125","inReplyTo":"YPfWkzRtQKthOgZx@coredump.intra.peff.net","subject":"[PATCH v2] multi-pack-index: fix potential segfault without sub-command","fromName":"Taylor Blau","fromEmail":"me@ttaylorr.com","sentAt":"2021-07-21T16:52:37Z","receivedAt":"2021-07-21T16:52:40Z","isPatch":true,"sender":{"key":"me@ttaylorr.com","avatar":"https://avatars.githubusercontent.com/u/301000140?v=4"},"body":"Since cd57bc41bb (builtin/multi-pack-index.c: display usage on\nunrecognized command, 2021-03-30) we have used a \"usage\" label to avoid\nhaving two separate callers of usage_with_options (one when no arguments\nare given, and another for unrecognized sub-commands).\n\nBut the first caller has been broken since cd57bc41bb, since it will\nhappily jump to usage without arguments, and then pass argv[0] to the\n\"unrecognized subcommand\" error.\n\nMany compilers will save us from a segfault here, but the end result is\nugly, since it mentions an unrecognized subcommand when we didn't even\npass one, and (on GCC) includes \"(null)\" in its output.\n\nMove the \"usage\" label down past the error about unrecognized\nsubcommands so that it is only triggered when it should be. While we're\nat it, bulk up our test coverage in this area, too.\n\nSigned-off-by: Taylor Blau <me@ttaylorr.com>\n---\nI didn't realize that we are veering away from test_i18ngrep in new\ntests, so here's a version of this trivial patch with grep instead.\n\nRange-diff against v1:\n1:  8c0bb3e0dc ! 1:  1280413042 multi-pack-index: fix potential segfault without sub-command\n    @@ t/t5319-multi-pack-index.sh: test_expect_success 'load reverse index when missin\n\n     +test_expect_success 'usage shown without sub-command' '\n     +\ttest_expect_code 129 git multi-pack-index 2>err &&\n    -+\t! test_i18ngrep \"unrecognized subcommand\" err\n    ++\t! grep \"unrecognized subcommand\" err\n     +'\n     +\n      test_done\n\n builtin/multi-pack-index.c  | 2 +-\n t/t5319-multi-pack-index.sh | 5 +++++\n 2 files changed, 6 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c\nindex 5d3ea445fd..8ff0dee2ec 100644\n--- a/builtin/multi-pack-index.c\n+++ b/builtin/multi-pack-index.c\n@@ -176,8 +176,8 @@ int cmd_multi_pack_index(int argc, const char **argv,\n \telse if (!strcmp(argv[0], \"expire\"))\n \t\treturn cmd_multi_pack_index_expire(argc, argv);\n \telse {\n-usage:\n \t\terror(_(\"unrecognized subcommand: %s\"), argv[0]);\n+usage:\n \t\tusage_with_options(builtin_multi_pack_index_usage,\n \t\t\t\t   builtin_multi_pack_index_options);\n \t}\ndiff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh\nindex 7609f1ea64..c0ec5a6fd3 100755\n--- a/t/t5319-multi-pack-index.sh\n+++ b/t/t5319-multi-pack-index.sh\n@@ -837,4 +837,9 @@ test_expect_success 'load reverse index when missing .idx, .pack' '\n \t)\n '\n\n+test_expect_success 'usage shown without sub-command' '\n+\ttest_expect_code 129 git multi-pack-index 2>err &&\n+\t! grep \"unrecognized subcommand\" err\n+'\n+\n test_done\n--\n2.31.1.163.ga65ce7f831\n"},{"id":"430986","messageId":"YPpxA1XWw65pRr0P@coredump.intra.peff.net","threadId":"56125","inReplyTo":"12804130427fd71e88ce10aed283c1f9cdd330ef.1626886330.git.me@ttaylorr.com","subject":"Re: [PATCH v2] multi-pack-index: fix potential segfault without sub-command","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2021-07-23T07:34:27Z","receivedAt":"2021-07-23T07:34:30Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Jul 21, 2021 at 12:52:37PM -0400, Taylor Blau wrote:\n\n> I didn't realize that we are veering away from test_i18ngrep in new\n> tests, so here's a version of this trivial patch with grep instead.\n\nThis looks fine to me, but it looks like Junio may have already merged\nthe original into 'next' via 995cb54b5b (Merge branch 'tb/reverse-midx'\ninto next, 2021-07-20).\n\nSo we'd have to update it on top. I think we can just leave it be,\nthough. There's nothing incorrect about using test_i18ngrep for now, and\nthere are a ton of existing instances to handle if we choose to do a\nbulk cleanup. One more isn't a big deal.\n\n-Peff\n"}]}