From 610b52d8005f10ae1b193f9c08ebf40de49623dd Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 28 Aug 2023 11:46:49 +0200 Subject: [PATCH] ParseRestartPolicy: validate for missing policy-names Also make it slightly more clearer we're returning a default (empty) policy if the input is empty. Signed-off-by: Sebastiaan van Stijn --- cli/command/container/opts_test.go | 4 ++++ opts/parse.go | 13 +++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/cli/command/container/opts_test.go b/cli/command/container/opts_test.go index 460c74fad06f..82ce73bd67cd 100644 --- a/cli/command/container/opts_test.go +++ b/cli/command/container/opts_test.go @@ -714,6 +714,10 @@ func TestParseRestartPolicy(t *testing.T) { Name: "no", }, }, + { + input: ":1", + expectedErr: "invalid restart policy format: no policy provided before colon", + }, { input: "always", expected: container.RestartPolicy{ diff --git a/opts/parse.go b/opts/parse.go index 017577e4bf09..f74cff74475f 100644 --- a/opts/parse.go +++ b/opts/parse.go @@ -71,13 +71,18 @@ func ConvertKVStringsToMapWithNil(values []string) map[string]*string { // ParseRestartPolicy returns the parsed policy or an error indicating what is incorrect func ParseRestartPolicy(policy string) (container.RestartPolicy, error) { - p := container.RestartPolicy{} - if policy == "" { - return p, nil + // for backward-compatibility, we don't set the default ("no") + // policy here, because older versions of the engine may not + // support it. + return container.RestartPolicy{}, nil } - k, v, _ := strings.Cut(policy, ":") + p := container.RestartPolicy{} + k, v, ok := strings.Cut(policy, ":") + if ok && k == "" { + return container.RestartPolicy{}, fmt.Errorf("invalid restart policy format: no policy provided before colon") + } if v != "" { count, err := strconv.Atoi(v) if err != nil {