diff --git a/src/path/filepath/path_windows.go b/src/path/filepath/path_windows.go index b4d8ac33010..80998decc6e 100644 --- a/src/path/filepath/path_windows.go +++ b/src/path/filepath/path_windows.go @@ -13,24 +13,24 @@ func isSlash(c uint8) bool { return c == '\\' || c == '/' } -// reservedNames lists reserved Windows names. Search for PRN in -// https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file -// for details. -var reservedNames = []string{ - "CON", "PRN", "AUX", "NUL", - "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", - "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", -} - // isReservedName returns true, if path is Windows reserved name. // See reservedNames for the full list. func isReservedName(path string) bool { - if len(path) == 0 { - return false + toUpper := func(c byte) byte { + if 'a' <= c && c <= 'z' { + return c - ('a' - 'A') + } + return c } - for _, reserved := range reservedNames { - if strings.EqualFold(path, reserved) { - return true + + // For details, search for PRN in + // https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file. + if 3 <= len(path) && len(path) <= 4 { + switch string([]byte{toUpper(path[0]), toUpper(path[1]), toUpper(path[2])}) { + case "CON", "PRN", "AUX", "NUL": + return len(path) == 3 + case "COM", "LPT": + return len(path) == 4 && '1' <= path[3] && path[3] <= '9' } } return false