From df6d86e937ab52e9f8e37129628524e7579e8cbd Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Sat, 3 Mar 2012 17:40:30 +0100 Subject: [PATCH] Fix for when not seeing an RR at all --- parse_test.go | 21 +++++++++++++++++++++ zscan.go | 11 +++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/parse_test.go b/parse_test.go index 69de738f..7e211949 100644 --- a/parse_test.go +++ b/parse_test.go @@ -426,3 +426,24 @@ func ExampleSOA() { fmt.Printf("%s\n", soa.String()) } } + +func TestLineNumberError(t *testing.T) { + s := "example.com. 1000 SOA master.example.com. admin.example.com. monkey 4294967294 4294967293 4294967295 100" + if _, err := NewRR(s); err != nil { + if err.Error() != "dns: bad SOA zone parameter: \"monkey\" at line: 1:68" { + t.Logf("Not expecting this error: " + err.Error()) + t.Fail() + } + } +} + +// Test with no known RR on the line +func TestLineNumberError2(t *testing.T) { + s := "example.com. 1000 SO master.example.com. admin.example.com. 1 4294967294 4294967293 4294967295 100" + _, err := NewRR(s) + if err == nil { + t.Fail() + } else { +// fmt.Printf("%s\n", err.Error()) + } +} diff --git a/zscan.go b/zscan.go index 4d03c11d..9fafeb20 100644 --- a/zscan.go +++ b/zscan.go @@ -23,7 +23,7 @@ const maxTok = 2048 // * Handle braces. const ( // Zonefile - _EOF = iota // Don't let it start with zero + _EOF = iota _STRING _BLANK _QUOTE @@ -70,7 +70,7 @@ func (e *ParseError) Error() (s string) { if e.file != "" { s = e.file + ": " } - s += "dns:" + e.err + ": `" + e.lex.token + "' at line: " + + s += "dns: " + e.err + ": " + strconv.QuoteToASCII(e.lex.token) + " at line: " + strconv.Itoa(e.lex.line) + ":" + strconv.Itoa(e.lex.column) return } @@ -343,6 +343,9 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { case _RRTYPE: h.Rrtype = l.torc st = _EXPECT_RDATA + case _NEWLINE: + t <- Token{Error: &ParseError{f, "premature newline, no RR type seen", l}} + return } case _EXPECT_ANY_NOCLASS: switch l.value { @@ -391,6 +394,10 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { st = _EXPECT_OWNER_DIR } } + // If we get here, we and the h.Rrtype is still zero, we haven't parsed anything + if h.Rrtype == 0 { + t <- Token{Error: &ParseError{f, "nothing made sense", lex{}}} + } } func (l lex) String() string {