Fix potential index out of range error

I had some extra spaces and junk in my ssh config and it was causing
this part of the code to fail with an out of range error.
This commit is contained in:
Sergey Lukjanov
2018-01-27 11:46:18 -08:00
committed by Kevin Burke
parent 802051befe
commit c665f6f442
3 changed files with 21 additions and 5 deletions
+14
View File
@@ -324,3 +324,17 @@ func TestMatchUnsupported(t *testing.T) {
t.Errorf("wrong error: %v", err)
}
}
func TestIndexInRange(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/config4"),
}
user, err := us.GetStrict("wap", "User")
if err != nil {
t.Fatal(err)
}
if user != "root" {
t.Errorf("expected User to be %q, got %q", "root", user)
}
}
+3 -5
View File
@@ -107,19 +107,17 @@ func (p *sshParser) parseKV() sshParserStateFn {
}
if strings.ToLower(key.val) == "host" {
strPatterns := strings.Split(val.val, " ")
patterns := make([]*Pattern, 0)
for i := range strPatterns {
if strPatterns[i] == "" {
strPatterns = append(strPatterns[:i], strPatterns[i+1:]...)
continue
}
}
patterns := make([]*Pattern, len(strPatterns))
for i := range strPatterns {
pat, err := NewPattern(strPatterns[i])
if err != nil {
p.raiseErrorf(val, "Invalid host pattern: %v", err)
return nil
}
patterns[i] = pat
patterns = append(patterns, pat)
}
p.config.Hosts = append(p.config.Hosts, &Host{
Patterns: patterns,
+4
View File
@@ -0,0 +1,4 @@
# Extra space at end of line is important.
Host wap
User root
KexAlgorithms diffie-hellman-group1-sha1