# [PATCH 1/5] t2004: modernize style

8 messages from 2014-12-24 to 2014-12-29. Participants: Eric Sunshine, Junio C Hamano.
Thread: https://gitlist.dev/t/38244

## Eric Sunshine, 2014-12-24 09:43

Subject: [PATCH 0/5] resolve checkout-index --temp path mangling
Message-ID: <1419414196-58587-1-git-send-email-sunshine@sunshineco.com>
URL: https://gitlist.dev/e/1419414196-58587-1-git-send-email-sunshine%40sunshineco.com

```
This patch series fixes a path mangling bug in checkout-index --temp
reported by Russ Cox[1]. As a bonus, it also squashes a bug in which
checkout-index was accessing and printing memory beyond end-of-string.

[1]: http://thread.gmane.org/gmane.comp.version-control.git/261739

Eric Sunshine (5):
  t2004: modernize style
  t2004: drop unnecessary write-tree/read-tree
  t2004: standardize file naming in symlink test
  t2004: demonstrate broken relative path printing
  checkout-index: fix --temp relative path mangling

 builtin/checkout-index.c       |  16 +-
 t/t2004-checkout-cache-temp.sh | 400 +++++++++++++++++++++--------------------
 2 files changed, 213 insertions(+), 203 deletions(-)

-- 
2.2.1.267.g0ad48de

```

## Eric Sunshine, 2014-12-24 09:43

Subject: [PATCH 1/5] t2004: modernize style
Message-ID: <1419414196-58587-2-git-send-email-sunshine@sunshineco.com>
URL: https://gitlist.dev/e/1419414196-58587-2-git-send-email-sunshine%40sunshineco.com
In-Reply-To: <1419414196-58587-1-git-send-email-sunshine@sunshineco.com>

