Skip to content

Commit

Permalink
Avoid using a goto loop in unpackDataOpt (#1505)
Browse files Browse the repository at this point in the history
We definitely shouldn't be using goto for a simple loop.

This is technically a behaviour change when off == len(msg), but we're
always called with off < len(msg) so this is unobservable.
  • Loading branch information
tmthrgd authored Nov 6, 2023
1 parent 1c418a3 commit 3996b17
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions msg_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,29 +406,24 @@ func packStringTxt(s []string, msg []byte, off int) (int, error) {

func unpackDataOpt(msg []byte, off int) ([]EDNS0, int, error) {
var edns []EDNS0
Option:
var code uint16
if off+4 > len(msg) {
return nil, len(msg), &Error{err: "overflow unpacking opt"}
}
code = binary.BigEndian.Uint16(msg[off:])
off += 2
optlen := binary.BigEndian.Uint16(msg[off:])
off += 2
if off+int(optlen) > len(msg) {
return nil, len(msg), &Error{err: "overflow unpacking opt"}
}
e := makeDataOpt(code)
if err := e.unpack(msg[off : off+int(optlen)]); err != nil {
return nil, len(msg), err
}
edns = append(edns, e)
off += int(optlen)

if off < len(msg) {
goto Option
for off < len(msg) {
if off+4 > len(msg) {
return nil, len(msg), &Error{err: "overflow unpacking opt"}
}
code := binary.BigEndian.Uint16(msg[off:])
off += 2
optlen := binary.BigEndian.Uint16(msg[off:])
off += 2
if off+int(optlen) > len(msg) {
return nil, len(msg), &Error{err: "overflow unpacking opt"}
}
opt := makeDataOpt(code)
if err := opt.unpack(msg[off : off+int(optlen)]); err != nil {
return nil, len(msg), err
}
edns = append(edns, opt)
off += int(optlen)
}

return edns, off, nil
}

Expand Down

0 comments on commit 3996b17

Please sign in to comment.