]> Cypherpunks repositories - gostls13.git/commitdiff
path/filepath: fix Join with Windows drive letter
authorYasuhiro Matsumoto <mattn.jp@gmail.com>
Mon, 20 Aug 2018 01:15:47 +0000 (10:15 +0900)
committerAlex Brainman <alex.brainman@gmail.com>
Sun, 26 Aug 2018 04:23:19 +0000 (04:23 +0000)
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: Alex Brainman <alex.brainman@gmail.com>
src/path/filepath/path_test.go
src/path/filepath/path_windows.go

index dde087253dda34ff1d2c57f29768855820e52946..e50ee97bcb3f7396ba1bcfbf7161dcad25707e7c 100644 (file)
@@ -271,6 +271,10 @@ var winjointests = []JoinTest{
        {[]string{`C:`, `a`}, `C:a`},
        {[]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`, `b`}, `C:a\b`},
        {[]string{`C:a`, `b`, `d`}, `C:a\b\d`},
index 409e8d6466a9506abc84aa9cb5dae34525f6de6f..519b6ebc329cc38052cb83e10fa35623c9dd85bf 100644 (file)
@@ -134,7 +134,14 @@ func joinNonEmpty(elem []string) string {
        if len(elem[0]) == 2 && elem[0][1] == ':' {
                // First element is drive letter without terminating slash.
                // 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
        // UNC path on Windows. Unless the first element is a UNC path, Join