From 906238edc6eb0ddface4a1923f6d41ef2a5ca59b Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Thu, 1 Mar 2018 15:22:09 +0100 Subject: [PATCH] fix: panicing on options parsing. (#642) --- clientconfig.go | 2 +- clientconfig_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/clientconfig.go b/clientconfig.go index a606ef69..f13cfa30 100644 --- a/clientconfig.go +++ b/clientconfig.go @@ -91,7 +91,7 @@ func ClientConfigFromReader(resolvconf io.Reader) (*ClientConfig, error) { n = 1 } c.Timeout = n - case len(s) >= 8 && s[:9] == "attempts:": + case len(s) >= 9 && s[:9] == "attempts:": n, _ := strconv.Atoi(s[9:]) if n < 1 { n = 1 diff --git a/clientconfig_test.go b/clientconfig_test.go index 5c10360d..ad5d7d08 100644 --- a/clientconfig_test.go +++ b/clientconfig_test.go @@ -59,7 +59,35 @@ func TestNdots(t *testing.T) { t.Errorf("Ndots not properly parsed: (Expected: %d / Was: %d)", ndotsVariants[data], cc.Ndots) } } +} +func TestClientConfigFromReaderAttempts(t *testing.T) { + testCases := []struct { + data string + expected int + }{ + {data: "options attempts:0", expected: 1}, + {data: "options attempts:1", expected: 1}, + {data: "options attempts:15", expected: 15}, + {data: "options attempts:16", expected: 16}, + {data: "options attempts:-1", expected: 1}, + {data: "options attempt:", expected: 2}, + } + + for _, test := range testCases { + test := test + t.Run(strings.Replace(test.data, ":", " ", -1), func(t *testing.T) { + t.Parallel() + + cc, err := ClientConfigFromReader(strings.NewReader(test.data)) + if err != nil { + t.Errorf("error parsing resolv.conf: %v", err) + } + if cc.Attempts != test.expected { + t.Errorf("A attempts not properly parsed: (Expected: %d / Was: %d)", test.expected, cc.Attempts) + } + }) + } } func TestReadFromFile(t *testing.T) {