diff --git a/lib/util_path.c b/lib/util_path.c index e6e8c97b5..9d2bf05bf 100644 --- a/lib/util_path.c +++ b/lib/util_path.c @@ -148,8 +148,14 @@ xbps_path_rel(char *dst, size_t dstlen, const char *from, const char *to) if (*top == '/') suffix = top; - for (up = -1, fromp--; fromp && *fromp; fromp = strchr(fromp+1, '/'), up++) - ; + up = -1; + if (fromp > frombuf) { + fromp--; + for (; fromp && *fromp; fromp = strchr(fromp+1, '/'), up++) + ; + } else { + up = 0; + } while (up--) { for (const char *x = "../"; *x; x++) { diff --git a/tests/xbps/libxbps/util_path/main.c b/tests/xbps/libxbps/util_path/main.c index fbd14b9a6..97b57e923 100644 --- a/tests/xbps/libxbps/util_path/main.c +++ b/tests/xbps/libxbps/util_path/main.c @@ -133,6 +133,7 @@ ATF_TC_BODY(xbps_path_rel, tc) REL("/root/usr/bin//tar", "/root/usr/libexec/gtar", "../libexec/gtar"); REL("/root/usr/bin//tar", "/root/usr/libexec//gtar", "../libexec/gtar"); REL("/usr/bin/file", "/usr/bin/fileA", "fileA"); + REL("A", "B", ""); } ATF_TC(xbps_path_join);