Skip to content

Commit f4c21e8

Browse files
mhaggergitster
authored andcommitted
real_path(): properly handle nonexistent top-level paths
The change has two points: 1. Do not strip off a leading slash, because that erroneously turns an absolute path into a relative path. 2. Do not remove slashes from groups of multiple slashes; instead let chdir() handle them. It could be, for example, that it wants to leave leading double-slashes alone. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7bcf48d commit f4c21e8

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

abspath.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ const char *real_path(const char *path)
4545
if (!is_directory(buf)) {
4646
char *last_slash = find_last_dir_sep(buf);
4747
if (last_slash) {
48-
*last_slash = '\0';
4948
last_elem = xstrdup(last_slash + 1);
49+
last_slash[1] = '\0';
5050
} else {
5151
last_elem = xstrdup(buf);
5252
*buf = '\0';

t/t0060-path-utils.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ test_expect_success 'real path rejects the empty string' '
148148
test_must_fail test-path-utils real_path ""
149149
'
150150

151-
test_expect_failure POSIX 'real path works on absolute paths' '
151+
test_expect_success POSIX 'real path works on absolute paths' '
152152
nopath="hopefully-absent-path" &&
153153
test "/" = "$(test-path-utils real_path "/")" &&
154154
test "/$nopath" = "$(test-path-utils real_path "/$nopath")" &&

0 commit comments

Comments
 (0)