Skip to content

Commit

Permalink
fix(buffer): Solve OOR on old packet closes #364
Browse files Browse the repository at this point in the history
  • Loading branch information
OrlandoCo committed Jan 5, 2021
1 parent 7098465 commit 457db65
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions pkg/buffer/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package buffer

import (
"encoding/binary"
"math"

"github.com/pion/rtcp"
)
Expand All @@ -22,7 +23,7 @@ type Bucket struct {
func NewBucket(size int, nack bool) *Bucket {
b := &Bucket{
buf: make([]byte, size),
maxSteps: size/maxPktSize - 1,
maxSteps: int(math.Floor(float64(size)/float64(maxPktSize))) - 1,
}
if nack {
b.nacker = newNACKQueue()
Expand Down Expand Up @@ -109,7 +110,7 @@ func (b *Bucket) set(sn uint16, pkt []byte) []byte {
pos = b.maxSteps + pos + 1
}
off := pos * maxPktSize
if off > len(b.buf) {
if off > len(b.buf) || off < 0 {
return nil
}
binary.BigEndian.PutUint16(b.buf[off:], uint16(len(pkt)))
Expand All @@ -118,12 +119,10 @@ func (b *Bucket) set(sn uint16, pkt []byte) []byte {
}

func (b *Bucket) reset() {
if b.headSN != 0 {
b.headSN = 0
b.step = 0
b.onLost = nil
if b.nacker != nil {
b.nacker.reset()
}
b.headSN = 0
b.step = 0
b.onLost = nil
if b.nacker != nil {
b.nacker.reset()
}
}

0 comments on commit 457db65

Please sign in to comment.