Skip to content

Commit

Permalink
libct: validateID: stop using regexp
Browse files Browse the repository at this point in the history
Replace a regex with a simple for loop.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Aug 18, 2022
1 parent 9a05bd1 commit b75a818
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions libcontainer/factory_linux.go
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"os"
"regexp"
"runtime/debug"
"strconv"

Expand All @@ -27,8 +26,6 @@ const (
execFifoFilename = "exec.fifo"
)

var idRegex = regexp.MustCompile(`^[\w+-\.]+$`)

// Create creates a new container with the given id inside a given state
// directory (root), and returns a Container object.
//
Expand Down Expand Up @@ -261,7 +258,28 @@ func loadState(root string) (*State, error) {
}

func validateID(id string) error {
if !idRegex.MatchString(id) || string(os.PathSeparator)+id != utils.CleanPath(string(os.PathSeparator)+id) {
if len(id) < 1 {
return ErrInvalidID
}

// Allowed characters: 0-9 A-Z a-z _ + - .
for i := 0; i < len(id); i++ {
c := id[i]
switch {
case c >= 'a' && c <= 'z':
case c >= 'A' && c <= 'Z':
case c >= '0' && c <= '9':
case c == '_':
case c == '+':
case c == '-':
case c == '.':
default:
return ErrInvalidID
}

}

if string(os.PathSeparator)+id != utils.CleanPath(string(os.PathSeparator)+id) {
return ErrInvalidID
}

Expand Down

0 comments on commit b75a818

Please sign in to comment.