aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2022-02-22 15:32:47 +0100
committerGitHub <noreply@github.com>2022-02-22 15:32:47 +0100
commitba49ee6d97276ee19fc9b4d5f0d328df73273a74 (patch)
tree3a41d433fcff0f0194fe4c32a50bee57688ec6f9
parent8ddb221395a803579772e9836740c62a4caded8a (diff)
parentabb2667f4b56850d8c670b0bfa638d1bc03cfc88 (diff)
Merge pull request #21309 from vespa-engine/ean/stricter-nodes-count-validation
Add more strict validation for node counts
-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{}) {