From b8c489795dcb6b7be0013a21cd51ebdc0f54aabc Mon Sep 17 00:00:00 2001 From: Ma Shimiao Date: Fri, 16 Dec 2016 10:14:40 +0800 Subject: [PATCH] generate: enhance options parse Signed-off-by: Ma Shimiao --- cmd/oci-runtime-tool/generate.go | 35 +++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/cmd/oci-runtime-tool/generate.go b/cmd/oci-runtime-tool/generate.go index a9d92107b..cc1ee8b6c 100644 --- a/cmd/oci-runtime-tool/generate.go +++ b/cmd/oci-runtime-tool/generate.go @@ -328,7 +328,10 @@ func setupSpec(g *generate.Generator, context *cli.Context) error { if context.IsSet("prestart") { preStartHooks := context.StringSlice("prestart") for _, hook := range preStartHooks { - path, args := parseHook(hook) + path, args, err := parseHook(hook) + if err != nil { + return err + } g.AddPreStartHook(path, args) } } @@ -336,7 +339,10 @@ func setupSpec(g *generate.Generator, context *cli.Context) error { if context.IsSet("poststop") { postStopHooks := context.StringSlice("poststop") for _, hook := range postStopHooks { - path, args := parseHook(hook) + path, args, err := parseHook(hook) + if err != nil { + return err + } g.AddPostStopHook(path, args) } } @@ -344,7 +350,10 @@ func setupSpec(g *generate.Generator, context *cli.Context) error { if context.IsSet("poststart") { postStartHooks := context.StringSlice("poststart") for _, hook := range postStartHooks { - path, args := parseHook(hook) + path, args, err := parseHook(hook) + if err != nil { + return err + } g.AddPostStartHook(path, args) } } @@ -524,21 +533,24 @@ func parseIDMapping(idms string) (uint32, uint32, uint32, error) { return uint32(hid), uint32(cid), uint32(size), nil } -func parseHook(s string) (string, []string) { - parts := strings.Split(s, ":") +func parseHook(s string) (string, []string, error) { args := []string{} + parts := strings.Split(s, ":") + if len(parts) > 1 && parts[0] == "" { + return "", args, fmt.Errorf("invalid hook value: %s", s) + } path := parts[0] if len(parts) > 1 { args = parts[1:] } - return path, args + return path, args, nil } func parseNetworkPriority(np string) (string, int32, error) { var err error parts := strings.Split(np, ":") - if len(parts) != 2 { + if len(parts) != 2 || parts[0] == "" { return "", 0, fmt.Errorf("invalid value %v for --linux-network-priorities", np) } priority, err := strconv.Atoi(parts[1]) @@ -555,14 +567,14 @@ func parseTmpfsMount(s string) (string, []string, error) { var err error parts := strings.Split(s, ":") - if len(parts) == 2 { + if len(parts) == 2 && parts[0] != "" { dest = parts[0] options = strings.Split(parts[1], ",") } else if len(parts) == 1 { dest = parts[0] options = []string{"rw", "noexec", "nosuid", "nodev", "size=65536k"} } else { - err = fmt.Errorf("invalid value for --tmpfs") + err = fmt.Errorf("invalid -- tmpfs value: %s", s) } return dest, options, err @@ -582,12 +594,15 @@ func parseBindMount(s string) (string, string, []string, error) { return source, dest, options, fmt.Errorf("--bind should have format src:dest[:options...]") } + if source == "" || dest == "" { + return source, dest, options, fmt.Errorf("--bind should have format src:dest[:options...]") + } return source, dest, options, nil } func parseRlimit(rlimit string) (string, uint64, uint64, error) { parts := strings.Split(rlimit, ":") - if len(parts) != 3 { + if len(parts) != 3 || parts[0] == "" { return "", 0, 0, fmt.Errorf("invalid rlimits value: %s", rlimit) }