```
In particular:

* indent test body
* place test description on same line as test_expect_*
* place closing quote on its own line
* name output file "actual" rather than "out"
* name setup test "setup" rather than "preparation"

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 t/t2004-checkout-cache-temp.sh | 391 +++++++++++++++++++++--------------------
 1 file changed, 196 insertions(+), 195 deletions(-)

diff --git a/t/t2004-checkout-cache-temp.sh b/t/t2004-checkout-cache-temp.sh
index f171a55..28e50d6 100755
--- a/t/t2004-checkout-cache-temp.sh
+++ b/t/t2004-checkout-cache-temp.sh
@@ -10,202 +10,203 @@ rather than the tracked path.'
 
 . ./test-lib.sh
 
-test_expect_success \
-'preparation' '
-mkdir asubdir &&
-echo tree1path0 >path0 &&
-echo tree1path1 >path1 &&
-echo tree1path3 >path3 &&
-echo tree1path4 >path4 &&
-echo tree1asubdir/path5 >asubdir/path5 &&
-git update-index --add path0 path1 path3 path4 asubdir/path5 &&
-t1=$(git write-tree) &&
-rm -f path* .merge_* out .git/index &&
-echo tree2path0 >path0 &&
-echo tree2path1 >path1 &&
-echo tree2path2 >path2 &&
-echo tree2path4 >path4 &&
-git update-index --add path0 path1 path2 path4 &&
-t2=$(git write-tree) &&
-rm -f path* .merge_* out .git/index &&
-echo tree2path0 >path0 &&
-echo tree3path1 >path1 &&
-echo tree3path2 >path2 &&
-echo tree3path3 >path3 &&
-git update-index --add path0 path1 path2 path3 &&
-t3=$(git write-tree)'
-
-test_expect_success \
-'checkout one stage 0 to temporary file' '
-rm -f path* .merge_* out .git/index &&
-git read-tree $t1 &&
-git checkout-index --temp -- path1 >out &&
-test_line_count = 1 out &&
-test $(cut "-d	" -f2 out) = path1 &&
-p=$(cut "-d	" -f1 out) &&
-test -f $p &&
-test $(cat $p) = tree1path1'
-
-test_expect_success \
-'checkout all stage 0 to temporary files' '
-rm -f path* .merge_* out .git/index &&
-git read-tree $t1 &&
-git checkout-index -a --temp >out &&
-test_line_count = 5 out &&
-for f in path0 path1 path3 path4 asubdir/path5
-do
-	test $(grep $f out | cut "-d	" -f2) = $f &&
-	p=$(grep $f out | cut "-d	" -f1) &&
+test_expect_success 'setup' '
+	mkdir asubdir &&
+	echo tree1path0 >path0 &&
+	echo tree1path1 >path1 &&
+	echo tree1path3 >path3 &&
+	echo tree1path4 >path4 &&
+	echo tree1asubdir/path5 >asubdir/path5 &&
+	git update-index --add path0 path1 path3 path4 asubdir/path5 &&
+	t1=$(git write-tree) &&
+	rm -f path* .merge_* actual .git/index &&
+	echo tree2path0 >path0 &&
+	echo tree2path1 >path1 &&
+	echo tree2path2 >path2 &&
+	echo tree2path4 >path4 &&
+	git update-index --add path0 path1 path2 path4 &&
+	t2=$(git write-tree) &&
+	rm -f path* .merge_* actual .git/index &&
+	echo tree2path0 >path0 &&
+	echo tree3path1 >path1 &&
+	echo tree3path2 >path2 &&
+	echo tree3path3 >path3 &&
+	git update-index --add path0 path1 path2 path3 &&
+	t3=$(git write-tree)
+'
+
+test_expect_success 'checkout one stage 0 to temporary file' '
+	rm -f path* .merge_* actual .git/index &&
+	git read-tree $t1 &&
+	git checkout-index --temp -- path1 >actual &&
+	test_line_count = 1 actual &&
+	test $(cut "-d	" -f2 actual) = path1 &&
+	p=$(cut "-d	" -f1 actual) &&
 	test -f $p &&
-	test $(cat $p) = tree1$f
-done'
-
-test_expect_success \
-'prepare 3-way merge' '
-rm -f path* .merge_* out .git/index &&
-git read-tree -m $t1 $t2 $t3'
-
-test_expect_success \
-'checkout one stage 2 to temporary file' '
-rm -f path* .merge_* out &&
-git checkout-index --stage=2 --temp -- path1 >out &&
-test_line_count = 1 out &&
-test $(cut "-d	" -f2 out) = path1 &&
-p=$(cut "-d	" -f1 out) &&
-test -f $p &&
-test $(cat $p) = tree2path1'
-
-test_expect_success \
-'checkout all stage 2 to temporary files' '
-rm -f path* .merge_* out &&
-git checkout-index --all --stage=2 --temp >out &&
-test_line_count = 3 out &&
-for f in path1 path2 path4
-do
-	test $(grep $f out | cut "-d	" -f2) = $f &&
-	p=$(grep $f out | cut "-d	" -f1) &&
+	test $(cat $p) = tree1path1
+'
+
+test_expect_success 'checkout all stage 0 to temporary files' '
+	rm -f path* .merge_* actual .git/index &&
+	git read-tree $t1 &&
+	git checkout-index -a --temp >actual &&
+	test_line_count = 5 actual &&
+	for f in path0 path1 path3 path4 asubdir/path5
+	do
+		test $(grep $f actual | cut "-d	" -f2) = $f &&
+		p=$(grep $f actual | cut "-d	" -f1) &&
+		test -f $p &&
+		test $(cat $p) = tree1$f
+	done
+'
+
+test_expect_success 'setup 3-way merge' '
+	rm -f path* .merge_* actual .git/index &&
+	git read-tree -m $t1 $t2 $t3
+'
+
+test_expect_success 'checkout one stage 2 to temporary file' '
+	rm -f path* .merge_* actual &&
+	git checkout-index --stage=2 --temp -- path1 >actual &&
+	test_line_count = 1 actual &&
+	test $(cut "-d	" -f2 actual) = path1 &&
+	p=$(cut "-d	" -f1 actual) &&
 	test -f $p &&
-	test $(cat $p) = tree2$f
-done'
-
-test_expect_success \
-'checkout all stages/one file to nothing' '
-rm -f path* .merge_* out &&
-git checkout-index --stage=all --temp -- path0 >out &&
-test_line_count = 0 out'
-
-test_expect_success \
-'checkout all stages/one file to temporary files' '
-rm -f path* .merge_* out &&
-git checkout-index --stage=all --temp -- path1 >out &&
-test_line_count = 1 out &&
-test $(cut "-d	" -f2 out) = path1 &&
-cut "-d	" -f1 out | (read s1 s2 s3 &&
-test -f $s1 &&
-test -f $s2 &&
-test -f $s3 &&
-test $(cat $s1) = tree1path1 &&
-test $(cat $s2) = tree2path1 &&
-test $(cat $s3) = tree3path1)'
-
-test_expect_success \
-'checkout some stages/one file to temporary files' '
-rm -f path* .merge_* out &&
-git checkout-index --stage=all --temp -- path2 >out &&
-test_line_count = 1 out &&
-test $(cut "-d	" -f2 out) = path2 &&
-cut "-d	" -f1 out | (read s1 s2 s3 &&
-test $s1 = . &&
-test -f $s2 &&
-test -f $s3 &&
-test $(cat $s2) = tree2path2 &&
-test $(cat $s3) = tree3path2)'
-
-test_expect_success \
-'checkout all stages/all files to temporary files' '
-rm -f path* .merge_* out &&
-git checkout-index -a --stage=all --temp >out &&
-test_line_count = 5 out'
-
-test_expect_success \
-'-- path0: no entry' '
-test x$(grep path0 out | cut "-d	" -f2) = x'
-
-test_expect_success \
-'-- path1: all 3 stages' '
-test $(grep path1 out | cut "-d	" -f2) = path1 &&
-grep path1 out | cut "-d	" -f1 | (read s1 s2 s3 &&
-test -f $s1 &&
-test -f $s2 &&
-test -f $s3 &&
-test $(cat $s1) = tree1path1 &&
-test $(cat $s2) = tree2path1 &&
-test $(cat $s3) = tree3path1)'
-
-test_expect_success \
-'-- path2: no stage 1, have stage 2 and 3' '
-test $(grep path2 out | cut "-d	" -f2) = path2 &&
-grep path2 out | cut "-d	" -f1 | (read s1 s2 s3 &&
-test $s1 = . &&
-test -f $s2 &&
-test -f $s3 &&
-test $(cat $s2) = tree2path2 &&
-test $(cat $s3) = tree3path2)'
-
-test_expect_success \
-'-- path3: no stage 2, have stage 1 and 3' '
-test $(grep path3 out | cut "-d	" -f2) = path3 &&
-grep path3 out | cut "-d	" -f1 | (read s1 s2 s3 &&
-test -f $s1 &&
-test $s2 = . &&
-test -f $s3 &&
-test $(cat $s1) = tree1path3 &&
-test $(cat $s3) = tree3path3)'
-
-test_expect_success \
-'-- path4: no stage 3, have stage 1 and 3' '
-test $(grep path4 out | cut "-d	" -f2) = path4 &&
-grep path4 out | cut "-d	" -f1 | (read s1 s2 s3 &&
-test -f $s1 &&
-test -f $s2 &&
-test $s3 = . &&
-test $(cat $s1) = tree1path4 &&
-test $(cat $s2) = tree2path4)'
-
-test_expect_success \
-'-- asubdir/path5: no stage 2 and 3 have stage 1' '
-test $(grep asubdir/path5 out | cut "-d	" -f2) = asubdir/path5 &&
-grep asubdir/path5 out | cut "-d	" -f1 | (read s1 s2 s3 &&
-test -f $s1 &&
-test $s2 = . &&
-test $s3 = . &&
-test $(cat $s1) = tree1asubdir/path5)'
-
-test_expect_success \
-'checkout --temp within subdir' '
-(cd asubdir &&
- git checkout-index -a --stage=all >out &&
- test_line_count = 1 out &&
- test $(grep path5 out | cut "-d	" -f2) = path5 &&
- grep path5 out | cut "-d	" -f1 | (read s1 s2 s3 &&
- test -f ../$s1 &&
- test $s2 = . &&
- test $s3 = . &&
- test $(cat ../$s1) = tree1asubdir/path5)
-)'
-
-test_expect_success \
-'checkout --temp symlink' '
-rm -f path* .merge_* out .git/index &&
-test_ln_s_add b a &&
-t4=$(git write-tree) &&
-rm -f .git/index &&
-git read-tree $t4 &&
-git checkout-index --temp -a >out &&
-test_line_count = 1 out &&
-test $(cut "-d	" -f2 out) = a &&
-p=$(cut "-d	" -f1 out) &&
-test -f $p &&
-test $(cat $p) = b'
+	test $(cat $p) = tree2path1
+'
+
+test_expect_success 'checkout all stage 2 to temporary files' '
+	rm -f path* .merge_* actual &&
+	git checkout-index --all --stage=2 --temp >actual &&
+	test_line_count = 3 actual &&
+	for f in path1 path2 path4
+	do
+		test $(grep $f actual | cut "-d	" -f2) = $f &&
+		p=$(grep $f actual | cut "-d	" -f1) &&
+		test -f $p &&
+		test $(cat $p) = tree2$f
+	done
+'
+
+test_expect_success 'checkout all stages/one file to nothing' '
+	rm -f path* .merge_* actual &&
+	git checkout-index --stage=all --temp -- path0 >actual &&
+	test_line_count = 0 actual
+'
+
+test_expect_success 'checkout all stages/one file to temporary files' '
+	rm -f path* .merge_* actual &&
+	git checkout-index --stage=all --temp -- path1 >actual &&
+	test_line_count = 1 actual &&
+	test $(cut "-d	" -f2 actual) = path1 &&
+	cut "-d	" -f1 actual | (read s1 s2 s3 &&
+	test -f $s1 &&
+	test -f $s2 &&
+	test -f $s3 &&
+	test $(cat $s1) = tree1path1 &&
+	test $(cat $s2) = tree2path1 &&
+	test $(cat $s3) = tree3path1)
+'
+
+test_expect_success 'checkout some stages/one file to temporary files' '
+	rm -f path* .merge_* actual &&
+	git checkout-index --stage=all --temp -- path2 >actual &&
+	test_line_count = 1 actual &&
+	test $(cut "-d	" -f2 actual) = path2 &&
+	cut "-d	" -f1 actual | (read s1 s2 s3 &&
+	test $s1 = . &&
+	test -f $s2 &&
+	test -f $s3 &&
+	test $(cat $s2) = tree2path2 &&
+	test $(cat $s3) = tree3path2)
+'
+
+test_expect_success 'checkout all stages/all files to temporary files' '
+	rm -f path* .merge_* actual &&
+	git checkout-index -a --stage=all --temp >actual &&
+	test_line_count = 5 actual
+'
+
+test_expect_success '-- path0: no entry' '
+	test x$(grep path0 actual | cut "-d	" -f2) = x
+'
+
+test_expect_success '-- path1: all 3 stages' '
+	test $(grep path1 actual | cut "-d	" -f2) = path1 &&
+	grep path1 actual | cut "-d	" -f1 | (read s1 s2 s3 &&
+	test -f $s1 &&
+	test -f $s2 &&
+	test -f $s3 &&
+	test $(cat $s1) = tree1path1 &&
+	test $(cat $s2) = tree2path1 &&
+	test $(cat $s3) = tree3path1)
+'
+
+test_expect_success '-- path2: no stage 1, have stage 2 and 3' '
+	test $(grep path2 actual | cut "-d	" -f2) = path2 &&
+	grep path2 actual | cut "-d	" -f1 | (read s1 s2 s3 &&
+	test $s1 = . &&
+	test -f $s2 &&
+	test -f $s3 &&
+	test $(cat $s2) = tree2path2 &&
+	test $(cat $s3) = tree3path2)
+'
+
+test_expect_success '-- path3: no stage 2, have stage 1 and 3' '
+	test $(grep path3 actual | cut "-d	" -f2) = path3 &&
+	grep path3 actual | cut "-d	" -f1 | (read s1 s2 s3 &&
+	test -f $s1 &&
+	test $s2 = . &&
+	test -f $s3 &&
+	test $(cat $s1) = tree1path3 &&
+	test $(cat $s3) = tree3path3)
+'
+
+test_expect_success '-- path4: no stage 3, have stage 1 and 3' '
+	test $(grep path4 actual | cut "-d	" -f2) = path4 &&
+	grep path4 actual | cut "-d	" -f1 | (read s1 s2 s3 &&
+	test -f $s1 &&
+	test -f $s2 &&
+	test $s3 = . &&
+	test $(cat $s1) = tree1path4 &&
+	test $(cat $s2) = tree2path4)
+'
+
+test_expect_success '-- asubdir/path5: no stage 2 and 3 have stage 1' '
+	test $(grep asubdir/path5 actual | cut "-d	" -f2) = asubdir/path5 &&
+	grep asubdir/path5 actual | cut "-d	" -f1 | (read s1 s2 s3 &&
+	test -f $s1 &&
+	test $s2 = . &&
+	test $s3 = . &&
+	test $(cat $s1) = tree1asubdir/path5)
+'
+
+test_expect_success 'checkout --temp within subdir' '
+	(
+		cd asubdir &&
+		git checkout-index -a --stage=all >actual &&
+		test_line_count = 1 actual &&
+		test $(grep path5 actual | cut "-d	" -f2) = path5 &&
+		grep path5 actual | cut "-d	" -f1 | (read s1 s2 s3 &&
+		test -f ../$s1 &&
+		test $s2 = . &&
+		test $s3 = . &&
+		test $(cat ../$s1) = tree1asubdir/path5)
+	)
+'
+
+test_expect_success 'checkout --temp symlink' '
+	rm -f path* .merge_* actual .git/index &&
+	test_ln_s_add b a &&
+	t4=$(git write-tree) &&
+	rm -f .git/index &&
+	git read-tree $t4 &&
+	git checkout-index --temp -a >actual &&
+	test_line_count = 1 actual &&
+	test $(cut "-d	" -f2 actual) = a &&
+	p=$(cut "-d	" -f1 actual) &&
+	test -f $p &&
+	test $(cat $p) = b
+'
 
 test_done
-- 
2.2.1.267.g0ad48de

```

