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 <miek@miek.nl>

* Restore tests

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben 2018-11-27 14:26:11 +00:00 committed by GitHub
parent 8f269a6b16
commit 1ff265a784
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 19 deletions

View File

@ -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.

13
msg.go
View File

@ -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
}