From 1ff265a78454967a4321dc2c0e38e267323bc3d4 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Tue, 27 Nov 2018 14:26:11 +0000 Subject: [PATCH] Remove ErrTruncated from the library (#815) * Remove ErrTruncated from the library ErrTruncated is removed. This (correctly) assume that a truncated message will be fully formed. Any message that isn't fully formed will return (most likely) an unpack error. Any program using ErrTruncated will fail to compile when they update to this version: this is by design: you're doing it wrong. For checking if a message was truncated you should checked the msg.Truncated boolean; assuming the unpack didn't fail. Fixes #814 Signed-off-by: Miek Gieben * Restore tests Signed-off-by: Miek Gieben --- client_test.go | 19 ++++++++++--------- msg.go | 13 +++---------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/client_test.go b/client_test.go index 6b1d787b..edfd6b1d 100644 --- a/client_test.go +++ b/client_test.go @@ -373,12 +373,12 @@ func TestTruncatedMsg(t *testing.T) { m.Truncated = true buf, err = m.Pack() if err != nil { - t.Errorf("failed to pack truncated: %v", err) + t.Errorf("failed to pack truncated message: %v", err) } r = new(Msg) - if err = r.Unpack(buf); err != nil && err != ErrTruncated { - t.Errorf("unable to unpack truncated message: %v", err) + if err = r.Unpack(buf); err != nil { + t.Errorf("failed to unpack truncated message: %v", err) } if !r.Truncated { t.Errorf("truncated message wasn't unpacked as truncated") @@ -407,9 +407,10 @@ func TestTruncatedMsg(t *testing.T) { buf1 = buf[:len(buf)-off] r = new(Msg) - if err = r.Unpack(buf1); err != nil && err != ErrTruncated { - t.Errorf("unable to unpack cutoff message: %v", err) + if err = r.Unpack(buf1); err == nil { + t.Error("cutoff message should have failed to unpack") } + // r's header might be still usable. if !r.Truncated { t.Error("truncated cutoff message wasn't unpacked as truncated") } @@ -438,8 +439,8 @@ func TestTruncatedMsg(t *testing.T) { buf1 = buf[:len(buf)-off] r = new(Msg) - if err = r.Unpack(buf1); err != nil && err != ErrTruncated { - t.Errorf("unable to unpack cutoff message: %v", err) + if err = r.Unpack(buf1); err == nil { + t.Error("cutoff message should have failed to unpack") } if !r.Truncated { t.Error("truncated cutoff message wasn't unpacked as truncated") @@ -454,8 +455,8 @@ func TestTruncatedMsg(t *testing.T) { r = new(Msg) err = r.Unpack(buf1) - if err == nil || err == ErrTruncated { - t.Errorf("error should not be ErrTruncated from question cutoff unpack: %v", err) + if err == nil { + t.Errorf("error should be nil after question cutoff unpack: %v", err) } // Finally, if we only have the header, we don't return an error. diff --git a/msg.go b/msg.go index b1367c1f..798e15e9 100644 --- a/msg.go +++ b/msg.go @@ -46,10 +46,9 @@ var ( ErrRRset error = &Error{err: "bad rrset"} ErrSecret error = &Error{err: "no secrets defined"} ErrShortRead error = &Error{err: "short read"} - ErrSig error = &Error{err: "bad signature"} // ErrSig indicates that a signature can not be cryptographically validated. - ErrSoa error = &Error{err: "no SOA"} // ErrSOA indicates that no SOA RR was seen when doing zone transfers. - ErrTime error = &Error{err: "bad time"} // ErrTime indicates a timing error in TSIG authentication. - ErrTruncated error = &Error{err: "failed to unpack truncated message"} // ErrTruncated indicates that we failed to unpack a truncated message. We unpacked as much as we had so Msg can still be used, if desired. + ErrSig error = &Error{err: "bad signature"} // ErrSig indicates that a signature can not be cryptographically validated. + ErrSoa error = &Error{err: "no SOA"} // ErrSOA indicates that no SOA RR was seen when doing zone transfers. + ErrTime error = &Error{err: "bad time"} // ErrTime indicates a timing error in TSIG authentication. ) // Id by default, returns a 16 bits random number to be used as a @@ -813,8 +812,6 @@ func (dns *Msg) Unpack(msg []byte) (err error) { var q Question q, off, err = unpackQuestion(msg, off) if err != nil { - // Even if Truncated is set, we only will set ErrTruncated if we - // actually got the questions return err } if off1 == off { // Offset does not increase anymore, dh.Qdcount is a lie! @@ -847,10 +844,6 @@ func (dns *Msg) Unpack(msg []byte) (err error) { // TODO(miek) make this an error? // use PackOpt to let people tell how detailed the error reporting should be? // println("dns: extra bytes in dns packet", off, "<", len(msg)) - } else if dns.Truncated { - // Whether we ran into a an error or not, we want to return that it - // was truncated - err = ErrTruncated } return err }