## Eric Sunshine, 2014-12-24 09:43

Subject: [PATCH 2/5] t2004: drop unnecessary write-tree/read-tree
Message-ID: <1419414196-58587-3-git-send-email-sunshine@sunshineco.com>
URL: https://gitlist.dev/e/1419414196-58587-3-git-send-email-sunshine%40sunshineco.com
In-Reply-To: <1419414196-58587-1-git-send-email-sunshine@sunshineco.com>

```
Unlike earlier tests which reference several trees prepared by "setup",
no other tests utilize the tree from the "symlink" test, so there is no
need to write it (or read it back immediately).

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 t/t2004-checkout-cache-temp.sh | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/t/t2004-checkout-cache-temp.sh b/t/t2004-checkout-cache-temp.sh
index 28e50d6..22d1a24 100755
--- a/t/t2004-checkout-cache-temp.sh
+++ b/t/t2004-checkout-cache-temp.sh
@@ -198,9 +198,6 @@ test_expect_success 'checkout --temp within subdir' '
 test_expect_success 'checkout --temp symlink' '
 	rm -f path* .merge_* actual .git/index &&
 	test_ln_s_add b a &&
-	t4=$(git write-tree) &&
-	rm -f .git/index &&
-	git read-tree $t4 &&
 	git checkout-index --temp -a >actual &&
 	test_line_count = 1 actual &&
 	test $(cut "-d	" -f2 actual) = a &&
-- 
2.2.1.267.g0ad48de

```

