From 806d13b1ccdbdde4bbdfb96902791c4b7ed125f6 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 4 Aug 2008 00:51:42 -0700 Subject: update-index: refuse to add working tree items beyond symlinks When "sym" is a symbolic link that is inside the working tree, and it points at a directory "dir" that has "path" in it, "update-index --add sym/path" used to mistakenly add "sym/path" as if "sym" were a normal directory. "git apply", "git diff" and "git merge" have been taught about this issue some time ago, but "update-index" and "add" have been left ignorant for too long. Signed-off-by: Junio C Hamano diff --git a/builtin-update-index.c b/builtin-update-index.c index 38eb53c..434cb8e 100644 --- a/builtin-update-index.c +++ b/builtin-update-index.c @@ -194,6 +194,10 @@ static int process_path(const char *path) int len; struct stat st; + len = strlen(path); + if (has_symlink_leading_path(len, path)) + return error("'%s' is beyond a symbolic link", path); + /* * First things first: get the stat information, to decide * what to do about the pathname! @@ -201,7 +205,6 @@ static int process_path(const char *path) if (lstat(path, &st) < 0) return process_lstat_error(path, errno); - len = strlen(path); if (S_ISDIR(st.st_mode)) return process_directory(path, len, &st); diff --git a/t/t0055-beyond-symlinks.sh b/t/t0055-beyond-symlinks.sh new file mode 100755 index 0000000..eb11dd7 --- /dev/null +++ b/t/t0055-beyond-symlinks.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +test_description='update-index refuses to add beyond symlinks' + +. ./test-lib.sh + +test_expect_success setup ' + >a && + mkdir b && + ln -s b c && + >c/d && + git update-index --add a b/d +' + +test_expect_success 'update-index --add beyond symlinks' ' + test_must_fail git update-index --add c/d && + ! ( git ls-files | grep c/d ) +' + +test_done -- cgit v0.10.2-6-g49f6 From 725b06050a083474e240a2436121e0a80bb9f175 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 4 Aug 2008 00:52:37 -0700 Subject: add: refuse to add working tree items beyond symlinks This is the same fix for the issue of adding "sym/path" when "sym" is a symblic link that points at a directory "dir" with "path" in it. Signed-off-by: Junio C Hamano diff --git a/builtin-add.c b/builtin-add.c index fc3f96e..81b64d7 100644 --- a/builtin-add.c +++ b/builtin-add.c @@ -153,6 +153,16 @@ static const char **validate_pathspec(int argc, const char **argv, const char *p { const char **pathspec = get_pathspec(prefix, argv); + if (pathspec) { + const char **p; + for (p = pathspec; *p; p++) { + if (has_symlink_leading_path(strlen(*p), *p)) { + int len = prefix ? strlen(prefix) : 0; + die("'%s' is beyond a symbolic link", *p + len); + } + } + } + return pathspec; } @@ -278,7 +288,7 @@ int cmd_add(int argc, const char **argv, const char *prefix) fprintf(stderr, "Maybe you wanted to say 'git add .'?\n"); return 0; } - pathspec = get_pathspec(prefix, argv); + pathspec = validate_pathspec(argc, argv, prefix); /* * If we are adding new files, we need to scan the working diff --git a/dir.c b/dir.c index 29d1d5b..ae7046f 100644 --- a/dir.c +++ b/dir.c @@ -727,8 +727,12 @@ static void free_simplify(struct path_simplify *simplify) int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec) { - struct path_simplify *simplify = create_simplify(pathspec); + struct path_simplify *simplify; + if (has_symlink_leading_path(strlen(path), path)) + return dir->nr; + + simplify = create_simplify(pathspec); read_directory_recursive(dir, path, base, baselen, 0, simplify); free_simplify(simplify); qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name); diff --git a/t/t0055-beyond-symlinks.sh b/t/t0055-beyond-symlinks.sh index eb11dd7..b29c37a 100755 --- a/t/t0055-beyond-symlinks.sh +++ b/t/t0055-beyond-symlinks.sh @@ -1,6 +1,6 @@ #!/bin/sh -test_description='update-index refuses to add beyond symlinks' +test_description='update-index and add refuse to add beyond symlinks' . ./test-lib.sh @@ -17,4 +17,9 @@ test_expect_success 'update-index --add beyond symlinks' ' ! ( git ls-files | grep c/d ) ' +test_expect_success 'add beyond symlinks' ' + test_must_fail git add c/d && + ! ( git ls-files | grep c/d ) +' + test_done -- cgit v0.10.2-6-g49f6