Skip to content

Commit

Permalink
Merge pull request #4 from imsodin/master
Browse files Browse the repository at this point in the history
check for error before resizing slice on read
  • Loading branch information
AudriusButkevicius authored May 11, 2021
2 parents 763491a + d535b60 commit e9aaf99
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,27 @@ func (d *PacketFilter) readFrom() packet {
}
}

var errUnexpectedNegativeLength = errors.New("ReadMsgUDP returned a negative number of read bytes")

func (d *PacketFilter) readMsgUdp() packet {
buf := d.bufPool.Get().([]byte)
oobBuf := d.bufPool.Get().([]byte)
n, oobn, flags, addr, err := d.oobConn.ReadMsgUDP(buf, oobBuf)

// This is entirely unexpected, but happens in the wild
if n < 0 {
if err == nil {
err = errUnexpectedNegativeLength
}
n = 0
}
if oobn < 0 {
if err == nil {
err = errUnexpectedNegativeLength
}
oobn = 0
}

return packet{
n: n,
oobn: oobn,
Expand Down

0 comments on commit e9aaf99

Please sign in to comment.