Skip to content

Commit 22ce37d

Browse files
committed
Merge branch 'mh/abspath'
* mh/abspath: t0060: split absolute path test in two to exercise some of it on Windows t0060: verify that real_path() removes extra slashes real_path(): properly handle nonexistent top-level paths t0060: verify that real_path() works correctly with absolute paths real_path(): reject the empty string t0060: verify that real_path() fails if passed the empty string absolute_path(): reject the empty string t0060: verify that absolute_path() fails if passed the empty string t0060: move tests of real_path() from t0000 to here
2 parents 0f80d89 + bacca78 commit 22ce37d

File tree

3 files changed

+66
-20
lines changed

3 files changed

+66
-20
lines changed

abspath.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ const char *real_path(const char *path)
3535
if (path == buf || path == next_buf)
3636
return path;
3737

38+
if (!*path)
39+
die("The empty string is not a valid path");
40+
3841
if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
3942
die ("Too long path: %.*s", 60, path);
4043

4144
while (depth--) {
4245
if (!is_directory(buf)) {
4346
char *last_slash = find_last_dir_sep(buf);
4447
if (last_slash) {
45-
*last_slash = '\0';
4648
last_elem = xstrdup(last_slash + 1);
49+
last_slash[1] = '\0';
4750
} else {
4851
last_elem = xstrdup(buf);
4952
*buf = '\0';
@@ -123,7 +126,9 @@ const char *absolute_path(const char *path)
123126
{
124127
static char buf[PATH_MAX + 1];
125128

126-
if (is_absolute_path(path)) {
129+
if (!*path) {
130+
die("The empty string is not a valid path");
131+
} else if (is_absolute_path(path)) {
127132
if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
128133
die("Too long path: %.*s", 60, path);
129134
} else {

t/t0000-basic.sh

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -450,24 +450,6 @@ test_expect_success 'update-index D/F conflict' '
450450
test $numpath0 = 1
451451
'
452452

453-
test_expect_success SYMLINKS 'real path works as expected' '
454-
mkdir first &&
455-
ln -s ../.git first/.git &&
456-
mkdir second &&
457-
ln -s ../first second/other &&
458-
mkdir third &&
459-
dir="$(cd .git; pwd -P)" &&
460-
dir2=third/../second/other/.git &&
461-
test "$dir" = "$(test-path-utils real_path $dir2)" &&
462-
file="$dir"/index &&
463-
test "$file" = "$(test-path-utils real_path $dir2/index)" &&
464-
basename=blub &&
465-
test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
466-
ln -s ../first/file .git/syml &&
467-
sym="$(cd first; pwd -P)"/file &&
468-
test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
469-
'
470-
471453
test_expect_success 'very long name in the index handled sanely' '
472454
473455
a=a && # 1

t/t0060-path-utils.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,63 @@ test_expect_success 'strip_path_suffix' '
139139
test c:/msysgit = $(test-path-utils strip_path_suffix \
140140
c:/msysgit/libexec//git-core libexec/git-core)
141141
'
142+
143+
test_expect_success 'absolute path rejects the empty string' '
144+
test_must_fail test-path-utils absolute_path ""
145+
'
146+
147+
test_expect_success 'real path rejects the empty string' '
148+
test_must_fail test-path-utils real_path ""
149+
'
150+
151+
test_expect_success POSIX 'real path works on absolute paths 1' '
152+
nopath="hopefully-absent-path" &&
153+
test "/" = "$(test-path-utils real_path "/")" &&
154+
test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
155+
'
156+
157+
test_expect_success 'real path works on absolute paths 2' '
158+
nopath="hopefully-absent-path" &&
159+
# Find an existing top-level directory for the remaining tests:
160+
d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
161+
test "$d" = "$(test-path-utils real_path "$d")" &&
162+
test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")"
163+
'
164+
165+
test_expect_success POSIX 'real path removes extra leading slashes' '
166+
nopath="hopefully-absent-path" &&
167+
test "/" = "$(test-path-utils real_path "///")" &&
168+
test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
169+
# Find an existing top-level directory for the remaining tests:
170+
d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
171+
test "$d" = "$(test-path-utils real_path "//$d")" &&
172+
test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
173+
'
174+
175+
test_expect_success 'real path removes other extra slashes' '
176+
nopath="hopefully-absent-path" &&
177+
# Find an existing top-level directory for the remaining tests:
178+
d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
179+
test "$d" = "$(test-path-utils real_path "$d///")" &&
180+
test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")"
181+
'
182+
183+
test_expect_success SYMLINKS 'real path works on symlinks' '
184+
mkdir first &&
185+
ln -s ../.git first/.git &&
186+
mkdir second &&
187+
ln -s ../first second/other &&
188+
mkdir third &&
189+
dir="$(cd .git; pwd -P)" &&
190+
dir2=third/../second/other/.git &&
191+
test "$dir" = "$(test-path-utils real_path $dir2)" &&
192+
file="$dir"/index &&
193+
test "$file" = "$(test-path-utils real_path $dir2/index)" &&
194+
basename=blub &&
195+
test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
196+
ln -s ../first/file .git/syml &&
197+
sym="$(cd first; pwd -P)"/file &&
198+
test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
199+
'
200+
142201
test_done

0 commit comments

Comments
 (0)