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 6, 2024
1 parent e5a40bc commit da17676
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 222 deletions.
18 changes: 9 additions & 9 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1123,14 +1123,14 @@ 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
Expand All @@ -1139,7 +1139,7 @@ func unpackQuestion(msg []byte, off int) (Question, int, error) {
if off == len(msg) {
return q, off, nil
}
return q, off, err
return q, off, fmt.Errorf("question.Qclass: %w", err)
}

func (dh *Header) pack(msg []byte, off int, compression compressionMap, compress bool) (int, error) {
Expand Down Expand Up @@ -1177,27 +1177,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 da17676

Please sign in to comment.