{"thread":{"id":"57050","subject":"[PATCH 0/3] sparse-checkout: fix segfault on malformed patterns","startedAt":"2021-12-07T20:02:20Z","lastAt":"2021-12-16T19:16:41Z","messageCount":24,"participants":["Derrick Stolee via GitGitGadget","Elijah Newren","Derrick Stolee","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"443343","messageId":"pull.1069.git.1638907336.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":null,"subject":"[PATCH 0/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-07T20:02:13Z","receivedAt":"2021-12-07T20:02:20Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"This series fixes some issues with parsing sparse-checkout patterns when\ncore.sparseCheckoutCone is enabled but the sparse-checkout file itself\ncontains patterns that don't match the cone mode format.\n\nThe first patch fixes a segfault first reported in [1]. The other two\npatches are from an earlier submission [2] that never got picked up and I\nlost track of. There was another patch involving 'git sparse-checkout init\n--cone' that isn't necessary, especially with Elijah doing some work in that\nspace right now.\n\n[1] https://github.com/git-for-windows/git/issues/3498 [2]\nhttps://lore.kernel.org/git/pull.1043.git.1632160658.gitgitgadget@gmail.com\n\nThanks, -Stolee\n\nDerrick Stolee (3):\n  sparse-checkout: fix segfault on malformed patterns\n  sparse-checkout: fix OOM error with mixed patterns\n  sparse-checkout: refuse to add to bad patterns\n\n builtin/sparse-checkout.c          |  5 ++++-\n dir.c                              |  5 +----\n t/t1091-sparse-checkout-builtin.sh | 31 +++++++++++++++++++++++++++++-\n 3 files changed, 35 insertions(+), 6 deletions(-)\n\n\nbase-commit: abe6bb3905392d5eb6b01fa6e54d7e784e0522aa\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1069%2Fderrickstolee%2Fsparse-checkout%2Finput-bug-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1069/derrickstolee/sparse-checkout/input-bug-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/1069\n-- \ngitgitgadget\n"},{"id":"443344","messageId":"becbee16d2ee503a7f698364aef672d4cadf5079.1638907336.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.git.1638907336.gitgitgadget@gmail.com","subject":"[PATCH 1/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-07T20:02:14Z","receivedAt":"2021-12-07T20:02:23Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThen core.sparseCheckoutCone is enabled, the sparse-checkout patterns are\nused to populate two hashsets that accelerate pattern matching. If the user\nmodifies the sparse-checkout file outside of the 'sparse-checkout' builtin,\nthen strange patterns can happen, triggering some error checks.\n\nOne of these error checks is possible to hit when some special characters\nexist in a line. A warning message is correctly written to stderr, but then\nthere is additional logic that attempts to remove the line from the hashset\nand free the data. This leads to a segfault in the 'git sparse-checkout\nlist' command because it iterates over the contents of the hashset, which is\nno invalid.\n\nThe fix here is to stop trying to remove from the hashset. Better to leave\nbad data in the sparse-checkout matching logic (with a warning) than to\nsegfault. If we are in this state, then we are already traversing into\nundefined behavior, so this change to keep the entry in the hashset is no\nworse than removing it.\n\nAdd a test that triggers the segfault without the code change.\n\nReported-by: John Burnett <johnburnett@johnburnett.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n dir.c                              |  3 ---\n t/t1091-sparse-checkout-builtin.sh | 15 +++++++++++++++\n 2 files changed, 15 insertions(+), 3 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex 5aa6fbad0b7..0693c7cb3ee 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -819,9 +819,6 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern\n \t\t/* we already included this at the parent level */\n \t\twarning(_(\"your sparse-checkout file may have issues: pattern '%s' is repeated\"),\n \t\t\tgiven->pattern);\n-\t\thashmap_remove(&pl->parent_hashmap, &translated->ent, &data);\n-\t\tfree(data);\n-\t\tfree(translated);\n \t}\n \n \treturn;\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex 272ba1b566b..c72b8ee2e7b 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -708,4 +708,19 @@ test_expect_success 'cone mode clears ignored subdirectories' '\n \ttest_cmp expect out\n '\n \n+test_expect_success 'malformed cone-mode patterns' '\n+\tgit -C repo sparse-checkout init --cone &&\n+\tmkdir -p repo/foo/bar &&\n+\ttouch repo/foo/bar/x repo/foo/y &&\n+\tcat >repo/.git/info/sparse-checkout <<-\\EOF &&\n+\t/*\n+\t!/*/\n+\t/foo/\n+\t!/foo/*/\n+\t/foo/\\*/\n+\tEOF\n+\tcat repo/.git/info/sparse-checkout &&\n+\tgit -C repo sparse-checkout list\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"443345","messageId":"239bf23eacb7dbf7b0659b483709dc4579d43b1a.1638907336.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.git.1638907336.gitgitgadget@gmail.com","subject":"[PATCH 2/3] sparse-checkout: fix OOM error with mixed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-07T20:02:15Z","receivedAt":"2021-12-07T20:02:26Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nAdd a test to t1091-sparse-checkout-builtin.sh that would result in an\ninfinite loop and out-of-memory error before this change. The issue\nrelies on having non-cone-mode patterns while trying to modify the\npatterns in cone-mode.\n\nThe fix is simple, allowing us to break from the loop when the input\npath does not contain a slash, as the \"dir\" pattern we added does not.\n\nThis is only a fix to the critical out-of-memory error. A better\nresponse to such a strange state will follow in a later change.\n\nReported-by: Calbabreaker <calbabreaker@gmail.com>\nHelped-by: Taylor Blau <me@ttaylorr.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/sparse-checkout.c          |  2 +-\n t/t1091-sparse-checkout-builtin.sh | 11 +++++++++++\n 2 files changed, 12 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c\nindex d0f5c4702be..9ccdcde9832 100644\n--- a/builtin/sparse-checkout.c\n+++ b/builtin/sparse-checkout.c\n@@ -483,7 +483,7 @@ static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *pat\n \t\tchar *oldpattern = e->pattern;\n \t\tsize_t newlen;\n \n-\t\tif (slash == e->pattern)\n+\t\tif (!slash || slash == e->pattern)\n \t\t\tbreak;\n \n \t\tnewlen = slash - e->pattern;\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex c72b8ee2e7b..67ce24c9c83 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -103,6 +103,17 @@ test_expect_success 'clone --sparse' '\n \tcheck_files clone a\n '\n \n+test_expect_success 'switching to cone mode with non-cone mode patterns' '\n+\tgit init bad-patterns &&\n+\t(\n+\t\tcd bad-patterns &&\n+\t\tgit sparse-checkout init &&\n+\t\tgit sparse-checkout add dir &&\n+\t\tgit config core.sparseCheckoutCone true &&\n+\t\tgit sparse-checkout add dir\n+\t)\n+'\n+\n test_expect_success 'interaction with clone --no-checkout (unborn index)' '\n \tgit clone --no-checkout \"file://$(pwd)/repo\" clone_no_checkout &&\n \tgit -C clone_no_checkout sparse-checkout init --cone &&\n-- \ngitgitgadget\n\n"},{"id":"443346","messageId":"cc52fb2b0b76ccfd8f17f0e359b440433569dd80.1638907336.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.git.1638907336.gitgitgadget@gmail.com","subject":"[PATCH 3/3] sparse-checkout: refuse to add to bad patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-07T20:02:16Z","receivedAt":"2021-12-07T20:02:27Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen in cone mode sparse-checkout, it is unclear how 'git\nsparse-checkout add <dir1> ...' should behave if the existing\nsparse-checkout file does not match the cone mode patterns. Change the\nbehavior to fail with an error message about the existing patterns.\n\nAlso, all cone mode patterns start with a '/' character, so add that\nrestriction. This is necessary for our example test 'cone mode: warn on\nbad pattern', but also requires modifying the example sparse-checkout\nfile we use to test the warnings related to recognizing cone mode\npatterns.\n\nThis error checking would cause a failure further down the test script\nbecause of a test that adds non-cone mode patterns without cleaning them\nup. Perform that cleanup as part of the test now.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/sparse-checkout.c          | 3 +++\n dir.c                              | 2 +-\n t/t1091-sparse-checkout-builtin.sh | 7 +++++--\n 3 files changed, 9 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c\nindex 9ccdcde9832..6580075a631 100644\n--- a/builtin/sparse-checkout.c\n+++ b/builtin/sparse-checkout.c\n@@ -598,6 +598,9 @@ static void add_patterns_cone_mode(int argc, const char **argv,\n \t\tdie(_(\"unable to load existing sparse-checkout patterns\"));\n \tfree(sparse_filename);\n \n+\tif (!existing.use_cone_patterns)\n+\t\tdie(_(\"existing sparse-checkout patterns do not use cone mode\"));\n+\n \thashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {\n \t\tif (!hashmap_contains_parent(&pl->recursive_hashmap,\n \t\t\t\t\tpe->pattern, &buffer) ||\ndiff --git a/dir.c b/dir.c\nindex 0693c7cb3ee..a5dddafa16d 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -727,7 +727,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern\n \t}\n \n \tif (given->patternlen < 2 ||\n-\t    *given->pattern == '*' ||\n+\t    *given->pattern != '/' ||\n \t    strstr(given->pattern, \"**\")) {\n \t\t/* Not a cone pattern. */\n \t\twarning(_(\"unrecognized pattern: '%s'\"), given->pattern);\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex 67ce24c9c83..2ed247d75a5 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -110,7 +110,8 @@ test_expect_success 'switching to cone mode with non-cone mode patterns' '\n \t\tgit sparse-checkout init &&\n \t\tgit sparse-checkout add dir &&\n \t\tgit config core.sparseCheckoutCone true &&\n-\t\tgit sparse-checkout add dir\n+\t\ttest_must_fail git sparse-checkout add dir 2>err &&\n+\t\tgrep \"existing sparse-checkout patterns do not use cone mode\" err\n \t)\n '\n \n@@ -176,12 +177,14 @@ test_expect_success 'set sparse-checkout using --stdin' '\n '\n \n test_expect_success 'add to sparse-checkout' '\n-\tcat repo/.git/info/sparse-checkout >expect &&\n+\tcat repo/.git/info/sparse-checkout >old &&\n+\ttest_when_finished cp old repo/.git/info/sparse-checkout &&\n \tcat >add <<-\\EOF &&\n \tpattern1\n \t/folder1/\n \tpattern2\n \tEOF\n+\tcat old >expect &&\n \tcat add >>expect &&\n \tgit -C repo sparse-checkout add --stdin <add &&\n \tgit -C repo sparse-checkout list >actual &&\n-- \ngitgitgadget\n"},{"id":"443359","messageId":"CABPp-BF0VkL0VQYWu1SObVGVQA7TkHXT67vHQVQpkvmc3xMa8g@mail.gmail.com","threadId":"57050","inReplyTo":"becbee16d2ee503a7f698364aef672d4cadf5079.1638907336.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 1/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2021-12-07T20:22:44Z","receivedAt":"2021-12-07T20:23:18Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Tue, Dec 7, 2021 at 12:02 PM Derrick Stolee via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> From: Derrick Stolee <dstolee@microsoft.com>\n>\n> Then core.sparseCheckoutCone is enabled, the sparse-checkout patterns are\n> used to populate two hashsets that accelerate pattern matching. If the user\n> modifies the sparse-checkout file outside of the 'sparse-checkout' builtin,\n> then strange patterns can happen, triggering some error checks.\n>\n> One of these error checks is possible to hit when some special characters\n> exist in a line. A warning message is correctly written to stderr, but then\n> there is additional logic that attempts to remove the line from the hashset\n> and free the data. This leads to a segfault in the 'git sparse-checkout\n> list' command because it iterates over the contents of the hashset, which is\n> no invalid.\n\ns/no invalid/now invalid/ ?\n\n>\n> The fix here is to stop trying to remove from the hashset. Better to leave\n> bad data in the sparse-checkout matching logic (with a warning) than to\n> segfault. If we are in this state, then we are already traversing into\n> undefined behavior, so this change to keep the entry in the hashset is no\n> worse than removing it.\n>\n> Add a test that triggers the segfault without the code change.\n>\n> Reported-by: John Burnett <johnburnett@johnburnett.com>\n> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>\n> ---\n>  dir.c                              |  3 ---\n>  t/t1091-sparse-checkout-builtin.sh | 15 +++++++++++++++\n>  2 files changed, 15 insertions(+), 3 deletions(-)\n>\n> diff --git a/dir.c b/dir.c\n> index 5aa6fbad0b7..0693c7cb3ee 100644\n> --- a/dir.c\n> +++ b/dir.c\n> @@ -819,9 +819,6 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern\n>                 /* we already included this at the parent level */\n>                 warning(_(\"your sparse-checkout file may have issues: pattern '%s' is repeated\"),\n>                         given->pattern);\n> -               hashmap_remove(&pl->parent_hashmap, &translated->ent, &data);\n> -               free(data);\n> -               free(translated);\n>         }\n>\n>         return;\n> diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\n> index 272ba1b566b..c72b8ee2e7b 100755\n> --- a/t/t1091-sparse-checkout-builtin.sh\n> +++ b/t/t1091-sparse-checkout-builtin.sh\n> @@ -708,4 +708,19 @@ test_expect_success 'cone mode clears ignored subdirectories' '\n>         test_cmp expect out\n>  '\n>\n> +test_expect_success 'malformed cone-mode patterns' '\n> +       git -C repo sparse-checkout init --cone &&\n> +       mkdir -p repo/foo/bar &&\n> +       touch repo/foo/bar/x repo/foo/y &&\n> +       cat >repo/.git/info/sparse-checkout <<-\\EOF &&\n> +       /*\n> +       !/*/\n> +       /foo/\n> +       !/foo/*/\n> +       /foo/\\*/\n> +       EOF\n> +       cat repo/.git/info/sparse-checkout &&\n> +       git -C repo sparse-checkout list\n> +'\n> +\n>  test_done\n> --\n> gitgitgadget\n>\n"},{"id":"443377","messageId":"CABPp-BF-xvJzfWO33p1ODtkLGMy2KENAbEu7KnrxPNq7UubbFg@mail.gmail.com","threadId":"57050","inReplyTo":"pull.1069.git.1638907336.gitgitgadget@gmail.com","subject":"Re: [PATCH 0/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2021-12-07T21:51:24Z","receivedAt":"2021-12-07T21:51:39Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Tue, Dec 7, 2021 at 12:02 PM Derrick Stolee via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> This series fixes some issues with parsing sparse-checkout patterns when\n> core.sparseCheckoutCone is enabled but the sparse-checkout file itself\n> contains patterns that don't match the cone mode format.\n\nI was only able to find what I think is a small typo in one of the\ncommits.  Everything else looks good to me.\n\n> The first patch fixes a segfault first reported in [1]. The other two\n> patches are from an earlier submission [2] that never got picked up and I\n> lost track of. There was another patch involving 'git sparse-checkout init\n\nSorry for missing that series earlier.  Glad we've got some of them now.\n\n> --cone' that isn't necessary, especially with Elijah doing some work in that\n> space right now.\n>\n> [1] https://github.com/git-for-windows/git/issues/3498 [2]\n> https://lore.kernel.org/git/pull.1043.git.1632160658.gitgitgadget@gmail.com\n>\n> Thanks, -Stolee\n>\n> Derrick Stolee (3):\n>   sparse-checkout: fix segfault on malformed patterns\n>   sparse-checkout: fix OOM error with mixed patterns\n>   sparse-checkout: refuse to add to bad patterns\n>\n>  builtin/sparse-checkout.c          |  5 ++++-\n>  dir.c                              |  5 +----\n>  t/t1091-sparse-checkout-builtin.sh | 31 +++++++++++++++++++++++++++++-\n>  3 files changed, 35 insertions(+), 6 deletions(-)\n>\n>\n> base-commit: abe6bb3905392d5eb6b01fa6e54d7e784e0522aa\n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1069%2Fderrickstolee%2Fsparse-checkout%2Finput-bug-v1\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1069/derrickstolee/sparse-checkout/input-bug-v1\n> Pull-Request: https://github.com/gitgitgadget/git/pull/1069\n> --\n> gitgitgadget\n"},{"id":"443446","messageId":"63bae98d-5c81-a6e4-5364-a643dbe4ef5e@gmail.com","threadId":"57050","inReplyTo":"CABPp-BF-xvJzfWO33p1ODtkLGMy2KENAbEu7KnrxPNq7UubbFg@mail.gmail.com","subject":"Re: [PATCH 0/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2021-12-08T14:23:24Z","receivedAt":"2021-12-08T14:23:28Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 12/7/2021 4:51 PM, Elijah Newren wrote:\n> On Tue, Dec 7, 2021 at 12:02 PM Derrick Stolee via GitGitGadget\n> <gitgitgadget@gmail.com> wrote:\n>>\n>> This series fixes some issues with parsing sparse-checkout patterns when\n>> core.sparseCheckoutCone is enabled but the sparse-checkout file itself\n>> contains patterns that don't match the cone mode format.\n> \n> I was only able to find what I think is a small typo in one of the\n> commits.  Everything else looks good to me.\n\nThanks. I'll give this a few more days for more feedback before sending\na v2 with that fix.\n \n>> The first patch fixes a segfault first reported in [1]. The other two\n>> patches are from an earlier submission [2] that never got picked up and I\n>> lost track of. There was another patch involving 'git sparse-checkout init\n> \n> Sorry for missing that series earlier.  Glad we've got some of them now.\n\nThe fault was my own for dropping this during a particularly busy time.\n\nThanks,\n-Stolee\n"},{"id":"443814","messageId":"pull.1069.v2.git.1639149490.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.git.1638907336.gitgitgadget@gmail.com","subject":"[PATCH v2 0/4] sparse-checkout: fix segfault on malformed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-10T15:18:07Z","receivedAt":"2021-12-10T15:18:18Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"This series fixes some issues with parsing sparse-checkout patterns when\ncore.sparseCheckoutCone is enabled but the sparse-checkout file itself\ncontains patterns that don't match the cone mode format.\n\nThe first patch fixes a segfault first reported in [1]. The other two\npatches are from an earlier submission [2] that never got picked up and I\nlost track of. There was another patch involving 'git sparse-checkout init\n--cone' that isn't necessary, especially with Elijah doing some work in that\nspace right now.\n\n[1] https://github.com/git-for-windows/git/issues/3498 [2]\nhttps://lore.kernel.org/git/pull.1043.git.1632160658.gitgitgadget@gmail.com\n\nThanks, -Stolee\n\nDerrick Stolee (4):\n  sparse-checkout: fix segfault on malformed patterns\n  sparse-checkout: fix OOM error with mixed patterns\n  sparse-checkout: refuse to add to bad patterns\n  amend! sparse-checkout: fix segfault on malformed patterns\n\n builtin/sparse-checkout.c          |  5 ++++-\n dir.c                              |  5 +----\n t/t1091-sparse-checkout-builtin.sh | 31 +++++++++++++++++++++++++++++-\n 3 files changed, 35 insertions(+), 6 deletions(-)\n\n\nbase-commit: abe6bb3905392d5eb6b01fa6e54d7e784e0522aa\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1069%2Fderrickstolee%2Fsparse-checkout%2Finput-bug-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1069/derrickstolee/sparse-checkout/input-bug-v2\nPull-Request: https://github.com/gitgitgadget/git/pull/1069\n\nRange-diff vs v1:\n\n 1:  becbee16d2e ! 1:  a0e3dd335c9 sparse-checkout: fix segfault on malformed patterns\n     @@ Commit message\n          Add a test that triggers the segfault without the code change.\n      \n          Reported-by: John Burnett <johnburnett@johnburnett.com>\n     +    Reviewed-by: Elijah Newren <newren@gmail.com>\n          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>\n      \n       ## dir.c ##\n 2:  239bf23eacb ! 2:  86fbf130c03 sparse-checkout: fix OOM error with mixed patterns\n     @@ Commit message\n      \n          Reported-by: Calbabreaker <calbabreaker@gmail.com>\n          Helped-by: Taylor Blau <me@ttaylorr.com>\n     +    Reviewed-by: Elijah Newren <newren@gmail.com>\n          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>\n      \n       ## builtin/sparse-checkout.c ##\n 3:  cc52fb2b0b7 ! 3:  5d096e380a4 sparse-checkout: refuse to add to bad patterns\n     @@ Commit message\n          because of a test that adds non-cone mode patterns without cleaning them\n          up. Perform that cleanup as part of the test now.\n      \n     +    Reviewed-by: Elijah Newren <newren@gmail.com>\n          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>\n      \n       ## builtin/sparse-checkout.c ##\n -:  ----------- > 4:  7bacb3760f3 amend! sparse-checkout: fix segfault on malformed patterns\n\n-- \ngitgitgadget\n"},{"id":"443815","messageId":"86fbf130c039b2c087974b213e12919f7809e05b.1639149490.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.v2.git.1639149490.gitgitgadget@gmail.com","subject":"[PATCH v2 2/4] sparse-checkout: fix OOM error with mixed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-10T15:18:09Z","receivedAt":"2021-12-10T15:18:21Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nAdd a test to t1091-sparse-checkout-builtin.sh that would result in an\ninfinite loop and out-of-memory error before this change. The issue\nrelies on having non-cone-mode patterns while trying to modify the\npatterns in cone-mode.\n\nThe fix is simple, allowing us to break from the loop when the input\npath does not contain a slash, as the \"dir\" pattern we added does not.\n\nThis is only a fix to the critical out-of-memory error. A better\nresponse to such a strange state will follow in a later change.\n\nReported-by: Calbabreaker <calbabreaker@gmail.com>\nHelped-by: Taylor Blau <me@ttaylorr.com>\nReviewed-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/sparse-checkout.c          |  2 +-\n t/t1091-sparse-checkout-builtin.sh | 11 +++++++++++\n 2 files changed, 12 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c\nindex d0f5c4702be..9ccdcde9832 100644\n--- a/builtin/sparse-checkout.c\n+++ b/builtin/sparse-checkout.c\n@@ -483,7 +483,7 @@ static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *pat\n \t\tchar *oldpattern = e->pattern;\n \t\tsize_t newlen;\n \n-\t\tif (slash == e->pattern)\n+\t\tif (!slash || slash == e->pattern)\n \t\t\tbreak;\n \n \t\tnewlen = slash - e->pattern;\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex c72b8ee2e7b..67ce24c9c83 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -103,6 +103,17 @@ test_expect_success 'clone --sparse' '\n \tcheck_files clone a\n '\n \n+test_expect_success 'switching to cone mode with non-cone mode patterns' '\n+\tgit init bad-patterns &&\n+\t(\n+\t\tcd bad-patterns &&\n+\t\tgit sparse-checkout init &&\n+\t\tgit sparse-checkout add dir &&\n+\t\tgit config core.sparseCheckoutCone true &&\n+\t\tgit sparse-checkout add dir\n+\t)\n+'\n+\n test_expect_success 'interaction with clone --no-checkout (unborn index)' '\n \tgit clone --no-checkout \"file://$(pwd)/repo\" clone_no_checkout &&\n \tgit -C clone_no_checkout sparse-checkout init --cone &&\n-- \ngitgitgadget\n\n"},{"id":"443816","messageId":"a0e3dd335c98bb625fce6bbf9a7a7491e9be7760.1639149490.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.v2.git.1639149490.gitgitgadget@gmail.com","subject":"[PATCH v2 1/4] sparse-checkout: fix segfault on malformed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-10T15:18:08Z","receivedAt":"2021-12-10T15:18:22Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThen core.sparseCheckoutCone is enabled, the sparse-checkout patterns are\nused to populate two hashsets that accelerate pattern matching. If the user\nmodifies the sparse-checkout file outside of the 'sparse-checkout' builtin,\nthen strange patterns can happen, triggering some error checks.\n\nOne of these error checks is possible to hit when some special characters\nexist in a line. A warning message is correctly written to stderr, but then\nthere is additional logic that attempts to remove the line from the hashset\nand free the data. This leads to a segfault in the 'git sparse-checkout\nlist' command because it iterates over the contents of the hashset, which is\nno invalid.\n\nThe fix here is to stop trying to remove from the hashset. Better to leave\nbad data in the sparse-checkout matching logic (with a warning) than to\nsegfault. If we are in this state, then we are already traversing into\nundefined behavior, so this change to keep the entry in the hashset is no\nworse than removing it.\n\nAdd a test that triggers the segfault without the code change.\n\nReported-by: John Burnett <johnburnett@johnburnett.com>\nReviewed-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n dir.c                              |  3 ---\n t/t1091-sparse-checkout-builtin.sh | 15 +++++++++++++++\n 2 files changed, 15 insertions(+), 3 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex 5aa6fbad0b7..0693c7cb3ee 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -819,9 +819,6 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern\n \t\t/* we already included this at the parent level */\n \t\twarning(_(\"your sparse-checkout file may have issues: pattern '%s' is repeated\"),\n \t\t\tgiven->pattern);\n-\t\thashmap_remove(&pl->parent_hashmap, &translated->ent, &data);\n-\t\tfree(data);\n-\t\tfree(translated);\n \t}\n \n \treturn;\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex 272ba1b566b..c72b8ee2e7b 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -708,4 +708,19 @@ test_expect_success 'cone mode clears ignored subdirectories' '\n \ttest_cmp expect out\n '\n \n+test_expect_success 'malformed cone-mode patterns' '\n+\tgit -C repo sparse-checkout init --cone &&\n+\tmkdir -p repo/foo/bar &&\n+\ttouch repo/foo/bar/x repo/foo/y &&\n+\tcat >repo/.git/info/sparse-checkout <<-\\EOF &&\n+\t/*\n+\t!/*/\n+\t/foo/\n+\t!/foo/*/\n+\t/foo/\\*/\n+\tEOF\n+\tcat repo/.git/info/sparse-checkout &&\n+\tgit -C repo sparse-checkout list\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"443817","messageId":"5d096e380a400759f3915568228fa703232cdd18.1639149490.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.v2.git.1639149490.gitgitgadget@gmail.com","subject":"[PATCH v2 3/4] sparse-checkout: refuse to add to bad patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-10T15:18:10Z","receivedAt":"2021-12-10T15:18:23Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen in cone mode sparse-checkout, it is unclear how 'git\nsparse-checkout add <dir1> ...' should behave if the existing\nsparse-checkout file does not match the cone mode patterns. Change the\nbehavior to fail with an error message about the existing patterns.\n\nAlso, all cone mode patterns start with a '/' character, so add that\nrestriction. This is necessary for our example test 'cone mode: warn on\nbad pattern', but also requires modifying the example sparse-checkout\nfile we use to test the warnings related to recognizing cone mode\npatterns.\n\nThis error checking would cause a failure further down the test script\nbecause of a test that adds non-cone mode patterns without cleaning them\nup. Perform that cleanup as part of the test now.\n\nReviewed-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/sparse-checkout.c          | 3 +++\n dir.c                              | 2 +-\n t/t1091-sparse-checkout-builtin.sh | 7 +++++--\n 3 files changed, 9 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c\nindex 9ccdcde9832..6580075a631 100644\n--- a/builtin/sparse-checkout.c\n+++ b/builtin/sparse-checkout.c\n@@ -598,6 +598,9 @@ static void add_patterns_cone_mode(int argc, const char **argv,\n \t\tdie(_(\"unable to load existing sparse-checkout patterns\"));\n \tfree(sparse_filename);\n \n+\tif (!existing.use_cone_patterns)\n+\t\tdie(_(\"existing sparse-checkout patterns do not use cone mode\"));\n+\n \thashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {\n \t\tif (!hashmap_contains_parent(&pl->recursive_hashmap,\n \t\t\t\t\tpe->pattern, &buffer) ||\ndiff --git a/dir.c b/dir.c\nindex 0693c7cb3ee..a5dddafa16d 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -727,7 +727,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern\n \t}\n \n \tif (given->patternlen < 2 ||\n-\t    *given->pattern == '*' ||\n+\t    *given->pattern != '/' ||\n \t    strstr(given->pattern, \"**\")) {\n \t\t/* Not a cone pattern. */\n \t\twarning(_(\"unrecognized pattern: '%s'\"), given->pattern);\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex 67ce24c9c83..2ed247d75a5 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -110,7 +110,8 @@ test_expect_success 'switching to cone mode with non-cone mode patterns' '\n \t\tgit sparse-checkout init &&\n \t\tgit sparse-checkout add dir &&\n \t\tgit config core.sparseCheckoutCone true &&\n-\t\tgit sparse-checkout add dir\n+\t\ttest_must_fail git sparse-checkout add dir 2>err &&\n+\t\tgrep \"existing sparse-checkout patterns do not use cone mode\" err\n \t)\n '\n \n@@ -176,12 +177,14 @@ test_expect_success 'set sparse-checkout using --stdin' '\n '\n \n test_expect_success 'add to sparse-checkout' '\n-\tcat repo/.git/info/sparse-checkout >expect &&\n+\tcat repo/.git/info/sparse-checkout >old &&\n+\ttest_when_finished cp old repo/.git/info/sparse-checkout &&\n \tcat >add <<-\\EOF &&\n \tpattern1\n \t/folder1/\n \tpattern2\n \tEOF\n+\tcat old >expect &&\n \tcat add >>expect &&\n \tgit -C repo sparse-checkout add --stdin <add &&\n \tgit -C repo sparse-checkout list >actual &&\n-- \ngitgitgadget\n"},{"id":"444144","messageId":"pull.1069.v3.git.1639575968.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.v2.git.1639149490.gitgitgadget@gmail.com","subject":"[PATCH v3 0/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-15T13:46:05Z","receivedAt":"2021-12-15T13:46:12Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"This series fixes some issues with parsing sparse-checkout patterns when\ncore.sparseCheckoutCone is enabled but the sparse-checkout file itself\ncontains patterns that don't match the cone mode format.\n\nThe first patch fixes a segfault first reported in [1]. The other two\npatches are from an earlier submission [2] that never got picked up and I\nlost track of. There was another patch involving 'git sparse-checkout init\n--cone' that isn't necessary, especially with Elijah doing some work in that\nspace right now.\n\n[1] https://github.com/git-for-windows/git/issues/3498 [2]\nhttps://lore.kernel.org/git/pull.1043.git.1632160658.gitgitgadget@gmail.com\n\nThanks, -Stolee\n\n\nUpdates in v2 and v3\n====================\n\n * I intended to fix a typo in a patch, but accidentally sent the amend!\n   commit in v2\n * v3 has the typo fix properly squashed in.\n * Added Elijah's review.\n\nDerrick Stolee (3):\n  sparse-checkout: fix segfault on malformed patterns\n  sparse-checkout: fix OOM error with mixed patterns\n  sparse-checkout: refuse to add to bad patterns\n\n builtin/sparse-checkout.c          |  5 ++++-\n dir.c                              |  5 +----\n t/t1091-sparse-checkout-builtin.sh | 31 +++++++++++++++++++++++++++++-\n 3 files changed, 35 insertions(+), 6 deletions(-)\n\n\nbase-commit: abe6bb3905392d5eb6b01fa6e54d7e784e0522aa\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1069%2Fderrickstolee%2Fsparse-checkout%2Finput-bug-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1069/derrickstolee/sparse-checkout/input-bug-v3\nPull-Request: https://github.com/gitgitgadget/git/pull/1069\n\nRange-diff vs v2:\n\n 1:  a0e3dd335c9 ! 1:  1744a26845f sparse-checkout: fix segfault on malformed patterns\n     @@ Commit message\n          there is additional logic that attempts to remove the line from the hashset\n          and free the data. This leads to a segfault in the 'git sparse-checkout\n          list' command because it iterates over the contents of the hashset, which is\n     -    no invalid.\n     +    now invalid.\n      \n          The fix here is to stop trying to remove from the hashset. Better to leave\n          bad data in the sparse-checkout matching logic (with a warning) than to\n 2:  86fbf130c03 = 2:  a2fe867222e sparse-checkout: fix OOM error with mixed patterns\n 3:  5d096e380a4 = 3:  a0e5a942ae0 sparse-checkout: refuse to add to bad patterns\n 4:  7bacb3760f3 < -:  ----------- amend! sparse-checkout: fix segfault on malformed patterns\n\n-- \ngitgitgadget\n"},{"id":"444145","messageId":"1744a26845fbe4d7dbc80f387be1d842b5f8fe94.1639575968.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.v3.git.1639575968.gitgitgadget@gmail.com","subject":"[PATCH v3 1/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-15T13:46:06Z","receivedAt":"2021-12-15T13:46:13Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThen core.sparseCheckoutCone is enabled, the sparse-checkout patterns are\nused to populate two hashsets that accelerate pattern matching. If the user\nmodifies the sparse-checkout file outside of the 'sparse-checkout' builtin,\nthen strange patterns can happen, triggering some error checks.\n\nOne of these error checks is possible to hit when some special characters\nexist in a line. A warning message is correctly written to stderr, but then\nthere is additional logic that attempts to remove the line from the hashset\nand free the data. This leads to a segfault in the 'git sparse-checkout\nlist' command because it iterates over the contents of the hashset, which is\nnow invalid.\n\nThe fix here is to stop trying to remove from the hashset. Better to leave\nbad data in the sparse-checkout matching logic (with a warning) than to\nsegfault. If we are in this state, then we are already traversing into\nundefined behavior, so this change to keep the entry in the hashset is no\nworse than removing it.\n\nAdd a test that triggers the segfault without the code change.\n\nReported-by: John Burnett <johnburnett@johnburnett.com>\nReviewed-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n dir.c                              |  3 ---\n t/t1091-sparse-checkout-builtin.sh | 15 +++++++++++++++\n 2 files changed, 15 insertions(+), 3 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex 5aa6fbad0b7..0693c7cb3ee 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -819,9 +819,6 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern\n \t\t/* we already included this at the parent level */\n \t\twarning(_(\"your sparse-checkout file may have issues: pattern '%s' is repeated\"),\n \t\t\tgiven->pattern);\n-\t\thashmap_remove(&pl->parent_hashmap, &translated->ent, &data);\n-\t\tfree(data);\n-\t\tfree(translated);\n \t}\n \n \treturn;\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex 272ba1b566b..c72b8ee2e7b 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -708,4 +708,19 @@ test_expect_success 'cone mode clears ignored subdirectories' '\n \ttest_cmp expect out\n '\n \n+test_expect_success 'malformed cone-mode patterns' '\n+\tgit -C repo sparse-checkout init --cone &&\n+\tmkdir -p repo/foo/bar &&\n+\ttouch repo/foo/bar/x repo/foo/y &&\n+\tcat >repo/.git/info/sparse-checkout <<-\\EOF &&\n+\t/*\n+\t!/*/\n+\t/foo/\n+\t!/foo/*/\n+\t/foo/\\*/\n+\tEOF\n+\tcat repo/.git/info/sparse-checkout &&\n+\tgit -C repo sparse-checkout list\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"444146","messageId":"a2fe867222ef32656acdd45f02e3157194154e57.1639575968.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.v3.git.1639575968.gitgitgadget@gmail.com","subject":"[PATCH v3 2/3] sparse-checkout: fix OOM error with mixed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-15T13:46:07Z","receivedAt":"2021-12-15T13:46:14Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nAdd a test to t1091-sparse-checkout-builtin.sh that would result in an\ninfinite loop and out-of-memory error before this change. The issue\nrelies on having non-cone-mode patterns while trying to modify the\npatterns in cone-mode.\n\nThe fix is simple, allowing us to break from the loop when the input\npath does not contain a slash, as the \"dir\" pattern we added does not.\n\nThis is only a fix to the critical out-of-memory error. A better\nresponse to such a strange state will follow in a later change.\n\nReported-by: Calbabreaker <calbabreaker@gmail.com>\nHelped-by: Taylor Blau <me@ttaylorr.com>\nReviewed-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/sparse-checkout.c          |  2 +-\n t/t1091-sparse-checkout-builtin.sh | 11 +++++++++++\n 2 files changed, 12 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c\nindex d0f5c4702be..9ccdcde9832 100644\n--- a/builtin/sparse-checkout.c\n+++ b/builtin/sparse-checkout.c\n@@ -483,7 +483,7 @@ static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *pat\n \t\tchar *oldpattern = e->pattern;\n \t\tsize_t newlen;\n \n-\t\tif (slash == e->pattern)\n+\t\tif (!slash || slash == e->pattern)\n \t\t\tbreak;\n \n \t\tnewlen = slash - e->pattern;\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex c72b8ee2e7b..67ce24c9c83 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -103,6 +103,17 @@ test_expect_success 'clone --sparse' '\n \tcheck_files clone a\n '\n \n+test_expect_success 'switching to cone mode with non-cone mode patterns' '\n+\tgit init bad-patterns &&\n+\t(\n+\t\tcd bad-patterns &&\n+\t\tgit sparse-checkout init &&\n+\t\tgit sparse-checkout add dir &&\n+\t\tgit config core.sparseCheckoutCone true &&\n+\t\tgit sparse-checkout add dir\n+\t)\n+'\n+\n test_expect_success 'interaction with clone --no-checkout (unborn index)' '\n \tgit clone --no-checkout \"file://$(pwd)/repo\" clone_no_checkout &&\n \tgit -C clone_no_checkout sparse-checkout init --cone &&\n-- \ngitgitgadget\n\n"},{"id":"444147","messageId":"a0e5a942ae0597b4662bfb67dfb6dffab7113f52.1639575968.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.v3.git.1639575968.gitgitgadget@gmail.com","subject":"[PATCH v3 3/3] sparse-checkout: refuse to add to bad patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-15T13:46:08Z","receivedAt":"2021-12-15T13:46:17Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen in cone mode sparse-checkout, it is unclear how 'git\nsparse-checkout add <dir1> ...' should behave if the existing\nsparse-checkout file does not match the cone mode patterns. Change the\nbehavior to fail with an error message about the existing patterns.\n\nAlso, all cone mode patterns start with a '/' character, so add that\nrestriction. This is necessary for our example test 'cone mode: warn on\nbad pattern', but also requires modifying the example sparse-checkout\nfile we use to test the warnings related to recognizing cone mode\npatterns.\n\nThis error checking would cause a failure further down the test script\nbecause of a test that adds non-cone mode patterns without cleaning them\nup. Perform that cleanup as part of the test now.\n\nReviewed-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/sparse-checkout.c          | 3 +++\n dir.c                              | 2 +-\n t/t1091-sparse-checkout-builtin.sh | 7 +++++--\n 3 files changed, 9 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c\nindex 9ccdcde9832..6580075a631 100644\n--- a/builtin/sparse-checkout.c\n+++ b/builtin/sparse-checkout.c\n@@ -598,6 +598,9 @@ static void add_patterns_cone_mode(int argc, const char **argv,\n \t\tdie(_(\"unable to load existing sparse-checkout patterns\"));\n \tfree(sparse_filename);\n \n+\tif (!existing.use_cone_patterns)\n+\t\tdie(_(\"existing sparse-checkout patterns do not use cone mode\"));\n+\n \thashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {\n \t\tif (!hashmap_contains_parent(&pl->recursive_hashmap,\n \t\t\t\t\tpe->pattern, &buffer) ||\ndiff --git a/dir.c b/dir.c\nindex 0693c7cb3ee..a5dddafa16d 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -727,7 +727,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern\n \t}\n \n \tif (given->patternlen < 2 ||\n-\t    *given->pattern == '*' ||\n+\t    *given->pattern != '/' ||\n \t    strstr(given->pattern, \"**\")) {\n \t\t/* Not a cone pattern. */\n \t\twarning(_(\"unrecognized pattern: '%s'\"), given->pattern);\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex 67ce24c9c83..2ed247d75a5 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -110,7 +110,8 @@ test_expect_success 'switching to cone mode with non-cone mode patterns' '\n \t\tgit sparse-checkout init &&\n \t\tgit sparse-checkout add dir &&\n \t\tgit config core.sparseCheckoutCone true &&\n-\t\tgit sparse-checkout add dir\n+\t\ttest_must_fail git sparse-checkout add dir 2>err &&\n+\t\tgrep \"existing sparse-checkout patterns do not use cone mode\" err\n \t)\n '\n \n@@ -176,12 +177,14 @@ test_expect_success 'set sparse-checkout using --stdin' '\n '\n \n test_expect_success 'add to sparse-checkout' '\n-\tcat repo/.git/info/sparse-checkout >expect &&\n+\tcat repo/.git/info/sparse-checkout >old &&\n+\ttest_when_finished cp old repo/.git/info/sparse-checkout &&\n \tcat >add <<-\\EOF &&\n \tpattern1\n \t/folder1/\n \tpattern2\n \tEOF\n+\tcat old >expect &&\n \tcat add >>expect &&\n \tgit -C repo sparse-checkout add --stdin <add &&\n \tgit -C repo sparse-checkout list >actual &&\n-- \ngitgitgadget\n"},{"id":"444168","messageId":"xmqq5yrp61lw.fsf@gitster.g","threadId":"57050","inReplyTo":"pull.1069.v3.git.1639575968.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 0/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-12-15T20:43:07Z","receivedAt":"2021-12-15T20:43:13Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Derrick Stolee via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> This series fixes some issues with parsing sparse-checkout patterns when\n> core.sparseCheckoutCone is enabled but the sparse-checkout file itself\n> contains patterns that don't match the cone mode format.\n>\n> The first patch fixes a segfault first reported in [1]. The other two\n> patches are from an earlier submission [2] that never got picked up and I\n> lost track of. There was another patch involving 'git sparse-checkout init\n> --cone' that isn't necessary, especially with Elijah doing some work in that\n> space right now.\n\nThanks.  The segfault fix matters even more with Elijah's \"we can\nflip between modes\" feature, right?\n\n\n> [1] https://github.com/git-for-windows/git/issues/3498 [2]\n> https://lore.kernel.org/git/pull.1043.git.1632160658.gitgitgadget@gmail.com\n>\n> Thanks, -Stolee\n>\n>\n> Updates in v2 and v3\n> ====================\n>\n>  * I intended to fix a typo in a patch, but accidentally sent the amend!\n>    commit in v2\n>  * v3 has the typo fix properly squashed in.\n>  * Added Elijah's review.\n>\n> Derrick Stolee (3):\n>   sparse-checkout: fix segfault on malformed patterns\n>   sparse-checkout: fix OOM error with mixed patterns\n>   sparse-checkout: refuse to add to bad patterns\n>\n>  builtin/sparse-checkout.c          |  5 ++++-\n>  dir.c                              |  5 +----\n>  t/t1091-sparse-checkout-builtin.sh | 31 +++++++++++++++++++++++++++++-\n>  3 files changed, 35 insertions(+), 6 deletions(-)\n>\n>\n> base-commit: abe6bb3905392d5eb6b01fa6e54d7e784e0522aa\n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1069%2Fderrickstolee%2Fsparse-checkout%2Finput-bug-v3\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1069/derrickstolee/sparse-checkout/input-bug-v3\n> Pull-Request: https://github.com/gitgitgadget/git/pull/1069\n>\n> Range-diff vs v2:\n>\n>  1:  a0e3dd335c9 ! 1:  1744a26845f sparse-checkout: fix segfault on malformed patterns\n>      @@ Commit message\n>           there is additional logic that attempts to remove the line from the hashset\n>           and free the data. This leads to a segfault in the 'git sparse-checkout\n>           list' command because it iterates over the contents of the hashset, which is\n>      -    no invalid.\n>      +    now invalid.\n>       \n>           The fix here is to stop trying to remove from the hashset. Better to leave\n>           bad data in the sparse-checkout matching logic (with a warning) than to\n>  2:  86fbf130c03 = 2:  a2fe867222e sparse-checkout: fix OOM error with mixed patterns\n>  3:  5d096e380a4 = 3:  a0e5a942ae0 sparse-checkout: refuse to add to bad patterns\n>  4:  7bacb3760f3 < -:  ----------- amend! sparse-checkout: fix segfault on malformed patterns\n"},{"id":"444170","messageId":"xmqqv8zp4mfm.fsf@gitster.g","threadId":"57050","inReplyTo":"1744a26845fbe4d7dbc80f387be1d842b5f8fe94.1639575968.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 1/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-12-15T20:56:13Z","receivedAt":"2021-12-15T20:56:20Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Derrick Stolee via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Derrick Stolee <dstolee@microsoft.com>\n>\n> Then core.sparseCheckoutCone is enabled, the sparse-checkout patterns are\n> used to populate two hashsets that accelerate pattern matching. If the user\n> modifies the sparse-checkout file outside of the 'sparse-checkout' builtin,\n> then strange patterns can happen, triggering some error checks.\n>\n> One of these error checks is possible to hit when some special characters\n> exist in a line. A warning message is correctly written to stderr, but then\n> there is additional logic that attempts to remove the line from the hashset\n> and free the data.\n\nMakes sense.\n\n> This leads to a segfault in the 'git sparse-checkout\n> list' command because it iterates over the contents of the hashset, which is\n> now invalid.\n\nUnderstandable.\n\n> The fix here is to stop trying to remove from the hashset. Better to leave\n> bad data in the sparse-checkout matching logic (with a warning) than to\n> segfault.\n\nTrue, as long as it won't make the situation worse by depending on\nthat bad data to further damage working tree data or in-repo data\nwhen damaged working tree data gets committed.  And \"list segfaults\nwith freed/NULLed data---so leave the bad ones in to print these bad\nones\" feels OK-ish.  \n\nAs long as the user is not transporting the listed output to another\nrepository, which may fall into \"making the situation worse\"\ncategory by spreading an existing breakage, that is.\n\nIn other words, this may paper over the segfault, and it may be safe\nonly for \"sparse-checkout list\", but is it safe for other operations\nthat actually use this bad data to further affect other things in\nthe repository?  If not, I wonder if we want to hard die to lock the\nrepository down before the issue is fixed to avoid spreading the\ndamage?\n\n> diff --git a/dir.c b/dir.c\n> index 5aa6fbad0b7..0693c7cb3ee 100644\n> --- a/dir.c\n> +++ b/dir.c\n> @@ -819,9 +819,6 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern\n>  \t\t/* we already included this at the parent level */\n>  \t\twarning(_(\"your sparse-checkout file may have issues: pattern '%s' is repeated\"),\n>  \t\t\tgiven->pattern);\n> -\t\thashmap_remove(&pl->parent_hashmap, &translated->ent, &data);\n> -\t\tfree(data);\n> -\t\tfree(translated);\n>  \t}\n>  \n>  \treturn;\n> diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\n> index 272ba1b566b..c72b8ee2e7b 100755\n> --- a/t/t1091-sparse-checkout-builtin.sh\n> +++ b/t/t1091-sparse-checkout-builtin.sh\n> @@ -708,4 +708,19 @@ test_expect_success 'cone mode clears ignored subdirectories' '\n>  \ttest_cmp expect out\n>  '\n>  \n> +test_expect_success 'malformed cone-mode patterns' '\n> +\tgit -C repo sparse-checkout init --cone &&\n> +\tmkdir -p repo/foo/bar &&\n> +\ttouch repo/foo/bar/x repo/foo/y &&\n> +\tcat >repo/.git/info/sparse-checkout <<-\\EOF &&\n> +\t/*\n> +\t!/*/\n> +\t/foo/\n> +\t!/foo/*/\n> +\t/foo/\\*/\n> +\tEOF\n> +\tcat repo/.git/info/sparse-checkout &&\n\nStray debugging output?\n\n> +\tgit -C repo sparse-checkout list\n\nAnd we are happy as long as the command does not segfault, and we do\nnot care what the output is.\n\n> +'\n> +\n>  test_done\n\nWill queue, but not convinced yet.\n"},{"id":"444226","messageId":"55dee0a3-5a55-1af9-74b3-296426458f5c@gmail.com","threadId":"57050","inReplyTo":"xmqqv8zp4mfm.fsf@gitster.g","subject":"Re: [PATCH v3 1/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2021-12-16T14:23:10Z","receivedAt":"2021-12-16T14:23:16Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 12/15/2021 3:56 PM, Junio C Hamano wrote:\n> \"Derrick Stolee via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n> \n>> From: Derrick Stolee <dstolee@microsoft.com>\n>>\n>> Then core.sparseCheckoutCone is enabled, the sparse-checkout patterns are\n>> used to populate two hashsets that accelerate pattern matching. If the user\n>> modifies the sparse-checkout file outside of the 'sparse-checkout' builtin,\n>> then strange patterns can happen, triggering some error checks.\n>>\n>> One of these error checks is possible to hit when some special characters\n>> exist in a line. A warning message is correctly written to stderr, but then\n>> there is additional logic that attempts to remove the line from the hashset\n>> and free the data.\n> \n> Makes sense.\n> \n>> This leads to a segfault in the 'git sparse-checkout\n>> list' command because it iterates over the contents of the hashset, which is\n>> now invalid.\n> \n> Understandable.\n> \n>> The fix here is to stop trying to remove from the hashset. Better to leave\n>> bad data in the sparse-checkout matching logic (with a warning) than to\n>> segfault.\n> \n> True, as long as it won't make the situation worse by depending on\n> that bad data to further damage working tree data or in-repo data\n> when damaged working tree data gets committed.  And \"list segfaults\n> with freed/NULLed data---so leave the bad ones in to print these bad\n> ones\" feels OK-ish.  \n> \n> As long as the user is not transporting the listed output to another\n> repository, which may fall into \"making the situation worse\"\n> category by spreading an existing breakage, that is.\n> \n> In other words, this may paper over the segfault, and it may be safe\n> only for \"sparse-checkout list\", but is it safe for other operations\n> that actually use this bad data to further affect other things in\n> the repository?  If not, I wonder if we want to hard die to lock the\n> repository down before the issue is fixed to avoid spreading the\n> damage?\n\nYou're right. I should do what we do elsewhere in this method and\ndisable cone mode pattern matching, reverting to the old matching\nalgorithms. This will clear the hashsets and avoid using them anywhere\nelse.\n\n>> +test_expect_success 'malformed cone-mode patterns' '\n>> +\tgit -C repo sparse-checkout init --cone &&\n>> +\tmkdir -p repo/foo/bar &&\n>> +\ttouch repo/foo/bar/x repo/foo/y &&\n>> +\tcat >repo/.git/info/sparse-checkout <<-\\EOF &&\n>> +\t/*\n>> +\t!/*/\n>> +\t/foo/\n>> +\t!/foo/*/\n>> +\t/foo/\\*/\n>> +\tEOF\n>> +\tcat repo/.git/info/sparse-checkout &&\n> \n> Stray debugging output?\n\nThanks.\n\n>> +\tgit -C repo sparse-checkout list\n> \n> And we are happy as long as the command does not segfault, and we do\n> not care what the output is.\n\nAnd I'll be able to test that the 'list' subcommand changes behavior\nwhen cone mode is disabled, in addition to checking stderr for the\ncorrect warnings.\n\nThanks,\n-Stolee\n"},{"id":"444227","messageId":"e0f90c01-f987-1c9c-cd93-393d0dc6e0dc@gmail.com","threadId":"57050","inReplyTo":"xmqq5yrp61lw.fsf@gitster.g","subject":"Re: [PATCH v3 0/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2021-12-16T14:24:35Z","receivedAt":"2021-12-16T14:24:41Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 12/15/2021 3:43 PM, Junio C Hamano wrote:\n> \"Derrick Stolee via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n> \n>> This series fixes some issues with parsing sparse-checkout patterns when\n>> core.sparseCheckoutCone is enabled but the sparse-checkout file itself\n>> contains patterns that don't match the cone mode format.\n>>\n>> The first patch fixes a segfault first reported in [1]. The other two\n>> patches are from an earlier submission [2] that never got picked up and I\n>> lost track of. There was another patch involving 'git sparse-checkout init\n>> --cone' that isn't necessary, especially with Elijah doing some work in that\n>> space right now.\n> \n> Thanks.  The segfault fix matters even more with Elijah's \"we can\n> flip between modes\" feature, right?\n\nGenerally, I think segfaults are a higher priority, but this one is hard\nto find in the wild, I think. I'll send a v4 today and hopefully it makes\nit an easy decision for you.\n\nThanks,\n-Stolee\n"},{"id":"444235","messageId":"pull.1069.v4.git.1639671222.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.v3.git.1639575968.gitgitgadget@gmail.com","subject":"[PATCH v4 0/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-16T16:13:39Z","receivedAt":"2021-12-16T16:13:47Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"This series fixes some issues with parsing sparse-checkout patterns when\ncore.sparseCheckoutCone is enabled but the sparse-checkout file itself\ncontains patterns that don't match the cone mode format.\n\nThe first patch fixes a segfault first reported in [1]. The other two\npatches are from an earlier submission [2] that never got picked up and I\nlost track of. There was another patch involving 'git sparse-checkout init\n--cone' that isn't necessary, especially with Elijah doing some work in that\nspace right now.\n\n[1] https://github.com/git-for-windows/git/issues/3498 [2]\nhttps://lore.kernel.org/git/pull.1043.git.1632160658.gitgitgadget@gmail.com\n\nThanks, -Stolee\n\n\nUpdate in v4\n============\n\n * For added precaution, this kind of unexpected duplicate pattern will\n   disable cone mode matching.\n * Tests are updated to verify this new behavior.\n\n\nUpdates in v2 and v3\n====================\n\n * I intended to fix a typo in a patch, but accidentally sent the amend!\n   commit in v2\n * v3 has the typo fix properly squashed in.\n * Added Elijah's review.\n\nDerrick Stolee (3):\n  sparse-checkout: fix segfault on malformed patterns\n  sparse-checkout: fix OOM error with mixed patterns\n  sparse-checkout: refuse to add to bad patterns\n\n builtin/sparse-checkout.c          |  5 +++-\n dir.c                              |  6 ++---\n t/t1091-sparse-checkout-builtin.sh | 37 +++++++++++++++++++++++++++++-\n 3 files changed, 42 insertions(+), 6 deletions(-)\n\n\nbase-commit: abe6bb3905392d5eb6b01fa6e54d7e784e0522aa\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1069%2Fderrickstolee%2Fsparse-checkout%2Finput-bug-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1069/derrickstolee/sparse-checkout/input-bug-v4\nPull-Request: https://github.com/gitgitgadget/git/pull/1069\n\nRange-diff vs v3:\n\n 1:  1744a26845f ! 1:  5353c541d9f sparse-checkout: fix segfault on malformed patterns\n     @@ Commit message\n          list' command because it iterates over the contents of the hashset, which is\n          now invalid.\n      \n     -    The fix here is to stop trying to remove from the hashset. Better to leave\n     -    bad data in the sparse-checkout matching logic (with a warning) than to\n     -    segfault. If we are in this state, then we are already traversing into\n     -    undefined behavior, so this change to keep the entry in the hashset is no\n     -    worse than removing it.\n     +    The fix here is to stop trying to remove from the hashset. In addition,\n     +    we disable cone mode sparse-checkout because of the malformed data. This\n     +    results in the pattern-matching working with a possibly-slower\n     +    algorithm, but using the patterns as they are in the sparse-checkout\n     +    file.\n     +\n     +    This also changes the behavior of commands such as 'git sparse-checkout\n     +    list' because the output patterns will be the contents of the\n     +    sparse-checkout file instead of the list of directories. This is an\n     +    existing behavior for other types of bad patterns.\n      \n          Add a test that triggers the segfault without the code change.\n      \n     @@ dir.c: static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_\n      -\t\thashmap_remove(&pl->parent_hashmap, &translated->ent, &data);\n      -\t\tfree(data);\n      -\t\tfree(translated);\n     ++\t\tgoto clear_hashmaps;\n       \t}\n       \n       \treturn;\n     @@ t/t1091-sparse-checkout-builtin.sh: test_expect_success 'cone mode clears ignore\n      +\t!/foo/*/\n      +\t/foo/\\*/\n      +\tEOF\n     -+\tcat repo/.git/info/sparse-checkout &&\n     -+\tgit -C repo sparse-checkout list\n     ++\n     ++\t# Listing the patterns will notice the duplicate pattern and\n     ++\t# emit a warning. It will list the patterns directly instead\n     ++\t# of using the cone-mode translation to a set of directories.\n     ++\tgit -C repo sparse-checkout list >actual 2>err &&\n     ++\ttest_cmp repo/.git/info/sparse-checkout actual &&\n     ++\tgrep \"warning: your sparse-checkout file may have issues: pattern .* is repeated\" err &&\n     ++\tgrep \"warning: disabling cone pattern matching\" err\n      +'\n      +\n       test_done\n 2:  a2fe867222e = 2:  3fd625290a3 sparse-checkout: fix OOM error with mixed patterns\n 3:  a0e5a942ae0 = 3:  f5f7b8b8e04 sparse-checkout: refuse to add to bad patterns\n\n-- \ngitgitgadget\n"},{"id":"444236","messageId":"5353c541d9f72aa0a14cd2b02e5ddcc0b6a4dae1.1639671222.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.v4.git.1639671222.gitgitgadget@gmail.com","subject":"[PATCH v4 1/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-16T16:13:40Z","receivedAt":"2021-12-16T16:13:48Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThen core.sparseCheckoutCone is enabled, the sparse-checkout patterns are\nused to populate two hashsets that accelerate pattern matching. If the user\nmodifies the sparse-checkout file outside of the 'sparse-checkout' builtin,\nthen strange patterns can happen, triggering some error checks.\n\nOne of these error checks is possible to hit when some special characters\nexist in a line. A warning message is correctly written to stderr, but then\nthere is additional logic that attempts to remove the line from the hashset\nand free the data. This leads to a segfault in the 'git sparse-checkout\nlist' command because it iterates over the contents of the hashset, which is\nnow invalid.\n\nThe fix here is to stop trying to remove from the hashset. In addition,\nwe disable cone mode sparse-checkout because of the malformed data. This\nresults in the pattern-matching working with a possibly-slower\nalgorithm, but using the patterns as they are in the sparse-checkout\nfile.\n\nThis also changes the behavior of commands such as 'git sparse-checkout\nlist' because the output patterns will be the contents of the\nsparse-checkout file instead of the list of directories. This is an\nexisting behavior for other types of bad patterns.\n\nAdd a test that triggers the segfault without the code change.\n\nReported-by: John Burnett <johnburnett@johnburnett.com>\nReviewed-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n dir.c                              |  4 +---\n t/t1091-sparse-checkout-builtin.sh | 21 +++++++++++++++++++++\n 2 files changed, 22 insertions(+), 3 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex 5aa6fbad0b7..7e72958d51d 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -819,9 +819,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern\n \t\t/* we already included this at the parent level */\n \t\twarning(_(\"your sparse-checkout file may have issues: pattern '%s' is repeated\"),\n \t\t\tgiven->pattern);\n-\t\thashmap_remove(&pl->parent_hashmap, &translated->ent, &data);\n-\t\tfree(data);\n-\t\tfree(translated);\n+\t\tgoto clear_hashmaps;\n \t}\n \n \treturn;\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex 272ba1b566b..3921ea80138 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -708,4 +708,25 @@ test_expect_success 'cone mode clears ignored subdirectories' '\n \ttest_cmp expect out\n '\n \n+test_expect_success 'malformed cone-mode patterns' '\n+\tgit -C repo sparse-checkout init --cone &&\n+\tmkdir -p repo/foo/bar &&\n+\ttouch repo/foo/bar/x repo/foo/y &&\n+\tcat >repo/.git/info/sparse-checkout <<-\\EOF &&\n+\t/*\n+\t!/*/\n+\t/foo/\n+\t!/foo/*/\n+\t/foo/\\*/\n+\tEOF\n+\n+\t# Listing the patterns will notice the duplicate pattern and\n+\t# emit a warning. It will list the patterns directly instead\n+\t# of using the cone-mode translation to a set of directories.\n+\tgit -C repo sparse-checkout list >actual 2>err &&\n+\ttest_cmp repo/.git/info/sparse-checkout actual &&\n+\tgrep \"warning: your sparse-checkout file may have issues: pattern .* is repeated\" err &&\n+\tgrep \"warning: disabling cone pattern matching\" err\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"444237","messageId":"3fd625290a318f853c7a96cbc10be76308ef2a3d.1639671222.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.v4.git.1639671222.gitgitgadget@gmail.com","subject":"[PATCH v4 2/3] sparse-checkout: fix OOM error with mixed patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-16T16:13:41Z","receivedAt":"2021-12-16T16:13:52Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nAdd a test to t1091-sparse-checkout-builtin.sh that would result in an\ninfinite loop and out-of-memory error before this change. The issue\nrelies on having non-cone-mode patterns while trying to modify the\npatterns in cone-mode.\n\nThe fix is simple, allowing us to break from the loop when the input\npath does not contain a slash, as the \"dir\" pattern we added does not.\n\nThis is only a fix to the critical out-of-memory error. A better\nresponse to such a strange state will follow in a later change.\n\nReported-by: Calbabreaker <calbabreaker@gmail.com>\nHelped-by: Taylor Blau <me@ttaylorr.com>\nReviewed-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/sparse-checkout.c          |  2 +-\n t/t1091-sparse-checkout-builtin.sh | 11 +++++++++++\n 2 files changed, 12 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c\nindex d0f5c4702be..9ccdcde9832 100644\n--- a/builtin/sparse-checkout.c\n+++ b/builtin/sparse-checkout.c\n@@ -483,7 +483,7 @@ static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *pat\n \t\tchar *oldpattern = e->pattern;\n \t\tsize_t newlen;\n \n-\t\tif (slash == e->pattern)\n+\t\tif (!slash || slash == e->pattern)\n \t\t\tbreak;\n \n \t\tnewlen = slash - e->pattern;\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex 3921ea80138..1f877ced0c8 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -103,6 +103,17 @@ test_expect_success 'clone --sparse' '\n \tcheck_files clone a\n '\n \n+test_expect_success 'switching to cone mode with non-cone mode patterns' '\n+\tgit init bad-patterns &&\n+\t(\n+\t\tcd bad-patterns &&\n+\t\tgit sparse-checkout init &&\n+\t\tgit sparse-checkout add dir &&\n+\t\tgit config core.sparseCheckoutCone true &&\n+\t\tgit sparse-checkout add dir\n+\t)\n+'\n+\n test_expect_success 'interaction with clone --no-checkout (unborn index)' '\n \tgit clone --no-checkout \"file://$(pwd)/repo\" clone_no_checkout &&\n \tgit -C clone_no_checkout sparse-checkout init --cone &&\n-- \ngitgitgadget\n\n"},{"id":"444238","messageId":"f5f7b8b8e0407cfc0609f1d254b4fa0842991278.1639671222.git.gitgitgadget@gmail.com","threadId":"57050","inReplyTo":"pull.1069.v4.git.1639671222.gitgitgadget@gmail.com","subject":"[PATCH v4 3/3] sparse-checkout: refuse to add to bad patterns","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-12-16T16:13:42Z","receivedAt":"2021-12-16T16:13:52Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen in cone mode sparse-checkout, it is unclear how 'git\nsparse-checkout add <dir1> ...' should behave if the existing\nsparse-checkout file does not match the cone mode patterns. Change the\nbehavior to fail with an error message about the existing patterns.\n\nAlso, all cone mode patterns start with a '/' character, so add that\nrestriction. This is necessary for our example test 'cone mode: warn on\nbad pattern', but also requires modifying the example sparse-checkout\nfile we use to test the warnings related to recognizing cone mode\npatterns.\n\nThis error checking would cause a failure further down the test script\nbecause of a test that adds non-cone mode patterns without cleaning them\nup. Perform that cleanup as part of the test now.\n\nReviewed-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/sparse-checkout.c          | 3 +++\n dir.c                              | 2 +-\n t/t1091-sparse-checkout-builtin.sh | 7 +++++--\n 3 files changed, 9 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c\nindex 9ccdcde9832..6580075a631 100644\n--- a/builtin/sparse-checkout.c\n+++ b/builtin/sparse-checkout.c\n@@ -598,6 +598,9 @@ static void add_patterns_cone_mode(int argc, const char **argv,\n \t\tdie(_(\"unable to load existing sparse-checkout patterns\"));\n \tfree(sparse_filename);\n \n+\tif (!existing.use_cone_patterns)\n+\t\tdie(_(\"existing sparse-checkout patterns do not use cone mode\"));\n+\n \thashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {\n \t\tif (!hashmap_contains_parent(&pl->recursive_hashmap,\n \t\t\t\t\tpe->pattern, &buffer) ||\ndiff --git a/dir.c b/dir.c\nindex 7e72958d51d..eca75e89213 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -727,7 +727,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern\n \t}\n \n \tif (given->patternlen < 2 ||\n-\t    *given->pattern == '*' ||\n+\t    *given->pattern != '/' ||\n \t    strstr(given->pattern, \"**\")) {\n \t\t/* Not a cone pattern. */\n \t\twarning(_(\"unrecognized pattern: '%s'\"), given->pattern);\ndiff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh\nindex 1f877ced0c8..34b97fd3ee0 100755\n--- a/t/t1091-sparse-checkout-builtin.sh\n+++ b/t/t1091-sparse-checkout-builtin.sh\n@@ -110,7 +110,8 @@ test_expect_success 'switching to cone mode with non-cone mode patterns' '\n \t\tgit sparse-checkout init &&\n \t\tgit sparse-checkout add dir &&\n \t\tgit config core.sparseCheckoutCone true &&\n-\t\tgit sparse-checkout add dir\n+\t\ttest_must_fail git sparse-checkout add dir 2>err &&\n+\t\tgrep \"existing sparse-checkout patterns do not use cone mode\" err\n \t)\n '\n \n@@ -176,12 +177,14 @@ test_expect_success 'set sparse-checkout using --stdin' '\n '\n \n test_expect_success 'add to sparse-checkout' '\n-\tcat repo/.git/info/sparse-checkout >expect &&\n+\tcat repo/.git/info/sparse-checkout >old &&\n+\ttest_when_finished cp old repo/.git/info/sparse-checkout &&\n \tcat >add <<-\\EOF &&\n \tpattern1\n \t/folder1/\n \tpattern2\n \tEOF\n+\tcat old >expect &&\n \tcat add >>expect &&\n \tgit -C repo sparse-checkout add --stdin <add &&\n \tgit -C repo sparse-checkout list >actual &&\n-- \ngitgitgadget\n"},{"id":"444252","messageId":"xmqqo85gbbsd.fsf@gitster.g","threadId":"57050","inReplyTo":"e0f90c01-f987-1c9c-cd93-393d0dc6e0dc@gmail.com","subject":"Re: [PATCH v3 0/3] sparse-checkout: fix segfault on malformed patterns","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-12-16T19:16:34Z","receivedAt":"2021-12-16T19:16:41Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Derrick Stolee <stolee@gmail.com> writes:\n\n>> Thanks.  The segfault fix matters even more with Elijah's \"we can\n>> flip between modes\" feature, right?\n>\n> Generally, I think segfaults are a higher priority, but this one is hard\n> to find in the wild, I think. I'll send a v4 today and hopefully it makes\n> it an easy decision for you.\n\nI agree about the priority and I was assuming that the topics do not\nhave semantic conflicts we cannot reconcile, but I do not offhand\nrecall what the other topic did when there patterns unsuitable in\nthe cone mode when transitioning to the cone mode (or vice-versa for\nthat matter).\n"}]}