## Eric Sunshine, 2014-12-24 09:43

Subject: [PATCH 3/5] t2004: standardize file naming in symlink test
Message-ID: <1419414196-58587-4-git-send-email-sunshine@sunshineco.com>
URL: https://gitlist.dev/e/1419414196-58587-4-git-send-email-sunshine%40sunshineco.com
In-Reply-To: <1419414196-58587-1-git-send-email-sunshine@sunshineco.com>

```
Update "symlink" test to use the common file naming scheme so that its
temporary files can be cleaned up by the "rm -f path*" idiom employed by
other tests in this script.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 t/t2004-checkout-cache-temp.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/t/t2004-checkout-cache-temp.sh b/t/t2004-checkout-cache-temp.sh
index 22d1a24..602fbc7 100755
--- a/t/t2004-checkout-cache-temp.sh
+++ b/t/t2004-checkout-cache-temp.sh
@@ -197,13 +197,13 @@ test_expect_success 'checkout --temp within subdir' '
 
 test_expect_success 'checkout --temp symlink' '
 	rm -f path* .merge_* actual .git/index &&
-	test_ln_s_add b a &&
+	test_ln_s_add path7 path6 &&
 	git checkout-index --temp -a >actual &&
 	test_line_count = 1 actual &&
-	test $(cut "-d	" -f2 actual) = a &&
+	test $(cut "-d	" -f2 actual) = path6 &&
 	p=$(cut "-d	" -f1 actual) &&
 	test -f $p &&
-	test $(cat $p) = b
+	test $(cat $p) = path7
 '
 
 test_done
-- 
2.2.1.267.g0ad48de

```

