Skip to content

Commit 906238e

Browse files
ldezmiekg
authored andcommitted
fix: panicing on options parsing. (miekg#642)
1 parent 4d25966 commit 906238e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

clientconfig.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func ClientConfigFromReader(resolvconf io.Reader) (*ClientConfig, error) {
9191
n = 1
9292
}
9393
c.Timeout = n
94-
case len(s) >= 8 && s[:9] == "attempts:":
94+
case len(s) >= 9 && s[:9] == "attempts:":
9595
n, _ := strconv.Atoi(s[9:])
9696
if n < 1 {
9797
n = 1

clientconfig_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,35 @@ func TestNdots(t *testing.T) {
5959
t.Errorf("Ndots not properly parsed: (Expected: %d / Was: %d)", ndotsVariants[data], cc.Ndots)
6060
}
6161
}
62+
}
63+
64+
func TestClientConfigFromReaderAttempts(t *testing.T) {
65+
testCases := []struct {
66+
data string
67+
expected int
68+
}{
69+
{data: "options attempts:0", expected: 1},
70+
{data: "options attempts:1", expected: 1},
71+
{data: "options attempts:15", expected: 15},
72+
{data: "options attempts:16", expected: 16},
73+
{data: "options attempts:-1", expected: 1},
74+
{data: "options attempt:", expected: 2},
75+
}
6276

77+
for _, test := range testCases {
78+
test := test
79+
t.Run(strings.Replace(test.data, ":", " ", -1), func(t *testing.T) {
80+
t.Parallel()
81+
82+
cc, err := ClientConfigFromReader(strings.NewReader(test.data))
83+
if err != nil {
84+
t.Errorf("error parsing resolv.conf: %v", err)
85+
}
86+
if cc.Attempts != test.expected {
87+
t.Errorf("A attempts not properly parsed: (Expected: %d / Was: %d)", test.expected, cc.Attempts)
88+
}
89+
})
90+
}
6391
}
6492

6593
func TestReadFromFile(t *testing.T) {

0 commit comments

Comments
 (0)