Skip to content

Commit 09481fe

Browse files
committed
Merge branch 'ds/sparse-checkout-malformed-pattern-fix'
Certain sparse-checkout patterns that are valid in non-cone mode led to segfault in cone mode, which has been corrected. * ds/sparse-checkout-malformed-pattern-fix: sparse-checkout: refuse to add to bad patterns sparse-checkout: fix OOM error with mixed patterns sparse-checkout: fix segfault on malformed patterns
2 parents e83ba64 + a3eca58 commit 09481fe

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

builtin/sparse-checkout.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *pat
504504
char *oldpattern = e->pattern;
505505
size_t newlen;
506506

507-
if (slash == e->pattern)
507+
if (!slash || slash == e->pattern)
508508
break;
509509

510510
newlen = slash - e->pattern;
@@ -612,6 +612,9 @@ static void add_patterns_cone_mode(int argc, const char **argv,
612612
die(_("unable to load existing sparse-checkout patterns"));
613613
free(sparse_filename);
614614

615+
if (!existing.use_cone_patterns)
616+
die(_("existing sparse-checkout patterns do not use cone mode"));
617+
615618
hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
616619
if (!hashmap_contains_parent(&pl->recursive_hashmap,
617620
pe->pattern, &buffer) ||

dir.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
727727
}
728728

729729
if (given->patternlen < 2 ||
730-
*given->pattern == '*' ||
730+
*given->pattern != '/' ||
731731
strstr(given->pattern, "**")) {
732732
/* Not a cone pattern. */
733733
warning(_("unrecognized pattern: '%s'"), given->pattern);
@@ -819,9 +819,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
819819
/* we already included this at the parent level */
820820
warning(_("your sparse-checkout file may have issues: pattern '%s' is repeated"),
821821
given->pattern);
822-
hashmap_remove(&pl->parent_hashmap, &translated->ent, &data);
823-
free(data);
824-
free(translated);
822+
goto clear_hashmaps;
825823
}
826824

827825
return;

t/t1091-sparse-checkout-builtin.sh

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ test_expect_success 'clone --sparse' '
111111
check_files clone a
112112
'
113113

114+
test_expect_success 'switching to cone mode with non-cone mode patterns' '
115+
git init bad-patterns &&
116+
(
117+
cd bad-patterns &&
118+
git sparse-checkout init &&
119+
git sparse-checkout add dir &&
120+
git config core.sparseCheckoutCone true &&
121+
test_must_fail git sparse-checkout add dir 2>err &&
122+
grep "existing sparse-checkout patterns do not use cone mode" err
123+
)
124+
'
125+
114126
test_expect_success 'interaction with clone --no-checkout (unborn index)' '
115127
git clone --no-checkout "file://$(pwd)/repo" clone_no_checkout &&
116128
git -C clone_no_checkout sparse-checkout init --cone &&
@@ -173,12 +185,14 @@ test_expect_success 'set sparse-checkout using --stdin' '
173185
'
174186

175187
test_expect_success 'add to sparse-checkout' '
176-
cat repo/.git/info/sparse-checkout >expect &&
188+
cat repo/.git/info/sparse-checkout >old &&
189+
test_when_finished cp old repo/.git/info/sparse-checkout &&
177190
cat >add <<-\EOF &&
178191
pattern1
179192
/folder1/
180193
pattern2
181194
EOF
195+
cat old >expect &&
182196
cat add >>expect &&
183197
git -C repo sparse-checkout add --stdin <add &&
184198
git -C repo sparse-checkout list >actual &&
@@ -716,4 +730,25 @@ test_expect_success 'cone mode clears ignored subdirectories' '
716730
test_cmp expect out
717731
'
718732

733+
test_expect_success 'malformed cone-mode patterns' '
734+
git -C repo sparse-checkout init --cone &&
735+
mkdir -p repo/foo/bar &&
736+
touch repo/foo/bar/x repo/foo/y &&
737+
cat >repo/.git/info/sparse-checkout <<-\EOF &&
738+
/*
739+
!/*/
740+
/foo/
741+
!/foo/*/
742+
/foo/\*/
743+
EOF
744+
745+
# Listing the patterns will notice the duplicate pattern and
746+
# emit a warning. It will list the patterns directly instead
747+
# of using the cone-mode translation to a set of directories.
748+
git -C repo sparse-checkout list >actual 2>err &&
749+
test_cmp repo/.git/info/sparse-checkout actual &&
750+
grep "warning: your sparse-checkout file may have issues: pattern .* is repeated" err &&
751+
grep "warning: disabling cone pattern matching" err
752+
'
753+
719754
test_done

0 commit comments

Comments
 (0)