sparse-checkout: fix OOM error with mixed patterns

Add a test to t1091-sparse-checkout-builtin.sh that would result in an infinite loop and out-of-memory error before this change. The issue relies on having non-cone-mode patterns while trying to modify the patterns in cone-mode. The fix is simple, allowing us to break from the loop when the input path does not contain a slash, as the "dir" pattern we added does not. This is only a fix to the critical out-of-memory error. A better response to such a strange state will follow in a later change. Reported-by: Calbabreaker <calbabreaker@gmail.com> Helped-by: Taylor Blau <me@ttaylorr.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Dec 16, 2021 at 16:13 UTC 391c3a1020127c9dbf6995effe2f84d3443292d8
2 files changed +12 -1
builtin/sparse-checkout.c
+1 -1
@@ -483,7 +483,7 @@ static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *pat
483 char *oldpattern = e->pattern;
484 size_t newlen;
485
486 - if (slash == e->pattern)
486 + if (!slash || slash == e->pattern)
487 break;
488
489 newlen = slash - e->pattern;
t/t1091-sparse-checkout-builtin.sh
+11
@@ -103,6 +103,17 @@ test_expect_success 'clone --sparse' '
103 check_files clone a
104 '
105
106 +test_expect_success 'switching to cone mode with non-cone mode patterns' '
107 + git init bad-patterns &&
108 + (
109 + cd bad-patterns &&
110 + git sparse-checkout init &&
111 + git sparse-checkout add dir &&
112 + git config core.sparseCheckoutCone true &&
113 + git sparse-checkout add dir
114 + )
115 +'
116 +
117 test_expect_success 'interaction with clone --no-checkout (unborn index)' '
118 git clone --no-checkout "file://$(pwd)/repo" clone_no_checkout &&
119 git -C clone_no_checkout sparse-checkout init --cone &&