Skip to content

Commit b7d3e14

Browse files
mattnalexbrainman
authored andcommitted
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Alex Brainman <[email protected]>
1 parent c21ba22 commit b7d3e14

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/path/filepath/path_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ var winjointests = []JoinTest{
271271
{[]string{`C:`, `a`}, `C:a`},
272272
{[]string{`C:`, `a\b`}, `C:a\b`},
273273
{[]string{`C:`, `a`, `b`}, `C:a\b`},
274+
{[]string{`C:`, ``, `b`}, `C:b`},
275+
{[]string{`C:`, ``, ``, `b`}, `C:b`},
276+
{[]string{`C:`, ``}, `C:.`},
277+
{[]string{`C:`, ``, ``}, `C:.`},
274278
{[]string{`C:.`, `a`}, `C:a`},
275279
{[]string{`C:a`, `b`}, `C:a\b`},
276280
{[]string{`C:a`, `b`, `d`}, `C:a\b\d`},

src/path/filepath/path_windows.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,14 @@ func joinNonEmpty(elem []string) string {
134134
if len(elem[0]) == 2 && elem[0][1] == ':' {
135135
// First element is drive letter without terminating slash.
136136
// Keep path relative to current directory on that drive.
137-
return Clean(elem[0] + strings.Join(elem[1:], string(Separator)))
137+
// Skip empty elements.
138+
i := 1
139+
for ; i < len(elem); i++ {
140+
if elem[i] != "" {
141+
break
142+
}
143+
}
144+
return Clean(elem[0] + strings.Join(elem[i:], string(Separator)))
138145
}
139146
// The following logic prevents Join from inadvertently creating a
140147
// UNC path on Windows. Unless the first element is a UNC path, Join

0 commit comments

Comments
 (0)