Skip to content

Commit

Permalink
Fix SIGSEGV when calling Find on empty Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean-Der committed Apr 16, 2024
1 parent 892c5ee commit 1449b4f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
8 changes: 0 additions & 8 deletions pkg/jitterbuffer/priority_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ func newNode(val *rtp.Packet, priority uint16) *node {
// Find a packet in the queue with the provided sequence number,
// regardless of position (the packet is retained in the queue)
func (q *PriorityQueue) Find(sqNum uint16) (*rtp.Packet, error) {
if q.next.priority == sqNum {
return q.next.val, nil
}

if sqNum < q.next.priority {
return nil, ErrInvalidOperation
}

next := q.next
for next != nil {
if next.priority == sqNum {
Expand Down
24 changes: 24 additions & 0 deletions pkg/jitterbuffer/priority_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

func TestPriorityQueue(t *testing.T) {
assert := assert.New(t)

t.Run("Appends packets in order", func(*testing.T) {
pkt := &rtp.Packet{Header: rtp.Header{SequenceNumber: 5000, Timestamp: 500}, Payload: []byte{0x02}}
q := NewQueue()
Expand All @@ -22,6 +23,7 @@ func TestPriorityQueue(t *testing.T) {
assert.Equal(q.next.priority, uint16(5000))
assert.Equal(q.next.next.priority, uint16(5004))
})

t.Run("Appends many in order", func(*testing.T) {
q := NewQueue()
for i := 0; i < 100; i++ {
Expand All @@ -40,6 +42,7 @@ func TestPriorityQueue(t *testing.T) {
assert.Equal(q.next.priority, uint16(5012))
assert.Equal(last.priority, uint16(5012+99))
})

t.Run("Can remove an element", func(*testing.T) {
pkt := &rtp.Packet{Header: rtp.Header{SequenceNumber: 5000, Timestamp: 500}, Payload: []byte{0x02}}
q := NewQueue()
Expand All @@ -55,6 +58,7 @@ func TestPriorityQueue(t *testing.T) {
nextPop, _ := q.Pop()
assert.Equal(nextPop.SequenceNumber, uint16(5012))
})

t.Run("Appends in order", func(*testing.T) {
q := NewQueue()
for i := 0; i < 100; i++ {
Expand All @@ -67,6 +71,7 @@ func TestPriorityQueue(t *testing.T) {
assert.Equal(uint16(101), q.Length())
assert.Equal(q.next.priority, uint16(5000))
})

t.Run("Can find", func(*testing.T) {
q := NewQueue()
for i := 0; i < 100; i++ {
Expand All @@ -77,3 +82,22 @@ func TestPriorityQueue(t *testing.T) {
assert.Equal(err, nil)
})
}

func TestPriorityQueue_Find(t *testing.T) {
packets := NewQueue()

packets.Push(&rtp.Packet{
Header: rtp.Header{
SequenceNumber: 1000,
Timestamp: 5,
SSRC: 5,
},
Payload: []uint8{0xA},
}, 1000)

_, err := packets.PopAt(1000)
assert.NoError(t, err)

_, err = packets.Find(1001)
assert.Error(t, err)
}

0 comments on commit 1449b4f

Please sign in to comment.