summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorEirik Nygaard <eirik.nygaard@yahooinc.com>2022-02-22 14:33:44 +0100
committerEirik Nygaard <eirik.nygaard@yahooinc.com>2022-02-22 14:33:44 +0100
commitabb2667f4b56850d8c670b0bfa638d1bc03cfc88 (patch)
tree29a97db0c5d240611b928c3f1b8d9dcedca5a2f2 /client
parent96916710a1b46cbed64f15345e56a3a68df235f5 (diff)
Add more strict validation for node counts
Disallow count values that are zero, negative and ranges where max is less than min.
Diffstat (limited to 'client')
-rw-r--r--client/go/vespa/xml/config.go20
-rw-r--r--client/go/vespa/xml/config_test.go4
2 files changed, 17 insertions, 7 deletions
diff --git a/client/go/vespa/xml/config.go b/client/go/vespa/xml/config.go
index f1c598bec05..c9efcb7f340 100644
--- a/client/go/vespa/xml/config.go
+++ b/client/go/vespa/xml/config.go
@@ -189,26 +189,32 @@ func ParseResources(s string) (Resources, error) {
// ParseNodeCount parses a node count range from string s.
func ParseNodeCount(s string) (int, int, error) {
parseErr := fmt.Errorf("invalid node count: %q", s)
+ min, max := 0, 0
n, err := strconv.Atoi(s)
if err == nil {
- return n, n, nil
- }
- if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
+ min = n
+ max = n
+ } else if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
parts := strings.Split(s[1:len(s)-1], ",")
if len(parts) != 2 {
return 0, 0, parseErr
}
- min, err := strconv.Atoi(strings.TrimSpace(parts[0]))
+ min, err = strconv.Atoi(strings.TrimSpace(parts[0]))
if err != nil {
return 0, 0, parseErr
}
- max, err := strconv.Atoi(strings.TrimSpace(parts[1]))
+ max, err = strconv.Atoi(strings.TrimSpace(parts[1]))
if err != nil {
return 0, 0, parseErr
}
- return min, max, nil
+ } else {
+ return 0, 0, parseErr
+ }
+
+ if min <= 0 || min > max {
+ return 0, 0, parseErr
}
- return 0, 0, parseErr
+ return min, max, nil
}
// IsProdRegion returns whether string s is a valid production region.
diff --git a/client/go/vespa/xml/config_test.go b/client/go/vespa/xml/config_test.go
index 0c94285a6e1..0180a243406 100644
--- a/client/go/vespa/xml/config_test.go
+++ b/client/go/vespa/xml/config_test.go
@@ -258,6 +258,10 @@ func TestParseNodeCount(t *testing.T) {
assertNodeCount(t, "foo", 0, 0, true)
assertNodeCount(t, "[foo,bar]", 0, 0, true)
+
+ assertNodeCount(t, "0", 0, 0, true)
+ assertNodeCount(t, "-1", 0, 0, true)
+ assertNodeCount(t, "[2, 1]", 0, 0, true)
}
func assertReplace(t *testing.T, input, want, parentElement, element string, data interface{}) {