From 61cb6842faa01b8df696e5be978ce2ad655d73f2 Mon Sep 17 00:00:00 2001 From: Arpit Jain Date: Fri, 25 Sep 2026 00:23:31 -0400 Subject: [PATCH] Return an error instead of panicking on a valueless mount option Signed-off-by: Arpit Jain --- pkg/flags/mountopt_test.go | 27 +++++++++++++++++++++++++++ pkg/flags/podspec_helper.go | 8 +++----- 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 pkg/flags/mountopt_test.go diff --git a/pkg/flags/mountopt_test.go b/pkg/flags/mountopt_test.go new file mode 100644 index 0000000000..9dc4f1f79c --- /dev/null +++ b/pkg/flags/mountopt_test.go @@ -0,0 +1,27 @@ +package flags + +import "testing" + +// A mount option without a value used to index past the end of the split. +func TestParseMountOptionsWithoutValue(t *testing.T) { + tests := []struct { + in string + wantErr bool + }{ + {"readonly=true", false}, + {"readonly=false", false}, + {"readonly", true}, + {"readonly=", false}, + {"bogus=1", true}, + } + + for _, tc := range tests { + _, err := parseMountOptions(tc.in) + if tc.wantErr && err == nil { + t.Errorf("%q: expected an error, got none", tc.in) + } + if !tc.wantErr && err != nil { + t.Errorf("%q: unexpected error: %v", tc.in, err) + } + } +} diff --git a/pkg/flags/podspec_helper.go b/pkg/flags/podspec_helper.go index e0fe6d772a..f8d891f6a1 100644 --- a/pkg/flags/podspec_helper.go +++ b/pkg/flags/podspec_helper.go @@ -955,6 +955,9 @@ func parseMountOptions(options string) (*util.OrderedMap, error) { pair := strings.SplitN(slice, "=", 2) switch strings.ToLower(pair[0]) { case "readonly": + if len(pair) != 2 { + return nil, fmt.Errorf("mount option %q requires a value, for example readOnly=true", pair[0]) + } mountOptions.Set("readonly", pair[1]) default: return nil, fmt.Errorf("unknown mount option %q", pair[0]) @@ -994,11 +997,6 @@ func getMountInfo(volume string) *MountInfo { configSlices := strings.SplitN(volume, ":", 2) var mountInfo MountInfo if len(configSlices) == 2 { - readOnlySlices := strings.SplitN(configSlices[1], "=", 2) - switch strings.ToLower(readOnlySlices[0]) { - case "readonly": - - } mountInfo.MountOptions = configSlices[1] } slices := strings.SplitN(configSlices[0], "/", 2)