Skip to content

Commit

Permalink
path/filepath: detect Windows CONIN$ and CONOUT$ paths in IsLocal
Browse files Browse the repository at this point in the history
CreateFile creates a handle to the console input or screen buffer
when opening a file named CONIN$ or CONOUT$:

https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#consoles

Detect these paths as non-local.

For #56219.

Change-Id: Ib09e76a110d6ec09aef8038074b9bcbae09d00d7
Reviewed-on: https://go-review.googlesource.com/c/go/+/451657
Run-TryBot: Damien Neil <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
  • Loading branch information
neild committed Nov 17, 2022
1 parent 38b9ff6 commit 217ed95
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/path/filepath/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ var winislocaltests = []IsLocalTest{
{`C:`, false},
{`C:\a`, false},
{`..\a`, false},
{`CONIN$`, false},
{`conin$`, false},
{`CONOUT$`, false},
{`conout$`, false},
{`dollar$`, true}, // not a special file name
}

var plan9islocaltests = []IsLocalTest{
Expand Down
13 changes: 12 additions & 1 deletion src/path/filepath/path_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func toUpper(c byte) byte {
return c
}

// isReservedName reports if name is a Windows reserved device name.
// isReservedName reports if name is a Windows reserved device name or a console handle.
// It does not detect names with an extension, which are also reserved on some Windows versions.
//
// For details, search for PRN in
Expand All @@ -34,6 +34,17 @@ func isReservedName(name string) bool {
return len(name) == 4 && '1' <= name[3] && name[3] <= '9'
}
}
// Passing CONIN$ or CONOUT$ to CreateFile opens a console handle.
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#consoles
//
// While CONIN$ and CONOUT$ aren't documented as being files,
// they behave the same as CON. For example, ./CONIN$ also opens the console input.
if len(name) == 6 && name[5] == '$' && strings.EqualFold(name, "CONIN$") {
return true
}
if len(name) == 7 && name[6] == '$' && strings.EqualFold(name, "CONOUT$") {
return true
}
return false
}

Expand Down

0 comments on commit 217ed95

Please sign in to comment.