## Eric Sunshine, 2014-12-24 09:43

Subject: [PATCH 4/5] t2004: demonstrate broken relative path printing
Message-ID: <1419414196-58587-5-git-send-email-sunshine@sunshineco.com>
URL: https://gitlist.dev/e/1419414196-58587-5-git-send-email-sunshine%40sunshineco.com
In-Reply-To: <1419414196-58587-1-git-send-email-sunshine@sunshineco.com>

```
checkout-index --temp only properly prints relative paths which are
descendants of the current directory. Paths in ancestor or sibling
directories (or their children) are often printed in mangled form. For
example:

    mkdir a bbb &&
    >file &&
    >bbb/file &&
    git update-index --add file bbb/file &&
    cd a &&
    git checkout-index --temp ../file ../bbb/file

prints:

    .merge_file_ooblek  le
    .merge_file_igloo0  b/file

rather than the correct:

    .merge_file_ooblek  ../file
    .merge_file_igloo0  ../bbb/file

Unfortunately, testing is complicated slightly by relative paths
sometimes _appearing_ to be printed correctly, but this is an accident
of implementation in which a "correct" copy of the string exists in
memory beyond the end of the real string, and that "correct" copy gets
printed. This test takes care to avoid the accidentally "correct"
behavior by testing with a filename longer than the directory name in
which checkout-index is invoked.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 t/t2004-checkout-cache-temp.sh | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/t/t2004-checkout-cache-temp.sh b/t/t2004-checkout-cache-temp.sh
index 602fbc7..3c2b2b9 100755
--- a/t/t2004-checkout-cache-temp.sh
+++ b/t/t2004-checkout-cache-temp.sh
@@ -206,4 +206,16 @@ test_expect_success 'checkout --temp symlink' '
 	test $(cat $p) = path7
 '
 
+test_expect_failure 'emit well-formed relative path' '
+	rm -f path* .merge_* actual .git/index &&
+	>path0123456789 &&
+	git update-index --add path0123456789 &&
+	(
+		cd asubdir &&
+		git checkout-index --temp -- ../path0123456789 >actual &&
+		test_line_count = 1 actual &&
+		test $(cut "-d	" -f2 actual) = ../path0123456789
+	)
+'
+
 test_done
-- 
2.2.1.267.g0ad48de

```

