Commit 0ab20ee9ead0bcdb626ef9fb52d63f58e13082a5

Stefan Sperling 2020-07-23T14:21:27

fix handling of symlinks to a nonexistent target

diff --git a/lib/worktree.c b/lib/worktree.c
index 1f4dfd3..03438c0 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1007,8 +1007,9 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
 	}
 
 	/* Only allow symlinks pointing at paths within the work tree. */
-	if (!got_path_is_child(resolved_path ? resolved_path : target_path,
-	        worktree->root_path, strlen(worktree->root_path))) {
+	if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
+	    abspath : target_path), worktree->root_path,
+	    strlen(worktree->root_path))) {
 		/* install as a regular file */
 		got_object_blob_rewind(blob);
 		err = install_blob(worktree, ondisk_path, path,
diff --git a/regress/cmdline/checkout.sh b/regress/cmdline/checkout.sh
index 8bb0b0d..6cde66b 100755
--- a/regress/cmdline/checkout.sh
+++ b/regress/cmdline/checkout.sh
@@ -509,8 +509,10 @@ function test_checkout_symlink {
 	(cd $testroot/repo && ln -s alpha alpha.link)
 	(cd $testroot/repo && ln -s epsilon epsilon.link)
 	(cd $testroot/repo && ln -s /etc/passwd passwd.link)
+	(cd $testroot/repo && ln -s ../beta epsilon/beta.link)
+	(cd $testroot/repo && ln -s nonexistent nonexistent.link)
 	(cd $testroot/repo && git add .)
-	git_commit $testroot/repo -m "add a symlink"
+	git_commit $testroot/repo -m "add symlinks"
 
 	got checkout $testroot/repo $testroot/wt > /dev/null
 	ret="$?"
@@ -565,9 +567,28 @@ function test_checkout_symlink {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/content.expected $testroot/content
+		test_done "$testroot" "$ret"
+		return 1
 	fi
-	test_done "$testroot" "$ret"
 
+	readlink $testroot/wt/epsilon/beta.link > $testroot/stdout
+	echo "../beta" > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	readlink $testroot/wt/nonexistent.link > $testroot/stdout
+	echo "nonexistent" > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
 }
 
 run_test test_checkout_basic