Skip to content

Commit 88c0e10

Browse files
authored
Narrow drive letter matching for splitdrive on Windows to one character, fixup docstring for joinpath (#58951)
Fixes #58929 Only a single letter followed by a colon is a valid drive prefix for this format, any other length of string prior to a colon isn't a valid drive in Windows: https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats Also the docstring for `joinpath` rendered Windows paths as unescaped, meaning they could not be copied and pasted on the REPL.
1 parent 9c94e7a commit 88c0e10

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

base/path.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ elseif Sys.iswindows()
4040
# Not a slash in either direction.
4141
N = raw"[^\\/]"
4242
# Drive letter, e.g. `C:`
43-
drive = "$(N)+:"
43+
drive = "$(N):"
4444
# UNC path, e.g. `\\server\share`
4545
unc = "$(S)$(S)$(N)+$(S)$(N)+"
4646
# Long drive letter, e.g. `\\?\C:`
@@ -357,7 +357,7 @@ the join of the preceding paths, then prior components are dropped.
357357
Note on Windows since there is a current directory for each drive, `joinpath("c:", "foo")`
358358
represents a path relative to the current directory on drive "c:" so this is equal to "c:foo",
359359
not "c:\\foo". Furthermore, `joinpath` treats this as a non-absolute path and ignores the drive
360-
letter casing, hence `joinpath("C:\\A","c:b") = "C:\\A\\b"`.
360+
letter casing, hence `joinpath("C:\\\\A","c:b") = "C:\\\\A\\\\b"`.
361361
362362
# Examples
363363
```jldoctest

test/path.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
end
6666

6767
if Sys.iswindows()
68-
@test joinpath(S("foo"),S("bar:baz")) == "bar:baz"
68+
@test joinpath(S("foo"),S("D:bar")) == "D:bar"
6969
@test joinpath(S("C:"),S("foo"),S("D:"),S("bar")) == "D:bar"
7070
@test joinpath(S("C:"),S("foo"),S("D:bar"),S("baz")) == "D:bar$(sep)baz"
7171

@@ -181,6 +181,9 @@
181181
("\\\\servername.com\\hello.world","\\filename.ext")
182182
@test splitdrive(S("C:\\foo\\bar")) ==
183183
("C:","\\foo\\bar")
184+
# only single characters followed by a colon are drives
185+
@test splitdrive(S("foo:bar")) ==
186+
("", "foo:bar")
184187
end
185188

186189
@test splitext(S("")) == ("", "")

0 commit comments

Comments
 (0)