Skip to content

Commit c2fc1f4

Browse files
committed
internal/opts: SetOpts: invalidate empty option-names
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 908bb95 commit c2fc1f4

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

internal/opts/opts.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package opts
22

33
import (
4+
"errors"
45
"fmt"
56
"strconv"
67
"strings"
@@ -17,6 +18,9 @@ type SetOpts struct {
1718
// internal map, by splitting on '='.
1819
func (opts *SetOpts) Set(value string) error {
1920
k, v, found := strings.Cut(value, "=")
21+
if k == "" {
22+
return errors.New("invalid option name: " + value)
23+
}
2024
var isSet bool
2125
if !found {
2226
isSet = true

internal/opts/opts_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func TestSetOpts(t *testing.T) {
2424

2525
err := o.Set("feature=not-a-bool")
2626
assert.Check(t, is.Error(err, `strconv.ParseBool: parsing "not-a-bool": invalid syntax`))
27+
err = o.Set("feature=")
28+
assert.Check(t, is.Error(err, `strconv.ParseBool: parsing "": invalid syntax`))
29+
err = o.Set("=true")
30+
assert.Check(t, is.Error(err, `invalid option name: =true`))
2731
}
2832

2933
func TestNamedSetOpts(t *testing.T) {
@@ -45,4 +49,8 @@ func TestNamedSetOpts(t *testing.T) {
4549

4650
err := o.Set("feature=not-a-bool")
4751
assert.Check(t, is.Error(err, `strconv.ParseBool: parsing "not-a-bool": invalid syntax`))
52+
err = o.Set("feature=")
53+
assert.Check(t, is.Error(err, `strconv.ParseBool: parsing "": invalid syntax`))
54+
err = o.Set("=true")
55+
assert.Check(t, is.Error(err, `invalid option name: =true`))
4856
}

0 commit comments

Comments
 (0)