From fb15c97c6caaecd7c03614ccc527c29a67e614bc Mon Sep 17 00:00:00 2001 From: vishalnayak Date: Wed, 17 May 2017 21:47:13 -0400 Subject: [PATCH] Fix index out of range bug in ParseKeyValues --- helper/strutil/strutil.go | 4 ++++ helper/strutil/strutil_test.go | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/helper/strutil/strutil.go b/helper/strutil/strutil.go index 986928e0ed..eaab5007bb 100644 --- a/helper/strutil/strutil.go +++ b/helper/strutil/strutil.go @@ -69,6 +69,10 @@ func ParseKeyValues(input string, out map[string]string, sep string) error { for _, keyValue := range keyValues { shards := strings.Split(keyValue, "=") + if len(shards) != 2 { + return fmt.Errorf("invalid format") + } + key := strings.TrimSpace(shards[0]) value := strings.TrimSpace(shards[1]) if key == "" || value == "" { diff --git a/helper/strutil/strutil_test.go b/helper/strutil/strutil_test.go index 9fd3bef393..5a76cd2dda 100644 --- a/helper/strutil/strutil_test.go +++ b/helper/strutil/strutil_test.go @@ -139,7 +139,7 @@ func TestStrutil_ParseKeyValues(t *testing.T) { input = "key1 = value1, key2 = " err = ParseKeyValues(input, actual, ",") if err == nil { - t.Fatal("expected an error") + t.Fatalf("expected an error") } for k, _ := range actual { delete(actual, k) @@ -148,11 +148,17 @@ func TestStrutil_ParseKeyValues(t *testing.T) { input = "key1 = value1, = value2 " err = ParseKeyValues(input, actual, ",") if err == nil { - t.Fatal("expected an error") + t.Fatalf("expected an error") } for k, _ := range actual { delete(actual, k) } + + input = "key1" + err = ParseKeyValues(input, actual, ",") + if err == nil { + t.Fatalf("expected an error") + } } func TestStrutil_ParseArbitraryKeyValues(t *testing.T) {