[BUG] git-check-ignore: Segmentation fault

4 messages, 2 authors, 2016-06-15 · open the first message on its own page

[BUG] git-check-ignore: Segmentation fault

From: Zoltan Klinger <hidden>
Date: 2016-06-15 22:56:11

Hi there,

The new git-check-ignore command seg faults when
    (1) it is called with single dot path name at $GIT_DIR level  _AND_
    (2) and .gitignore has at least one directory pattern.

Git version: 1.8.2.rc0.16.g20a599e

Reproduce the bug:
    $ git --version
    git version 1.8.2.rc0.16.g20a599e
    $ mkdir test
    $ cd test
    $ git init
    $ git check-ignore .  # All good, no errors here
    $ echo "dirpattern/" > .gitignore
    $ git check-ignore .
    Segmentation fault (core dumped)

The segmentation fault is actually caused by hash_name(const char
*name, int namelen) function in name-hash.c when the 'name' argument
is an empty stringi and namelen is 0.

The empty string comes from a call to the prefix_path(prefix, len,
path) function in setup.c. In this instance arguments 'prefix' is
NULL, 'len' is 0 and 'path' is "." .

Cheers,
Zoltan

Re: [BUG] git-check-ignore: Segmentation fault

From: Adam Spiers <hidden>
Date: 2016-06-15 22:56:12

On Tue, Feb 19, 2013 at 5:24 AM, Zoltan Klinger
[off-list ref] wrote:
Hi there,

The new git-check-ignore command seg faults when
    (1) it is called with single dot path name at $GIT_DIR level  _AND_
    (2) and .gitignore has at least one directory pattern.

Git version: 1.8.2.rc0.16.g20a599e

Reproduce the bug:
    $ git --version
    git version 1.8.2.rc0.16.g20a599e
    $ mkdir test
    $ cd test
    $ git init
    $ git check-ignore .  # All good, no errors here
    $ echo "dirpattern/" > .gitignore
    $ git check-ignore .
    Segmentation fault (core dumped)

The segmentation fault is actually caused by hash_name(const char
*name, int namelen) function in name-hash.c when the 'name' argument
is an empty stringi and namelen is 0.

The empty string comes from a call to the prefix_path(prefix, len,
path) function in setup.c. In this instance arguments 'prefix' is
NULL, 'len' is 0 and 'path' is "." .
Good catch!  Thanks for the very helpful bug report.  I can reproduce
this, and have a fix - see follow-up mail to follow shortly.

[PATCH 1/2] t0008: document test_expect_success_multi

From: Adam Spiers <hidden>
Date: 2016-06-15 22:56:12

test_expect_success_multi() helper function warrants some explanation,
since at first sight it may seem like generic test framework plumbing,
but is in fact specific to testing check-ignore, and allows more
thorough testing of the various output formats without significantly
increase the size of t0008.

Signed-off-by: Adam Spiers <redacted>
---
 t/t0008-ignores.sh | 10 ++++++++++
 1 file changed, 10 insertions(+)
diff --git a/t/t0008-ignores.sh b/t/t0008-ignores.sh
index d7df719..ebe7c70 100755
--- a/t/t0008-ignores.sh
+++ b/t/t0008-ignores.sh
@@ -75,6 +75,16 @@ test_check_ignore () {
 	stderr_empty_on_success "$expect_code"
 }
 
+# Runs the same code with 3 different levels of output verbosity,
+# expecting success each time.  Takes advantage of the fact that
+# check-ignore --verbose output is the same as normal output except
+# for the extra first column.
+#
+# Arguments:
+#   - (optional) prereqs for this test, e.g. 'SYMLINKS'
+#   - test name
+#   - output to expect from -v / --verbose mode
+#   - code to run (should invoke test_check_ignore)
 test_expect_success_multi () {
 	prereq=
 	if test $# -eq 4
-- 
1.8.1.291.g0730ed6

[PATCH 2/2] check-ignore.c: fix segfault with '.' argument from repo root

From: Adam Spiers <hidden>
Date: 2016-06-15 22:56:12

Fix a corner case where check-ignore would segfault when run with the
'.' argument from the top level of a repository, due to prefix_path()
converting '.' into the empty string.  It doesn't make much sense to
call check-ignore from the top level with '.' as a parameter, since
the top-level directory would never typically be ignored, but of
course it should not segfault in this case.

Signed-off-by: Adam Spiers <redacted>
---
 builtin/check-ignore.c | 2 +-
 t/t0008-ignores.sh     | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/builtin/check-ignore.c b/builtin/check-ignore.c
index 709535c..b0dd7c2 100644
--- a/builtin/check-ignore.c
+++ b/builtin/check-ignore.c
@@ -89,7 +89,7 @@ static int check_ignore(const char *prefix, const char **pathspec)
 					? strlen(prefix) : 0, path);
 		full_path = check_path_for_gitlink(full_path);
 		die_if_path_beyond_symlink(full_path, prefix);
-		if (!seen[i] && path[0]) {
+		if (!seen[i] && full_path[0]) {
 			exclude = last_exclude_matching_path(&check, full_path,
 							     -1, &dtype);
 			if (exclude) {
diff --git a/t/t0008-ignores.sh b/t/t0008-ignores.sh
index ebe7c70..9c1bde1 100755
--- a/t/t0008-ignores.sh
+++ b/t/t0008-ignores.sh
@@ -138,6 +138,7 @@ test_expect_success 'setup' '
 	cat <<-\EOF >.gitignore &&
 		one
 		ignored-*
+		top-level-dir/
 	EOF
 	for dir in . a
 	do
@@ -177,6 +178,10 @@ test_expect_success 'setup' '
 #
 # test invalid inputs
 
+test_expect_success_multi '. corner-case' '' '
+	test_check_ignore . 1
+'
+
 test_expect_success_multi 'empty command line' '' '
 	test_check_ignore "" 128 &&
 	stderr_contains "fatal: no path specified"
-- 
1.8.1.291.g0730ed6
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help