aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2011-11-27 21:28:52 -0500
committerBrad Fitzpatrick <bradfitz@golang.org>2011-11-27 21:28:52 -0500
commita620865639d4e8c159c563c05b6cd7b50596273c (patch)
tree74b3de30eb6741da16bdc38b4364cc2e846c8a0c
parentf1fecf8d2a128d203dab25b4a3ff4537b4b22e3e (diff)
downloadgo-a620865639d4e8c159c563c05b6cd7b50596273c.tar.gz
go-a620865639d4e8c159c563c05b6cd7b50596273c.zip
filepath/path: fix Rel buffer sizing
Fixes #2493. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/5433079
-rw-r--r--src/pkg/path/filepath/path.go6
-rw-r--r--src/pkg/path/filepath/path_test.go8
2 files changed, 13 insertions, 1 deletions
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"},
{".", "..", ".."},