Skip to content

Commit

Permalink
generate: add param check for mems and cpus
Browse files Browse the repository at this point in the history
Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
  • Loading branch information
Ma Shimiao committed Jul 5, 2017
1 parent 2f4cbd9 commit 038e9eb
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions cmd/oci-runtime-tool/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,19 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
}

if context.IsSet("linux-cpus") {
g.SetLinuxResourcesCPUCpus(context.String("linux-cpus"))
if err := uintListValid(context.String("linux-cpus")); err != nil {
return err
} else {
g.SetLinuxResourcesCPUCpus(context.String("linux-cpus"))
}
}

if context.IsSet("linux-mems") {
g.SetLinuxResourcesCPUMems(context.String("linux-mems"))
if err := uintListValid(context.String("linux-mems")); err != nil {
return err
} else {
g.SetLinuxResourcesCPUMems(context.String("linux-mems"))
}
}

if context.IsSet("linux-mem-limit") {
Expand Down Expand Up @@ -541,6 +549,38 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
return err
}

func uintListValid(val string) error {
if val == "" {
return nil
}

split := strings.Split(val, ",")
errInvalidFormat := fmt.Errorf("invalid format: %s", val)

for _, r := range split {
if !strings.Contains(r, "-") {
_, err := strconv.Atoi(r)
if err != nil {
return errInvalidFormat
}
} else {
split := strings.SplitN(r, "-", 2)
min, err := strconv.Atoi(split[0])
if err != nil {
return errInvalidFormat
}
max, err := strconv.Atoi(split[1])
if err != nil {
return errInvalidFormat
}
if max < min {
return errInvalidFormat
}
}
}
return nil
}

func parseIDMapping(idms string) (uint32, uint32, uint32, error) {
idm := strings.Split(idms, ":")
if len(idm) != 3 {
Expand Down

0 comments on commit 038e9eb

Please sign in to comment.