diff --git a/src/pkg/path/filepath/path.go b/src/pkg/path/filepath/path.go index 1b5d6c36495..3656227ff06 100644 --- a/src/pkg/path/filepath/path.go +++ b/src/pkg/path/filepath/path.go @@ -312,7 +312,11 @@ func Rel(basepath, targpath string) (string, error) { if b0 != bl { // Base elements left. Must go up before going down. seps := strings.Count(base[b0:bl], string(Separator)) - buf := make([]byte, 3+seps*3+tl-t0) + size := 2 + seps*3 + if tl != t0 { + size += 1 + tl - t0 + } + buf := make([]byte, size) n := copy(buf, "..") for i := 0; i < seps; i++ { buf[n] = Separator diff --git a/src/pkg/path/filepath/path_test.go b/src/pkg/path/filepath/path_test.go index bc5e85a6e06..983cc85c8ef 100644 --- a/src/pkg/path/filepath/path_test.go +++ b/src/pkg/path/filepath/path_test.go @@ -629,6 +629,10 @@ var reltests = []RelTests{ {"a/b/../c", "a/b", "../b"}, {"a/b/c", "a/c/d", "../../c/d"}, {"a/b", "c/d", "../../c/d"}, + {"a/b/c/d", "a/b", "../.."}, + {"a/b/c/d", "a/b/", "../.."}, + {"a/b/c/d/", "a/b", "../.."}, + {"a/b/c/d/", "a/b/", "../.."}, {"../../a/b", "../../a/b/c/d", "c/d"}, {"/a/b", "/a/b", "."}, {"/a/b/.", "/a/b", "."}, @@ -640,6 +644,10 @@ var reltests = []RelTests{ {"/a/b/../c", "/a/b", "../b"}, {"/a/b/c", "/a/c/d", "../../c/d"}, {"/a/b", "/c/d", "../../c/d"}, + {"/a/b/c/d", "/a/b", "../.."}, + {"/a/b/c/d", "/a/b/", "../.."}, + {"/a/b/c/d/", "/a/b", "../.."}, + {"/a/b/c/d/", "/a/b/", "../.."}, {"/../../a/b", "/../../a/b/c/d", "c/d"}, {".", "a/b", "a/b"}, {".", "..", ".."},