]> git.ipfire.org Git - thirdparty/git.git/commit - dir.c
sparse-checkout: fix segfault on malformed patterns
authorDerrick Stolee <dstolee@microsoft.com>
Thu, 16 Dec 2021 16:13:40 +0000 (16:13 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Dec 2021 22:39:57 +0000 (14:39 -0800)
commita481d4378cc503ac5646d44533a05fcac569a93f
treecb2b83b1b76f4548f7970fa97417ee65262232e5
parente773545c7fe7eca21b134847f4fc2cbc9547fa14
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 <johnburnett@johnburnett.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>
dir.c
t/t1091-sparse-checkout-builtin.sh