From: Gustavo Niemeyer Date: Mon, 28 Nov 2011 02:28:52 +0000 (-0500) Subject: filepath/path: fix Rel buffer sizing X-Git-Tag: weekly.2011-12-01~47 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a620865639d4e8c159c563c05b6cd7b50596273c;p=gostls13.git filepath/path: fix Rel buffer sizing Fixes #2493. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/5433079 --- diff --git a/src/pkg/path/filepath/path.go b/src/pkg/path/filepath/path.go index 1b5d6c3649..3656227ff0 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 bc5e85a6e0..983cc85c8e 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"}, {".", "..", ".."},