From 51a112fefc26fe557bab46abbc3b50565e56c570 Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Sat, 5 Dec 2015 14:29:39 +1100 Subject: [PATCH] path/filepath: handle c: as first parameter in Join properly This is CL 11882 brought back to life. Fixes #11551 Change-Id: I29810183957745442d1e9937f56a66fc9c6cc82a Reviewed-on: https://go-review.googlesource.com/17470 Reviewed-by: Russ Cox Run-TryBot: Alex Brainman TryBot-Result: Gobot Gobot --- src/path/filepath/path_test.go | 7 ++++++- src/path/filepath/path_windows.go | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go index e41a97da11..201f4fa869 100644 --- a/src/path/filepath/path_test.go +++ b/src/path/filepath/path_test.go @@ -267,7 +267,12 @@ var winjointests = []JoinTest{ {[]string{`C:\Windows\`, `System32`}, `C:\Windows\System32`}, {[]string{`C:\Windows\`, ``}, `C:\Windows`}, {[]string{`C:\`, `Windows`}, `C:\Windows`}, - {[]string{`C:`, `Windows`}, `C:\Windows`}, + {[]string{`C:`, `a`}, `C:a`}, + {[]string{`C:`, `a\b`}, `C:a\b`}, + {[]string{`C:`, `a`, `b`}, `C:a\b`}, + {[]string{`C:.`, `a`}, `C:a`}, + {[]string{`C:a`, `b`}, `C:a\b`}, + {[]string{`C:a`, `b`, `d`}, `C:a\b\d`}, {[]string{`\\host\share`, `foo`}, `\\host\share\foo`}, {[]string{`\\host\share\foo`}, `\\host\share\foo`}, {[]string{`//host/share`, `foo/bar`}, `\\host\share\foo\bar`}, diff --git a/src/path/filepath/path_windows.go b/src/path/filepath/path_windows.go index edf7966d19..ef6e7ca93f 100644 --- a/src/path/filepath/path_windows.go +++ b/src/path/filepath/path_windows.go @@ -120,6 +120,11 @@ func join(elem []string) string { // joinNonEmpty is like join, but it assumes that the first element is non-empty. func joinNonEmpty(elem []string) string { + if len(elem[0]) == 2 && elem[0][1] == ':' { + // First element is drive leter without terminating slash. + // Keep path relative to current directory on that drive. + return Clean(elem[0] + strings.Join(elem[1:], string(Separator))) + } // The following logic prevents Join from inadvertently creating a // UNC path on Windows. Unless the first element is a UNC path, Join // shouldn't create a UNC path. See golang.org/issue/9167. -- 2.50.0