Correctly parse files without trailing newline

If the file did not have a newline character as the last character,
parseKV() would panic with an NPE. Handle the parser changes better.

Fixes #21.
This commit is contained in:
Kevin Burke
2018-08-30 12:02:32 -06:00
parent 82cf3f9264
commit 555f37af0a
3 changed files with 22 additions and 0 deletions

View File

@@ -362,3 +362,19 @@ func TestDosLinesEndingsDecode(t *testing.T) {
t.Errorf("expected HostName to be %q, got %q", "8.8.8.8", host) t.Errorf("expected HostName to be %q, got %q", "8.8.8.8", host)
} }
} }
func TestNoTrailingNewline(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/config-no-ending-newline"),
systemConfigFinder: nullConfigFinder,
}
port, err := us.GetStrict("example", "Port")
if err != nil {
t.Fatal(err)
}
if port != "4242" {
t.Errorf("wrong port: got %q want 4242", port)
}
}

View File

@@ -96,6 +96,9 @@ func (p *sshParser) parseKV() sshParserStateFn {
} }
comment := "" comment := ""
tok := p.peek() tok := p.peek()
if tok == nil {
tok = &token{typ: tokenEOF}
}
if tok.typ == tokenComment && tok.Position.Line == val.Position.Line { if tok.typ == tokenComment && tok.Position.Line == val.Position.Line {
tok = p.getToken() tok = p.getToken()
comment = tok.val comment = tok.val

3
testdata/config-no-ending-newline vendored Normal file
View File

@@ -0,0 +1,3 @@
Host example
HostName example.com
Port 4242