Skip to content

Commit fbbb4e1

Browse files
pcloudsgitster
authored andcommitted
get_cwd_relative(): do not misinterpret root path
Commit 490544b (get_cwd_relative(): do not misinterpret suffix as subdirectory) handles case where: dir = "/path/work"; cwd = "/path/work-xyz"; When it comes to the end of get_cwd_relative(), dir is at '\0' and cwd is at '-'. The rest of cwd, "-xyz", clearly cannot be the relative path from dir to cwd. However there is another case where: dir = "/"; /* or even "c:/" */ cwd = "/path/to/here"; In this special case, while *cwd == 'p', which is not a path separator, the rest of cwd, "path/to/here", can be returned as a relative path from dir to cwd. Handle this case and make t1509 pass again. Reported-by: Albert Strasheim <[email protected]> Reported-by: Matthijs Kooijman <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 520ea85 commit fbbb4e1

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

dir.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,12 @@ char *get_relative_cwd(char *buffer, int size, const char *dir)
964964
case '/':
965965
return cwd + 1;
966966
default:
967+
/*
968+
* dir can end with a path separator when it's root
969+
* directory. Return proper prefix in that case.
970+
*/
971+
if (dir[-1] == '/')
972+
return cwd;
967973
return NULL;
968974
}
969975
}

0 commit comments

Comments
 (0)