Skip to content

Commit 935faf3

Browse files
mattnrsc
authored andcommitted
path/filepath: in Rel use case-insensitive comparison on Windows
Fixes #10802 Compare Volume name and each path elements using strings.EqualFold. Change-Id: Ibdefdb801d0326e53755bc9cc8c10eed998094e5 Reviewed-on: https://go-review.googlesource.com/16795 Reviewed-by: Russ Cox <[email protected]>
1 parent b28eeea commit 935faf3

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

src/path/filepath/path.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func Rel(basepath, targpath string) (string, error) {
269269
// Can't use IsAbs - `\a` and `a` are both relative in Windows.
270270
baseSlashed := len(base) > 0 && base[0] == Separator
271271
targSlashed := len(targ) > 0 && targ[0] == Separator
272-
if baseSlashed != targSlashed || baseVol != targVol {
272+
if baseSlashed != targSlashed || !sameWord(baseVol, targVol) {
273273
return "", errors.New("Rel: can't make " + targ + " relative to " + base)
274274
}
275275
// Position base[b0:bi] and targ[t0:ti] at the first differing elements.
@@ -283,7 +283,7 @@ func Rel(basepath, targpath string) (string, error) {
283283
for ti < tl && targ[ti] != Separator {
284284
ti++
285285
}
286-
if targ[t0:ti] != base[b0:bi] {
286+
if !sameWord(targ[t0:ti], base[b0:bi]) {
287287
break
288288
}
289289
if bi < bl {

src/path/filepath/path_plan9.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ func join(elem []string) string {
4242
}
4343
return ""
4444
}
45+
46+
func sameWord(a, b string) bool {
47+
return a == b
48+
}

src/path/filepath/path_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@ var winreltests = []RelTests{
10331033
{`C:a\b\c`, `C:a/b/d`, `..\d`},
10341034
{`C:\`, `D:\`, `err`},
10351035
{`C:`, `D:`, `err`},
1036+
{`C:\Projects`, `c:\projects\src`, `src`},
10361037
}
10371038

10381039
func TestRel(t *testing.T) {

src/path/filepath/path_unix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ func join(elem []string) string {
4444
}
4545
return ""
4646
}
47+
48+
func sameWord(a, b string) bool {
49+
return a == b
50+
}

src/path/filepath/path_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,7 @@ func joinNonEmpty(elem []string) string {
145145
func isUNC(path string) bool {
146146
return volumeNameLen(path) > 2
147147
}
148+
149+
func sameWord(a, b string) bool {
150+
return strings.EqualFold(a, b)
151+
}

0 commit comments

Comments
 (0)