Skip to content

Commit 186ccfb

Browse files
EricIOEric Skoglund
andauthored
Annotate unpack* function errors with where the error happened. (#1590)
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. Co-authored-by: Eric Skoglund <[email protected]>
1 parent 4669827 commit 186ccfb

File tree

3 files changed

+236
-223
lines changed

3 files changed

+236
-223
lines changed

msg.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ func (dns *Msg) unpack(dh Header, msg []byte, off int) (err error) {
872872
// TODO(miek) make this an error?
873873
// use PackOpt to let people tell how detailed the error reporting should be?
874874
// if off != len(msg) {
875-
// // println("dns: extra bytes in dns packet", off, "<", len(msg))
875+
// // println("dns: extra bytes in dns packet", off, "<", len(msg))
876876
// }
877877
return err
878878
}
@@ -1123,23 +1123,28 @@ func unpackQuestion(msg []byte, off int) (Question, int, error) {
11231123
)
11241124
q.Name, off, err = UnpackDomainName(msg, off)
11251125
if err != nil {
1126-
return q, off, err
1126+
return q, off, fmt.Errorf("question.Name: %w", err)
11271127
}
11281128
if off == len(msg) {
11291129
return q, off, nil
11301130
}
11311131
q.Qtype, off, err = unpackUint16(msg, off)
11321132
if err != nil {
1133-
return q, off, err
1133+
return q, off, fmt.Errorf("question.Qtype: %w", err)
11341134
}
11351135
if off == len(msg) {
11361136
return q, off, nil
11371137
}
11381138
q.Qclass, off, err = unpackUint16(msg, off)
1139+
if err != nil {
1140+
return q, off, fmt.Errorf("question.Qclass: %w", err)
1141+
}
1142+
11391143
if off == len(msg) {
11401144
return q, off, nil
11411145
}
1142-
return q, off, err
1146+
1147+
return q, off, nil
11431148
}
11441149

11451150
func (dh *Header) pack(msg []byte, off int, compression compressionMap, compress bool) (int, error) {
@@ -1177,27 +1182,27 @@ func unpackMsgHdr(msg []byte, off int) (Header, int, error) {
11771182
)
11781183
dh.Id, off, err = unpackUint16(msg, off)
11791184
if err != nil {
1180-
return dh, off, err
1185+
return dh, off, fmt.Errorf("header.Id: %w", err)
11811186
}
11821187
dh.Bits, off, err = unpackUint16(msg, off)
11831188
if err != nil {
1184-
return dh, off, err
1189+
return dh, off, fmt.Errorf("header.Bits: %w", err)
11851190
}
11861191
dh.Qdcount, off, err = unpackUint16(msg, off)
11871192
if err != nil {
1188-
return dh, off, err
1193+
return dh, off, fmt.Errorf("header.Qdcount: %w", err)
11891194
}
11901195
dh.Ancount, off, err = unpackUint16(msg, off)
11911196
if err != nil {
1192-
return dh, off, err
1197+
return dh, off, fmt.Errorf("header.Ancount: %w", err)
11931198
}
11941199
dh.Nscount, off, err = unpackUint16(msg, off)
11951200
if err != nil {
1196-
return dh, off, err
1201+
return dh, off, fmt.Errorf("header.Nscount: %w", err)
11971202
}
11981203
dh.Arcount, off, err = unpackUint16(msg, off)
11991204
if err != nil {
1200-
return dh, off, err
1205+
return dh, off, fmt.Errorf("header.Arcount: %w", err)
12011206
}
12021207
return dh, off, nil
12031208
}

msg_generate.go

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)