## Eric Sunshine, 2014-12-24 09:43

Subject: [PATCH 5/5] checkout-index: fix --temp relative path mangling
Message-ID: <1419414196-58587-6-git-send-email-sunshine@sunshineco.com>
URL: https://gitlist.dev/e/1419414196-58587-6-git-send-email-sunshine%40sunshineco.com
In-Reply-To: <1419414196-58587-1-git-send-email-sunshine@sunshineco.com>

```
checkout-index --temp only properly prints relative paths which are
descendants of the current directory. Paths in ancestor or sibling
directories (or their children) are often printed in mangled form. For
example:

    mkdir a bbb &&
    >file &&
    >bbb/file &&
    git update-index --add file bbb/file &&
    cd a &&
    git checkout-index --temp ../file ../bbb/file

prints:

    .merge_file_ooblek  le
    .merge_file_igloo0  b/file

rather than the correct:

    .merge_file_ooblek  ../file
    .merge_file_igloo0  ../bbb/file

Internally, given the above example, checkout-index prefixes each input
argument with the name of the current directory ("a/", in this case),
and then assumes that it can simply skip forward by strlen("a/") bytes
to recover the original name. This works for files in the current
directory or its descendants, but fails for files in ancestors or
siblings (or their children) due to path normalization.

For instance, given "../file", "a/" is appended, giving "a/../file".
Path normalization folds out "a/../", resulting in "file". Attempting
to recover the original name by skipping strlen("a/") bytes gives the
incorrect "le" rather than the desired "../file".

Fix this by taking advantage of write_name_quoted_relative() to recover
the original name properly, rather than assuming that it can be
recovered by skipping strlen(prefix) bytes.

As a bonus, this also fixes a bug in which checkout-index --temp
accessed and printed memory beyond the end-of-string. For instance,
within a subdirectory named "subdirectory", and given argument
"../file", prefixing would give "subdirectory/../file", which would
become "file" after normalization. checkout-index would then attempt to
recover the original name by skipping strlen("subdirectory/") bytes of
"file", which placed it well beyond end-of-string. Despite this error,
it often appeared to give the correct result, but only due to an
accident of implementation which left an apparently correct copy of the
path in memory following the normalized value. In particular, handed
"subdirectory/../file", in-place processing by normalize_path_copy_len()
resulted in "file\0rectory/../file". When checkout-index skipped
strlen("subdirectory/") bytes, it ended up back at "../file" and thus
appeared to give the correct answer, despite being past end-of-string.

Reported-by: Russ Cox <rsc@golang.org>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 builtin/checkout-index.c       | 16 ++++++++--------
 t/t2004-checkout-cache-temp.sh |  2 +-
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index 383dccf..031780f 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -18,7 +18,7 @@ static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
 
 static struct checkout state;
 
-static void write_tempfile_record(const char *name, int prefix_length)
+static void write_tempfile_record(const char *name, const char *prefix)
 {
 	int i;
 
@@ -35,14 +35,14 @@ static void write_tempfile_record(const char *name, int prefix_length)
 		fputs(topath[checkout_stage], stdout);
 
 	putchar('\t');
-	write_name_quoted(name + prefix_length, stdout, line_termination);
+	write_name_quoted_relative(name, prefix, stdout, line_termination);
 
 	for (i = 0; i < 4; i++) {
 		topath[i][0] = 0;
 	}
 }
 
-static int checkout_file(const char *name, int prefix_length)
+static int checkout_file(const char *name, const char *prefix)
 {
 	int namelen = strlen(name);
 	int pos = cache_name_pos(name, namelen);
@@ -71,7 +71,7 @@ static int checkout_file(const char *name, int prefix_length)
 
 	if (did_checkout) {
 		if (to_tempfile)
-			write_tempfile_record(name, prefix_length);
+			write_tempfile_record(name, prefix);
 		return errs > 0 ? -1 : 0;
 	}
 
@@ -106,7 +106,7 @@ static void checkout_all(const char *prefix, int prefix_length)
 		if (last_ce && to_tempfile) {
 			if (ce_namelen(last_ce) != ce_namelen(ce)
 			    || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
-				write_tempfile_record(last_ce->name, prefix_length);
+				write_tempfile_record(last_ce->name, prefix);
 		}
 		if (checkout_entry(ce, &state,
 		    to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
@@ -114,7 +114,7 @@ static void checkout_all(const char *prefix, int prefix_length)
 		last_ce = ce;
 	}
 	if (last_ce && to_tempfile)
-		write_tempfile_record(last_ce->name, prefix_length);
+		write_tempfile_record(last_ce->name, prefix);
 	if (errs)
 		/* we have already done our error reporting.
 		 * exit with the same code as die().
@@ -248,7 +248,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 		if (read_from_stdin)
 			die("git checkout-index: don't mix '--stdin' and explicit filenames");
 		p = prefix_path(prefix, prefix_length, arg);
-		checkout_file(p, prefix_length);
+		checkout_file(p, prefix);
 		if (p < arg || p > arg + strlen(arg))
 			free((char *)p);
 	}
@@ -268,7 +268,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 				strbuf_swap(&buf, &nbuf);
 			}
 			p = prefix_path(prefix, prefix_length, buf.buf);
-			checkout_file(p, prefix_length);
+			checkout_file(p, prefix);
 			if (p < buf.buf || p > buf.buf + buf.len)
 				free((char *)p);
 		}
diff --git a/t/t2004-checkout-cache-temp.sh b/t/t2004-checkout-cache-temp.sh
index 3c2b2b9..a12afe9 100755
--- a/t/t2004-checkout-cache-temp.sh
+++ b/t/t2004-checkout-cache-temp.sh
@@ -206,7 +206,7 @@ test_expect_success 'checkout --temp symlink' '
 	test $(cat $p) = path7
 '
 
-test_expect_failure 'emit well-formed relative path' '
+test_expect_success 'emit well-formed relative path' '
 	rm -f path* .merge_* actual .git/index &&
 	>path0123456789 &&
 	git update-index --add path0123456789 &&
-- 
2.2.1.267.g0ad48de

```

