Commit b7d3e14a authored by Yasuhiro Matsumoto's avatar Yasuhiro Matsumoto Committed by Alex Brainman

path/filepath: fix Join with Windows drive letter

Join("C:", "", "b") must return relative path "C:b"

Fixes #26953

Change-Id: I2f843ce3f9f18a1ce0e2d0f3a15233f237992776
Reviewed-on: https://go-review.googlesource.com/129758
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarAlex Brainman <alex.brainman@gmail.com>
parent c21ba224
...@@ -271,6 +271,10 @@ var winjointests = []JoinTest{ ...@@ -271,6 +271,10 @@ var winjointests = []JoinTest{
{[]string{`C:`, `a`}, `C:a`}, {[]string{`C:`, `a`}, `C:a`},
{[]string{`C:`, `a\b`}, `C:a\b`}, {[]string{`C:`, `a\b`}, `C:a\b`},
{[]string{`C:`, `a`, `b`}, `C:a\b`}, {[]string{`C:`, `a`, `b`}, `C:a\b`},
{[]string{`C:`, ``, `b`}, `C:b`},
{[]string{`C:`, ``, ``, `b`}, `C:b`},
{[]string{`C:`, ``}, `C:.`},
{[]string{`C:`, ``, ``}, `C:.`},
{[]string{`C:.`, `a`}, `C:a`}, {[]string{`C:.`, `a`}, `C:a`},
{[]string{`C:a`, `b`}, `C:a\b`}, {[]string{`C:a`, `b`}, `C:a\b`},
{[]string{`C:a`, `b`, `d`}, `C:a\b\d`}, {[]string{`C:a`, `b`, `d`}, `C:a\b\d`},
......
...@@ -134,7 +134,14 @@ func joinNonEmpty(elem []string) string { ...@@ -134,7 +134,14 @@ func joinNonEmpty(elem []string) string {
if len(elem[0]) == 2 && elem[0][1] == ':' { if len(elem[0]) == 2 && elem[0][1] == ':' {
// First element is drive letter without terminating slash. // First element is drive letter without terminating slash.
// Keep path relative to current directory on that drive. // Keep path relative to current directory on that drive.
return Clean(elem[0] + strings.Join(elem[1:], string(Separator))) // Skip empty elements.
i := 1
for ; i < len(elem); i++ {
if elem[i] != "" {
break
}
}
return Clean(elem[0] + strings.Join(elem[i:], string(Separator)))
} }
// The following logic prevents Join from inadvertently creating a // The following logic prevents Join from inadvertently creating a
// UNC path on Windows. Unless the first element is a UNC path, Join // UNC path on Windows. Unless the first element is a UNC path, Join
......
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