Avoid crash in samplebuilder.PopWithTimestamp

If sample is null, PopWithTimestamp will crash with SIGSEGV.
This commit is contained in:
Juliusz Chroboczek
2021-06-20 19:52:21 +02:00
committed by Sean DuBois
parent 41243cd179
commit 11f6a65fe2
2 changed files with 12 additions and 0 deletions

View File

@@ -300,6 +300,9 @@ func (s *SampleBuilder) Pop() *media.Sample {
// no sample is compiled). // no sample is compiled).
func (s *SampleBuilder) PopWithTimestamp() (*media.Sample, uint32) { func (s *SampleBuilder) PopWithTimestamp() (*media.Sample, uint32) {
sample := s.Pop() sample := s.Pop()
if sample == nil {
return nil, 0
}
return sample, sample.PacketTimestamp return sample, sample.PacketTimestamp
} }

View File

@@ -379,3 +379,12 @@ func TestSampleBuilderWithPacketReleaseHandler(t *testing.T) {
t.Errorf("Unexpected packet released by samples built") t.Errorf("Unexpected packet released by samples built")
} }
} }
func TestPopWithTimestamp(t *testing.T) {
t.Run("Crash on nil", func(t *testing.T) {
s := New(0, &fakeDepacketizer{}, 1)
sample, timestamp := s.PopWithTimestamp()
assert.Nil(t, sample)
assert.Equal(t, uint32(0), timestamp)
})
}