forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 2
/
check.go
53 lines (45 loc) · 1.09 KB
/
check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// Copyright (c) 2017-2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
"regexp"
"unicode"
)
// checkValid determines if the specified string is valid or not.
// It looks for:
//
// - Invalid (unprintable) characters.
// - Standard golang error strings added by the formatting functions into the
// resulting strings when issues are detected.
func checkValid(value string) error {
if value == "" {
return nil
}
for _, ch := range value {
if !(unicode.IsPrint(ch) || unicode.IsSpace(ch)) {
return fmt.Errorf("character %v (%x) in value %v not printable", ch, ch, value)
}
}
// See: https://golang.org/pkg/fmt/
invalidPatterns := []string{
`%!\(BADINDEX\)`,
`%!\(BADPREC\)`,
`%!\(BADWIDTH\)`,
`%!\(EXTRA\b`,
`%!\w\(MISSING\)`,
}
for _, pattern := range invalidPatterns {
re := regexp.MustCompile(pattern)
foundMissing := re.FindStringSubmatch(value)
if foundMissing != nil {
return fmt.Errorf("invalid pattern %q in value %v "+
"suggests log creator programming error",
pattern, value)
}
}
return nil
}