From 7f61c6631b70b2a18e0b61c825fa338d1dd0890e Mon Sep 17 00:00:00 2001 From: Tom Thorogood Date: Thu, 27 Sep 2018 04:02:05 +0930 Subject: [PATCH] Fix dominikh/go-tools nits (#758) * Remove unused functions and consts * Address gosimple nits * Address staticcheck nits This excludes several that were intentional or weren't actual errors. * Reduce size of lex struct This reduces the size of the lex struct by 8 bytes from: lex.token string: 0-16 (size 16, align 8) lex.tokenUpper string: 16-32 (size 16, align 8) lex.length int: 32-40 (size 8, align 8) lex.err bool: 40-41 (size 1, align 1) lex.value uint8: 41-42 (size 1, align 1) padding: 42-48 (size 6, align 0) lex.line int: 48-56 (size 8, align 8) lex.column int: 56-64 (size 8, align 8) lex.torc uint16: 64-66 (size 2, align 2) padding: 66-72 (size 6, align 0) lex.comment string: 72-88 (size 16, align 8) to: lex.token string: 0-16 (size 16, align 8) lex.tokenUpper string: 16-32 (size 16, align 8) lex.length int: 32-40 (size 8, align 8) lex.err bool: 40-41 (size 1, align 1) lex.value uint8: 41-42 (size 1, align 1) lex.torc uint16: 42-44 (size 2, align 2) padding: 44-48 (size 4, align 0) lex.line int: 48-56 (size 8, align 8) lex.column int: 56-64 (size 8, align 8) lex.comment string: 64-80 (size 16, align 8) * Reduce size of response struct This reduces the size of the response struct by 8 bytes from: response.msg []byte: 0-24 (size 24, align 8) response.hijacked bool: 24-25 (size 1, align 1) padding: 25-32 (size 7, align 0) response.tsigStatus error: 32-48 (size 16, align 8) response.tsigTimersOnly bool: 48-49 (size 1, align 1) padding: 49-56 (size 7, align 0) response.tsigRequestMAC string: 56-72 (size 16, align 8) response.tsigSecret map[string]string: 72-80 (size 8, align 8) response.udp *net.UDPConn: 80-88 (size 8, align 8) response.tcp net.Conn: 88-104 (size 16, align 8) response.udpSession *github.com/tmthrgd/dns.SessionUDP: 104-112 (size 8, align 8) response.writer github.com/tmthrgd/dns.Writer: 112-128 (size 16, align 8) response.wg *sync.WaitGroup: 128-136 (size 8, align 8) to: response.msg []byte: 0-24 (size 24, align 8) response.hijacked bool: 24-25 (size 1, align 1) response.tsigTimersOnly bool: 25-26 (size 1, align 1) padding: 26-32 (size 6, align 0) response.tsigStatus error: 32-48 (size 16, align 8) response.tsigRequestMAC string: 48-64 (size 16, align 8) response.tsigSecret map[string]string: 64-72 (size 8, align 8) response.udp *net.UDPConn: 72-80 (size 8, align 8) response.tcp net.Conn: 80-96 (size 16, align 8) response.udpSession *github.com/tmthrgd/dns.SessionUDP: 96-104 (size 8, align 8) response.writer github.com/tmthrgd/dns.Writer: 104-120 (size 16, align 8) response.wg *sync.WaitGroup: 120-128 (size 8, align 8) --- client.go | 2 +- dns_test.go | 8 +++----- dnssec_test.go | 12 ------------ msg.go | 6 ++---- msg_test.go | 5 +---- privaterr.go | 3 +-- scan.go | 7 ++----- scanner.go | 2 +- server.go | 2 +- sig0.go | 3 +-- udp_test.go | 7 ++++++- 11 files changed, 19 insertions(+), 38 deletions(-) diff --git a/client.go b/client.go index 7a319b02..63ced2bd 100644 --- a/client.go +++ b/client.go @@ -567,7 +567,7 @@ func (c *Client) ExchangeContext(ctx context.Context, m *Msg, a string) (r *Msg, if deadline, ok := ctx.Deadline(); !ok { timeout = 0 } else { - timeout = deadline.Sub(time.Now()) + timeout = time.Until(deadline) } // not passing the context to the underlying calls, as the API does not support // context. For timeouts you should set up Client.Dialer and call Client.Exchange. diff --git a/dns_test.go b/dns_test.go index e7b24c12..59ea56f1 100644 --- a/dns_test.go +++ b/dns_test.go @@ -10,8 +10,7 @@ import ( func TestPackUnpack(t *testing.T) { out := new(Msg) out.Answer = make([]RR, 1) - key := new(DNSKEY) - key = &DNSKEY{Flags: 257, Protocol: 3, Algorithm: RSASHA1} + key := &DNSKEY{Flags: 257, Protocol: 3, Algorithm: RSASHA1} key.Hdr = RR_Header{Name: "miek.nl.", Rrtype: TypeDNSKEY, Class: ClassINET, Ttl: 3600} key.PublicKey = "AwEAAaHIwpx3w4VHKi6i1LHnTaWeHCL154Jug0Rtc9ji5qwPXpBo6A5sRv7cSsPQKPIwxLpyCrbJ4mr2L0EPOdvP6z6YfljK2ZmTbogU9aSU2fiq/4wjxbdkLyoDVgtO+JsxNN4bjr4WcWhsmk1Hg93FV9ZpkWb0Tbad8DFqNDzr//kZ" @@ -25,8 +24,7 @@ func TestPackUnpack(t *testing.T) { t.Error("failed to unpack msg with DNSKEY") } - sig := new(RRSIG) - sig = &RRSIG{TypeCovered: TypeDNSKEY, Algorithm: RSASHA1, Labels: 2, + sig := &RRSIG{TypeCovered: TypeDNSKEY, Algorithm: RSASHA1, Labels: 2, OrigTtl: 3600, Expiration: 4000, Inception: 4000, KeyTag: 34641, SignerName: "miek.nl.", Signature: "AwEAAaHIwpx3w4VHKi6i1LHnTaWeHCL154Jug0Rtc9ji5qwPXpBo6A5sRv7cSsPQKPIwxLpyCrbJ4mr2L0EPOdvP6z6YfljK2ZmTbogU9aSU2fiq/4wjxbdkLyoDVgtO+JsxNN4bjr4WcWhsmk1Hg93FV9ZpkWb0Tbad8DFqNDzr//kZ"} sig.Hdr = RR_Header{Name: "miek.nl.", Rrtype: TypeRRSIG, Class: ClassINET, Ttl: 3600} @@ -293,7 +291,7 @@ func TestTKEY(t *testing.T) { if offset != len(tkeyBytes) { t.Fatalf("mismatched TKEY RR size %d != %d", len(tkeyBytes), offset) } - if bytes.Compare(tkeyBytes, msg[0:offset]) != 0 { + if !bytes.Equal(tkeyBytes, msg[0:offset]) { t.Fatal("mismatched TKEY data after rewriting bytes") } diff --git a/dnssec_test.go b/dnssec_test.go index 2dea4e50..eaea80c2 100644 --- a/dnssec_test.go +++ b/dnssec_test.go @@ -12,18 +12,6 @@ import ( "golang.org/x/crypto/ed25519" ) -func getKey() *DNSKEY { - key := new(DNSKEY) - key.Hdr.Name = "miek.nl." - key.Hdr.Class = ClassINET - key.Hdr.Ttl = 14400 - key.Flags = 256 - key.Protocol = 3 - key.Algorithm = RSASHA256 - key.PublicKey = "AwEAAcNEU67LJI5GEgF9QLNqLO1SMq1EdoQ6E9f85ha0k0ewQGCblyW2836GiVsm6k8Kr5ECIoMJ6fZWf3CQSQ9ycWfTyOHfmI3eQ/1Covhb2y4bAmL/07PhrL7ozWBW3wBfM335Ft9xjtXHPy7ztCbV9qZ4TVDTW/Iyg0PiwgoXVesz" - return key -} - func getSoa() *SOA { soa := new(SOA) soa.Hdr = RR_Header{"miek.nl.", TypeSOA, ClassINET, 14400, 0} diff --git a/msg.go b/msg.go index 282ea715..154b65ee 100644 --- a/msg.go +++ b/msg.go @@ -367,12 +367,10 @@ Loop: var buf [3]byte bufs := strconv.AppendInt(buf[:0], int64(b), 10) s = append(s, '\\') - for i := 0; i < 3-len(bufs); i++ { + for i := len(bufs); i < 3; i++ { s = append(s, '0') } - for _, r := range bufs { - s = append(s, r) - } + s = append(s, bufs...) // presentation-format \DDD escapes add 3 extra bytes maxLen += 3 } else { diff --git a/msg_test.go b/msg_test.go index afbe6d83..a9f516a0 100644 --- a/msg_test.go +++ b/msg_test.go @@ -8,10 +8,7 @@ import ( "testing" ) -const ( - maxPrintableLabel = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789x" - tooLongLabel = maxPrintableLabel + "x" -) +const maxPrintableLabel = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789x" var ( longDomain = maxPrintableLabel[:53] + strings.TrimSuffix( diff --git a/privaterr.go b/privaterr.go index 41989e7a..d931da7e 100644 --- a/privaterr.go +++ b/privaterr.go @@ -134,7 +134,7 @@ func PrivateHandle(rtypestr string, rtype uint16, generator func() PrivateRdata) typeToparserFunc[rtype] = parserFunc{setPrivateRR, true} } -// PrivateHandleRemove removes defenitions required to support private RR type. +// PrivateHandleRemove removes definitions required to support private RR type. func PrivateHandleRemove(rtype uint16) { rtypestr, ok := TypeToString[rtype] if ok { @@ -144,5 +144,4 @@ func PrivateHandleRemove(rtype uint16) { delete(StringToType, rtypestr) delete(typeToUnpack, rtype) } - return } diff --git a/scan.go b/scan.go index f9cd4740..a752dbd0 100644 --- a/scan.go +++ b/scan.go @@ -10,7 +10,6 @@ import ( ) 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; @@ -80,9 +79,9 @@ type lex struct { length int // length of the token err bool // when true, token text has lexer error value uint8 // value: zString, _BLANK, etc. + torc uint16 // type or class as parsed in the lexer, we only need to look this up in the grammar line int // line in the file column int // column in the file - torc uint16 // type or class as parsed in the lexer, we only need to look this up in the grammar comment string // any comment text seen } @@ -209,10 +208,9 @@ func parseZone(r io.Reader, origin, f string, defttl *ttlState, t chan *Token, i var prevName string for l := range c { // Lexer spotted an error already - if l.err == true { + if l.err { t <- &Token{Error: &ParseError{f, l.token, l}} return - } switch st { case zExpectOwnerDir: @@ -639,7 +637,6 @@ func zlexer(s *scan, c chan lex) { if quote { str[stri] = x stri++ - break } // discard if outside of quotes case '\n': diff --git a/scanner.go b/scanner.go index 424e5af9..5b124ec5 100644 --- a/scanner.go +++ b/scanner.go @@ -42,7 +42,7 @@ func (s *scan) tokenText() (byte, error) { // delay the newline handling until the next token is delivered, // fixes off-by-one errors when reporting a parse error. - if s.eof == true { + if s.eof { s.position.Line++ s.position.Column = 0 s.eof = false diff --git a/server.go b/server.go index e86a6595..b231c343 100644 --- a/server.go +++ b/server.go @@ -83,8 +83,8 @@ type ConnectionStater interface { type response struct { msg []byte hijacked bool // connection has been hijacked by handler - tsigStatus error tsigTimersOnly bool + tsigStatus error tsigRequestMAC string tsigSecret map[string]string // the tsig secrets udp *net.UDPConn // i/o connection if UDP was used diff --git a/sig0.go b/sig0.go index f31e9e68..07c2acb1 100644 --- a/sig0.go +++ b/sig0.go @@ -127,8 +127,7 @@ func (rr *SIG) Verify(k *KEY, buf []byte) error { if offset+1 >= buflen { continue } - var rdlen uint16 - rdlen = binary.BigEndian.Uint16(buf[offset:]) + rdlen := binary.BigEndian.Uint16(buf[offset:]) offset += 2 offset += int(rdlen) } diff --git a/udp_test.go b/udp_test.go index 22480261..5e2829cb 100644 --- a/udp_test.go +++ b/udp_test.go @@ -33,7 +33,8 @@ func TestSetUDPSocketOptions(t *testing.T) { b := make([]byte, 1) _, sess, err := ReadFromSessionUDP(c, b) if err != nil { - t.Fatalf("failed to read from conn: %v", err) + t.Errorf("failed to read from conn: %v", err) + // fallthrough to chan send below } ch <- sess }() @@ -46,6 +47,10 @@ func TestSetUDPSocketOptions(t *testing.T) { t.Fatalf("failed to write to conn: %v", err) } sess := <-ch + if sess == nil { + // t.Error was already called in the goroutine above. + t.FailNow() + } if len(sess.context) == 0 { t.Fatalf("empty session context: %v", sess) }