From 186871d2a9b95ae61baecde8c8ec901ba2b96dba Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 5 Dec 2013 09:54:46 +0000 Subject: [PATCH] Add check for > maxUint16 sizes TYPEXXXX or CLASSXXX These were silently wrapped in a uint16, nicer to actually give an error. --- parse_test.go | 19 ++++++++++++------- zscan.go | 5 +++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/parse_test.go b/parse_test.go index 0c89e85d..d4816c0e 100644 --- a/parse_test.go +++ b/parse_test.go @@ -741,15 +741,20 @@ func TestTXT(t *testing.T) { } func TestRR(t *testing.T) { - rr, err := NewRR("example.com IN TYPE1234 \\# 4 aabbccdd") - if err == nil { - t.Logf("%s\n", rr.String()) - } else { - t.Error("Failed to parse TYPE1234 RR: ", err.Error()) + _, err := NewRR("example.com IN TYPE1234 \\# 4 aabbccdd") + if err != nil { + t.Logf("Failed to parse TYPE1234 RR: ", err.Error()) + t.Fail() } - rr, err = NewRR("example.com IN TYPE1 \\# 4 0a000001") + _, err = NewRR("example.com IN TYPE655341 \\# 8 aabbccddaabbccdd") if err == nil { - t.Error("This should not work") + t.Logf("This should not work, for TYPE655341") + t.Fail() + } + _, err = NewRR("example.com IN TYPE1 \\# 4 0a000001") + if err == nil { + t.Logf("This should not work") + t.Fail() } } diff --git a/zscan.go b/zscan.go index 109d0fd7..fd4ecc1c 100644 --- a/zscan.go +++ b/zscan.go @@ -23,6 +23,7 @@ func (d debugging) Printf(format string, args ...interface{}) { } const maxTok = 2048 // Largest token we can return. +const maxUint16 = 1<<16 - 1 // Tokinize a RFC 1035 zone file. The tokenizer will normalize it: // * Add ownernames if they are left blank; @@ -789,7 +790,7 @@ func zlexer(s *scan, c chan lex) { // Extract the class number from CLASSxx func classToInt(token string) (uint16, bool) { class, ok := strconv.Atoi(token[5:]) - if ok != nil { + if ok != nil || class > maxUint16 { return 0, false } return uint16(class), true @@ -798,7 +799,7 @@ func classToInt(token string) (uint16, bool) { // Extract the rr number from TYPExxx func typeToInt(token string) (uint16, bool) { typ, ok := strconv.Atoi(token[4:]) - if ok != nil { + if ok != nil || typ > maxUint16 { return 0, false } return uint16(typ), true