## Eric Sunshine, 2014-12-28 03:19

Subject: Re: [PATCH 5/5] checkout-index: fix --temp relative path mangling
Message-ID: <CAPig+cTCQgdX0FgZCiL921bAycEoceeKXT5UpnD7_Ro=NJH0Yg@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcTCQgdX0FgZCiL921bAycEoceeKXT5UpnD7_Ro%3DNJH0Yg%40mail.gmail.com
In-Reply-To: <1419414196-58587-6-git-send-email-sunshine@sunshineco.com>

```
On Wed, Dec 24, 2014 at 4:43 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> checkout-index --temp only properly prints relative paths which are
> descendants of the current directory. Paths in ancestor or sibling
> directories (or their children) are often printed in mangled form. For
> example:
>
>     mkdir a bbb &&
>     >file &&
>     >bbb/file &&
>     git update-index --add file bbb/file &&
>     cd a &&
>     git checkout-index --temp ../file ../bbb/file
>
> prints:
>
>     .merge_file_ooblek  le
>     .merge_file_igloo0  b/file
>
> rather than the correct:
>
>     .merge_file_ooblek  ../file
>     .merge_file_igloo0  ../bbb/file
>
> Internally, given the above example, checkout-index prefixes each input
> argument with the name of the current directory ("a/", in this case),
> and then assumes that it can simply skip forward by strlen("a/") bytes
> to recover the original name. This works for files in the current
> directory or its descendants, but fails for files in ancestors or
> siblings (or their children) due to path normalization.
>
> For instance, given "../file", "a/" is appended, giving "a/../file".

s/appended/prepended/

> Path normalization folds out "a/../", resulting in "file". Attempting
> to recover the original name by skipping strlen("a/") bytes gives the
> incorrect "le" rather than the desired "../file".
>
> Fix this by taking advantage of write_name_quoted_relative() to recover
> the original name properly, rather than assuming that it can be
> recovered by skipping strlen(prefix) bytes.
>
> As a bonus, this also fixes a bug in which checkout-index --temp
> accessed and printed memory beyond the end-of-string. For instance,
> within a subdirectory named "subdirectory", and given argument
> "../file", prefixing would give "subdirectory/../file", which would
> become "file" after normalization. checkout-index would then attempt to
> recover the original name by skipping strlen("subdirectory/") bytes of
> "file", which placed it well beyond end-of-string. Despite this error,
> it often appeared to give the correct result, but only due to an
> accident of implementation which left an apparently correct copy of the
> path in memory following the normalized value. In particular, handed
> "subdirectory/../file", in-place processing by normalize_path_copy_len()
> resulted in "file\0rectory/../file". When checkout-index skipped
> strlen("subdirectory/") bytes, it ended up back at "../file" and thus
> appeared to give the correct answer, despite being past end-of-string.
>
> Reported-by: Russ Cox <rsc@golang.org>
> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>

```

## Junio C Hamano, 2014-12-29 18:58

Subject: Re: [PATCH 5/5] checkout-index: fix --temp relative path mangling
Message-ID: <xmqqlhlqz3j1.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqlhlqz3j1.fsf%40gitster.dls.corp.google.com
In-Reply-To: <1419414196-58587-6-git-send-email-sunshine@sunshineco.com>

```
Eric Sunshine <sunshine@sunshineco.com> writes:

> Fix this by taking advantage of write_name_quoted_relative() to recover
> the original name properly, rather than assuming that it can be
> recovered by skipping strlen(prefix) bytes.

Nice; I was wondering if we already had that helper when I saw the
initial report of the problem.

Thanks for a nicely constructed series.  Will queue.

```
