[PATCH 6/7] real_path(): properly handle nonexistent top-level paths
From: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
abspath.c | 5 ++++-
t/t0000-basic.sh | 2 +-
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/abspath.c b/abspath.c
index 3e8325c..0e1cd7f 100644
--- a/abspath.c
+++ b/abspath.c
@@ -45,8 +45,11 @@ const char *real_path(const char *path)
if (!is_directory(buf)) {
char *last_slash = find_last_dir_sep(buf);
if (last_slash) {
- *last_slash = '\0';
last_elem = xstrdup(last_slash + 1);
+ if (last_slash == buf)
+ last_slash[1] = '\0';
+ else
+ last_slash[0] = '\0';
} else {
last_elem = xstrdup(buf);
*buf = '\0';
diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh
index ad002ee..d929578 100755
--- a/t/t0000-basic.sh
+++ b/t/t0000-basic.sh
@@ -458,7 +458,7 @@ test_expect_success 'real path rejects the empty string' '
test_must_fail test-path-utils real_path ""
'
-test_expect_failure 'real path works on absolute paths' '
+test_expect_success 'real path works on absolute paths' '
nopath="hopefully-absent-path" &&
test "/" = "$(test-path-utils real_path "/")" &&
test "/$nopath" = "$(test-path-utils real_path "/$nopath")" &&
--
1.7.11.3