Skip to content

Commit a481d43

Browse files
derrickstoleegitster
authored andcommitted
sparse-checkout: fix segfault on malformed patterns
Then core.sparseCheckoutCone is enabled, the sparse-checkout patterns are used to populate two hashsets that accelerate pattern matching. If the user modifies the sparse-checkout file outside of the 'sparse-checkout' builtin, then strange patterns can happen, triggering some error checks. One of these error checks is possible to hit when some special characters exist in a line. A warning message is correctly written to stderr, but then there is additional logic that attempts to remove the line from the hashset and free the data. This leads to a segfault in the 'git sparse-checkout list' command because it iterates over the contents of the hashset, which is now invalid. The fix here is to stop trying to remove from the hashset. In addition, we disable cone mode sparse-checkout because of the malformed data. This results in the pattern-matching working with a possibly-slower algorithm, but using the patterns as they are in the sparse-checkout file. This also changes the behavior of commands such as 'git sparse-checkout list' because the output patterns will be the contents of the sparse-checkout file instead of the list of directories. This is an existing behavior for other types of bad patterns. Add a test that triggers the segfault without the code change. Reported-by: John Burnett <[email protected]> Reviewed-by: Elijah Newren <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e773545 commit a481d43

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

dir.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,4 +708,25 @@ test_expect_success 'cone mode clears ignored subdirectories' '
708708
test_cmp expect out
709709
'
710710

711+
test_expect_success 'malformed cone-mode patterns' '
712+
git -C repo sparse-checkout init --cone &&
713+
mkdir -p repo/foo/bar &&
714+
touch repo/foo/bar/x repo/foo/y &&
715+
cat >repo/.git/info/sparse-checkout <<-\EOF &&
716+
/*
717+
!/*/
718+
/foo/
719+
!/foo/*/
720+
/foo/\*/
721+
EOF
722+
723+
# Listing the patterns will notice the duplicate pattern and
724+
# emit a warning. It will list the patterns directly instead
725+
# of using the cone-mode translation to a set of directories.
726+
git -C repo sparse-checkout list >actual 2>err &&
727+
test_cmp repo/.git/info/sparse-checkout actual &&
728+
grep "warning: your sparse-checkout file may have issues: pattern .* is repeated" err &&
729+
grep "warning: disabling cone pattern matching" err
730+
'
731+
711732
test_done

0 commit comments

Comments
 (0)