]> Cypherpunks repositories - gostls13.git/commitdiff
filepath/path: fix Rel buffer sizing
authorGustavo Niemeyer <gustavo@niemeyer.net>
Mon, 28 Nov 2011 02:28:52 +0000 (21:28 -0500)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 28 Nov 2011 02:28:52 +0000 (21:28 -0500)
Fixes #2493.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5433079

src/pkg/path/filepath/path.go
src/pkg/path/filepath/path_test.go

index 1b5d6c36495b142b5afbb0794b83c30a58eb1350..3656227ff06d0e5631cf344097bb15089c10af0a 100644 (file)
@@ -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
index bc5e85a6e064de53b3c3f2e60b0d0c9ab374124c..983cc85c8ef3033b544c284c29addb92633be6a8 100644 (file)
@@ -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"},
        {".", "..", ".."},