Skip to content

Commit

Permalink
Annotate unpack* function errors with where the error happened.
Browse files Browse the repository at this point in the history
This commit adds annotations to the error message when an error occurs
during unpacking of a DNS message, the annotation will give detiail on
where in the DNS message the unpacking error occured at. This helps to
improve the debugability of such errors.
  • Loading branch information
Eric Skoglund committed Aug 15, 2024
1 parent e5a40bc commit 8ba105b
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 223 deletions.
25 changes: 15 additions & 10 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ func (dns *Msg) unpack(dh Header, msg []byte, off int) (err error) {
// TODO(miek) make this an error?
// use PackOpt to let people tell how detailed the error reporting should be?
// if off != len(msg) {
// // println("dns: extra bytes in dns packet", off, "<", len(msg))
// // println("dns: extra bytes in dns packet", off, "<", len(msg))
// }
return err

Expand Down Expand Up @@ -1123,23 +1123,28 @@ func unpackQuestion(msg []byte, off int) (Question, int, error) {
)
q.Name, off, err = UnpackDomainName(msg, off)
if err != nil {
return q, off, err
return q, off, fmt.Errorf("question.Name: %w", err)
}
if off == len(msg) {
return q, off, nil
}
q.Qtype, off, err = unpackUint16(msg, off)
if err != nil {
return q, off, err
return q, off, fmt.Errorf("question.Qtype: %w", err)
}
if off == len(msg) {
return q, off, nil
}
q.Qclass, off, err = unpackUint16(msg, off)
if err != nil {
return q, off, fmt.Errorf("question.Qclass: %w", err)
}

if off == len(msg) {
return q, off, nil
}
return q, off, err

return q, off, nil
}

func (dh *Header) pack(msg []byte, off int, compression compressionMap, compress bool) (int, error) {
Expand Down Expand Up @@ -1177,27 +1182,27 @@ func unpackMsgHdr(msg []byte, off int) (Header, int, error) {
)
dh.Id, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
return dh, off, fmt.Errorf("header.Id: %w", err)
}
dh.Bits, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
return dh, off, fmt.Errorf("header.Bits: %w", err)
}
dh.Qdcount, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
return dh, off, fmt.Errorf("header.Qdcount: %w", err)
}
dh.Ancount, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
return dh, off, fmt.Errorf("header.Ancount: %w", err)
}
dh.Nscount, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
return dh, off, fmt.Errorf("header.Nscount: %w", err)
}
dh.Arcount, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
return dh, off, fmt.Errorf("header.Arcount: %w", err)
}
return dh, off, nil
}
Expand Down
12 changes: 9 additions & 3 deletions msg_generate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8ba105b

Please sign in to comment.