Commit a6208656 authored by Gustavo Niemeyer's avatar Gustavo Niemeyer Committed by Brad Fitzpatrick

filepath/path: fix Rel buffer sizing

Fixes #2493.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5433079
parent f1fecf8d
......@@ -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
......
......@@ -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"},
{".", "..", ".."},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment