Skip to content

Commit

Permalink
Fix H264Reader buffer overflow
Browse files Browse the repository at this point in the history
Fixes #1734
  • Loading branch information
Antonito committed Jun 28, 2021
1 parent 1ba27d8 commit 299e04a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pkg/media/h264reader/h264reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ func (reader *H264Reader) processByte(readByte byte) (nalFound bool) {
countOfConsecutiveZeroBytesInPrefix = 3
}
nalUnitLength := len(reader.nalBuffer) - countOfConsecutiveZeroBytesInPrefix
reader.nalBuffer = reader.nalBuffer[0:nalUnitLength]
nalFound = true
if nalUnitLength > 0 {
reader.nalBuffer = reader.nalBuffer[0:nalUnitLength]
nalFound = true
}
} else {
reader.countOfConsecutiveZeroBytes = 0
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/media/h264reader/h264reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,24 @@ func TestSkipSEI(t *testing.T) {
assert.Nil(err)
assert.Equal(byte(0xAB), nal.Data[0])
}

func TestIssue1734_NextNal(t *testing.T) {
tt := [...][]byte{
[]byte("\x00\x00\x010\x00\x00\x01\x00\x00\x01"),
[]byte("\x00\x00\x00\x01\x00\x00\x01"),
}

for _, cur := range tt {
r, err := NewReader(bytes.NewReader(cur))
assert.NoError(t, err)

// Just make sure it doesn't crash
for {
nal, err := r.NextNAL()

if err != nil || nal == nil {
break
}
}
}
}

0 comments on commit 299e04a

Please sign in to comment.