Skip to content

Commit

Permalink
Detect reserved OS names
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Aug 25, 2024
1 parent c5f9a7b commit 1c4830d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions v2/app/BlockCompressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,21 @@ func NewBlockCompressor(argsMap map[string]any) (*BlockCompressor, error) {
this.inputName = argsMap["inputName"].(string)
delete(argsMap, "inputName")

if internal.IsReservedName(this.inputName) {
return nil, fmt.Errorf("'%s' is a reserved name", this.inputName)
}

if len(this.inputName) == 0 {
this.inputName = _COMP_STDIN
}

this.outputName = argsMap["outputName"].(string)
delete(argsMap, "outputName")

if internal.IsReservedName(this.outputName) {
return nil, fmt.Errorf("'%s' is a reserved name", this.outputName)
}

if len(this.outputName) == 0 && this.inputName == _COMP_STDIN {
this.outputName = _COMP_STDOUT
}
Expand Down
8 changes: 8 additions & 0 deletions v2/app/BlockDecompressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,21 @@ func NewBlockDecompressor(argsMap map[string]any) (*BlockDecompressor, error) {
this.inputName = argsMap["inputName"].(string)
delete(argsMap, "inputName")

if internal.IsReservedName(this.inputName) {
return nil, fmt.Errorf("'%s' is a reserved name", this.inputName)
}

if len(this.inputName) == 0 {
this.inputName = _DECOMP_STDIN
}

this.outputName = argsMap["outputName"].(string)
delete(argsMap, "outputName")

if internal.IsReservedName(this.outputName) {
return nil, fmt.Errorf("'%s' is a reserved name", this.outputName)
}

if len(this.outputName) == 0 && this.inputName == _DECOMP_STDIN {
this.outputName = _DECOMP_STDOUT
}
Expand Down
26 changes: 26 additions & 0 deletions v2/internal/File.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
)

Expand Down Expand Up @@ -173,3 +174,28 @@ func CreateFileList(target string, fileList []FileData, isRecursive, ignoreLinks

return fileList, err
}

func IsReservedName(fileName string) bool {
if runtime.GOOS != "windows" {
return false
}

// Sorted list
var reserved = []string{"AUX", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6",
"COM7", "COM8", "COM9", "COM¹", "COM²", "COM³", "CON", "LPT0", "LPT1", "LPT2",
"LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "NUL", "PRN"}

for _, r := range reserved {
res := strings.Compare(fileName, r)

if res == 0 {
return true
}

if res < 0 {
break
}
}

return false
}

0 comments on commit 1c4830d

Please sign in to comment.