@@ -18,6 +18,24 @@ func ParseKeyValueOpt(opt string) (string, string, error) {
1818 return strings .TrimSpace (parts [0 ]), strings .TrimSpace (parts [1 ]), nil
1919}
2020
21+ // ParseUintListMaximum parses and validates the specified string as the value
22+ // found in some cgroup file (e.g. `cpuset.cpus`, `cpuset.mems`), which could be
23+ // one of the formats below. Note that duplicates are actually allowed in the
24+ // input string. It returns a `map[int]bool` with available elements from `val`
25+ // set to `true`. Values larger than `maximum` cause an error if max is non zero,
26+ // in order to stop the map becoming excessively large.
27+ // Supported formats:
28+ // 7
29+ // 1-6
30+ // 0,3-4,7,8-10
31+ // 0-0,0,1-7
32+ // 03,1-3 <- this is gonna get parsed as [1,2,3]
33+ // 3,2,1
34+ // 0-2,3,1
35+ func ParseUintListMaximum (val string , maximum int ) (map [int ]bool , error ) {
36+ return parseUintList (val , maximum )
37+ }
38+
2139// ParseUintList parses and validates the specified string as the value
2240// found in some cgroup file (e.g. `cpuset.cpus`, `cpuset.mems`), which could be
2341// one of the formats below. Note that duplicates are actually allowed in the
@@ -32,6 +50,10 @@ func ParseKeyValueOpt(opt string) (string, string, error) {
3250// 3,2,1
3351// 0-2,3,1
3452func ParseUintList (val string ) (map [int ]bool , error ) {
53+ return parseUintList (val , 0 )
54+ }
55+
56+ func parseUintList (val string , maximum int ) (map [int ]bool , error ) {
3557 if val == "" {
3658 return map [int ]bool {}, nil
3759 }
@@ -46,6 +68,9 @@ func ParseUintList(val string) (map[int]bool, error) {
4668 if err != nil {
4769 return nil , errInvalidFormat
4870 }
71+ if maximum != 0 && v > maximum {
72+ return nil , fmt .Errorf ("value of out range, maximum is %d" , maximum )
73+ }
4974 availableInts [v ] = true
5075 } else {
5176 split := strings .SplitN (r , "-" , 2 )
@@ -60,6 +85,9 @@ func ParseUintList(val string) (map[int]bool, error) {
6085 if max < min {
6186 return nil , errInvalidFormat
6287 }
88+ if maximum != 0 && max > maximum {
89+ return nil , fmt .Errorf ("value of out range, maximum is %d" , maximum )
90+ }
6391 for i := min ; i <= max ; i ++ {
6492 availableInts [i ] = true
6593 }
0 commit comments