git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 7/8] real_path(): properly handle nonexistent top-level paths

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Sep 6, 2012, 22:41 UTC
Message-ID
<1346971264-23744-8-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1346971264-23744-1-git-send-email-mhagger@alum.mit.edu>
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 <mhagger@alum.mit.edu>
---
 abspath.c             | 2 +-
 t/t0060-path-utils.sh | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/abspath.c b/abspath.c
index 3e8325c..05f2d79 100644
--- a/abspath.c
+++ b/abspath.c
@@ -45,8 +45,8 @@ 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);
+				last_slash[1] = '\0';
 			} else {
 				last_elem = xstrdup(buf);
 				*buf = '\0';
diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh
index 3121691..30361f9 100755
--- a/t/t0060-path-utils.sh
+++ b/t/t0060-path-utils.sh
@@ -148,7 +148,7 @@ test_expect_success 'real path rejects the empty string' '
 	test_must_fail test-path-utils real_path ""
 '
 
-test_expect_failure POSIX 'real path works on absolute paths' '
+test_expect_success POSIX '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
Previous: Michael HaggertyNext: Michael Haggerty
Message 11 of 14 in “Fix some bugs in abspath.c”
  1. 0/8 Fix some bugs in abspath.cMichael Haggerty, Sep 6, 2012
  2. 1/8 t0060: move tests of real_path() from t0000 to hereMichael Haggerty, Sep 6, 2012
  3. 2/8 t0060: verify that absolute_path() fails if passed the empty stringMichael Haggerty, Sep 6, 2012
  4. 3/8 absolute_path(): reject the empty stringMichael Haggerty, Sep 6, 2012
  5. Junio C HamanoSep 6, 2012
  6. Michael HaggertySep 9, 2012
  7. Junio C HamanoSep 9, 2012
  8. 4/8 t0060: verify that real_path() fails if passed the empty stringMichael Haggerty, Sep 6, 2012
  9. 5/8 real_path(): reject the empty stringMichael Haggerty, Sep 6, 2012
  10. 6/8 t0060: verify that real_path() works correctly with absolute pathsMichael Haggerty, Sep 6, 2012
  11. 7/8 real_path(): properly handle nonexistent top-level pathsMichael Haggerty, Sep 6, 2012
  12. 8/8 t0060: verify that real_path() removes extra slashesMichael Haggerty, Sep 6, 2012
  13. 9/8 t0060: split absolute path test in two to exercise some of it on WindowsJohannes Sixt, Sep 9, 2012
  14. Michael HaggertySep 10, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.