Skip to content

Commit

Permalink
Merge pull request #6064 from chipaca/precompile-some-regexps
Browse files Browse the repository at this point in the history
many: move regexp.(Must)Compile out of non-init functions into variables
  • Loading branch information
mvo5 committed Oct 30, 2018
2 parents 34ccf19 + 8db4fb0 commit 6e2c5d8
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 25 deletions.
9 changes: 5 additions & 4 deletions asserts/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ func parseShadowLine(line string) (*shadow, error) {
}, nil
}

// see crypt(3) for the legal chars
var isValidSaltAndHash = regexp.MustCompile(`^[a-zA-Z0-9./]+$`).MatchString

func checkHashedPassword(headers map[string]interface{}, name string) (string, error) {
pw, err := checkOptionalString(headers, name)
if err != nil {
Expand Down Expand Up @@ -197,12 +200,10 @@ func checkHashedPassword(headers map[string]interface{}, name string) (string, e
}
}

// see crypt(3) for the legal chars
validSaltAndHash := regexp.MustCompile(`^[a-zA-Z0-9./]+$`)
if !validSaltAndHash.MatchString(shd.Salt) {
if !isValidSaltAndHash(shd.Salt) {
return "", fmt.Errorf("%q header has invalid chars in salt %q", name, shd.Salt)
}
if !validSaltAndHash.MatchString(shd.Hash) {
if !isValidSaltAndHash(shd.Hash) {
return "", fmt.Errorf("%q header has invalid chars in hash %q", name, shd.Hash)
}

Expand Down
5 changes: 3 additions & 2 deletions client/icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Icon struct {
Content []byte
}

var contentDispositionMatcher = regexp.MustCompile(`attachment; filename=(.+)`).FindStringSubmatch

// Icon returns the Icon belonging to an installed snap
func (c *Client) Icon(pkgID string) (*Icon, error) {
const errPrefix = "cannot retrieve icon"
Expand All @@ -45,8 +47,7 @@ func (c *Client) Icon(pkgID string) (*Icon, error) {
return nil, fmt.Errorf("%s: Not Found", errPrefix)
}

re := regexp.MustCompile(`attachment; filename=(.+)`)
matches := re.FindStringSubmatch(response.Header.Get("Content-Disposition"))
matches := contentDispositionMatcher(response.Header.Get("Content-Disposition"))

if matches == nil || matches[1] == "" {
return nil, fmt.Errorf("%s: cannot determine filename", errPrefix)
Expand Down
13 changes: 7 additions & 6 deletions interfaces/builtin/dbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ func (iface *dbusInterface) StaticInfo() interfaces.StaticInfo {
}
}

// snapd has AppArmor rules (see above) allowing binds to busName-PID
// so to avoid overlap with different snaps (eg, busName running as PID
// 123 and busName-123), don't allow busName to end with -PID. If that
// rule is removed, this limitation can be lifted.
var isInvalidSnappyBusName = regexp.MustCompile("-[0-9]+$").MatchString

// Obtain yaml-specified bus well-known name
func (iface *dbusInterface) getAttribs(attribs interfaces.Attrer) (string, string, error) {
// bus attribute
Expand All @@ -243,12 +249,7 @@ func (iface *dbusInterface) getAttribs(attribs interfaces.Attrer) (string, strin
return "", "", err
}

// snapd has AppArmor rules (see above) allowing binds to busName-PID
// so to avoid overlap with different snaps (eg, busName running as PID
// 123 and busName-123), don't allow busName to end with -PID. If that
// rule is removed, this limitation can be lifted.
invalidSnappyBusName := regexp.MustCompile("-[0-9]+$")
if invalidSnappyBusName.MatchString(name) {
if isInvalidSnappyBusName(name) {
return "", "", fmt.Errorf("DBus bus name must not end with -NUMBER")
}

Expand Down
5 changes: 3 additions & 2 deletions interfaces/builtin/mpris.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ func (iface *mprisInterface) AppArmorConnectedSlot(spec *apparmor.Specification,
return nil
}

var isValidDBusElement = regexp.MustCompile("^[a-zA-Z0-9_-]*$").MatchString

func (iface *mprisInterface) getName(attribs map[string]interface{}) (string, error) {
// default to snap instance name if 'name' attribute not set
// parallel-installs: snaps utilizing the mpris interface must adjust
Expand All @@ -215,8 +217,7 @@ func (iface *mprisInterface) getName(attribs map[string]interface{}) (string, er
return "", fmt.Errorf("name element %v is not a string", raw)
}

validDBusElement := regexp.MustCompile("^[a-zA-Z0-9_-]*$")
if !validDBusElement.MatchString(name) {
if !isValidDBusElement(name) {
return "", fmt.Errorf("invalid name element: %q", name)
}
mprisName = name
Expand Down
5 changes: 3 additions & 2 deletions interfaces/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ const (
SecuritySystemd SecuritySystem = "systemd"
)

var isValidBusName = regexp.MustCompile(`^[a-zA-Z_-][a-zA-Z0-9_-]*(\.[a-zA-Z_-][a-zA-Z0-9_-]*)+$`).MatchString

// ValidateDBusBusName checks if a string conforms to
// https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
func ValidateDBusBusName(busName string) error {
Expand All @@ -225,8 +227,7 @@ func ValidateDBusBusName(busName string) error {
return fmt.Errorf("DBus bus name is too long (must be <= 255)")
}

validBusName := regexp.MustCompile("^[a-zA-Z_-][a-zA-Z0-9_-]*(\\.[a-zA-Z_-][a-zA-Z0-9_-]*)+$")
if !validBusName.MatchString(busName) {
if !isValidBusName(busName) {
return fmt.Errorf("invalid DBus bus name: %q", busName)
}
return nil
Expand Down
11 changes: 6 additions & 5 deletions osutil/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ type AddUserOptions struct {
ForcePasswordChange bool
}

// we check the (user)name ourselves, adduser is a bit too
// strict (i.e. no `.`) - this regexp is in sync with that SSO
// allows as valid usernames
var isValidUsername = regexp.MustCompile(`^[a-z0-9][-a-z0-9+.-_]*$`).MatchString

func AddUser(name string, opts *AddUserOptions) error {
if opts == nil {
opts = &AddUserOptions{}
}

// we check the (user)name ourselves, adduser is a bit too
// strict (i.e. no `.`) - this regexp is in sync with that SSO
// allows as valid usernames
validNames := regexp.MustCompile(`^[a-z0-9][-a-z0-9+.-_]*$`)
if !validNames.MatchString(name) {
if !isValidUsername(name) {
return fmt.Errorf("cannot add user %q: name contains invalid characters", name)
}

Expand Down
4 changes: 3 additions & 1 deletion snap/squashfs/squashfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ type unsquashfsStderrWriter struct {
strutil.MatchCounter
}

var unsquashfsStderrRegexp = regexp.MustCompile(`(?m).*\b[Ff]ailed\b.*`)

func newUnsquashfsStderrWriter() *unsquashfsStderrWriter {
return &unsquashfsStderrWriter{strutil.MatchCounter{
Regexp: regexp.MustCompile(`(?m).*\b[Ff]ailed\b.*`),
Regexp: unsquashfsStderrRegexp,
N: 4, // note Err below uses this value
}}
}
Expand Down
5 changes: 2 additions & 3 deletions snap/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,12 +591,11 @@ func validateAppRestart(app *AppInfo) error {
// command-chain, which also doesn't allow whitespace.
var appContentWhitelist = regexp.MustCompile(`^[A-Za-z0-9/. _#:$-]*$`)
var commandChainContentWhitelist = regexp.MustCompile(`^[A-Za-z0-9/._#:$-]*$`)
var validAppName = regexp.MustCompile("^[a-zA-Z0-9](?:-?[a-zA-Z0-9])*$").MatchString

// ValidAppName tells whether a string is a valid application name.
func ValidAppName(n string) bool {
var validAppName = regexp.MustCompile("^[a-zA-Z0-9](?:-?[a-zA-Z0-9])*$")

return validAppName.MatchString(n)
return validAppName(n)
}

// ValidateApp verifies the content in the app info.
Expand Down

0 comments on commit 6e2c5d8

Please sign in to comment.