Skip to content

Commit c735660

Browse files
authored
h264: prevent Annex-B from decoding more NALUs than MaxNALUsPerAccessUnit (#303)
1 parent 744002a commit c735660

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

pkg/codecs/h264/annexb.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ func (a *AnnexB) Unmarshal(buf []byte) error {
8484
return ErrAnnexBNoInitialDelimiter
8585
}
8686

87+
if count > MaxNALUsPerAccessUnit {
88+
return fmt.Errorf("NALU count (%d) exceeds maximum allowed (%d)",
89+
count, MaxNALUsPerAccessUnit)
90+
}
91+
8792
*a = make([][]byte, 0, count)
8893
i := 0
8994
start := 0

pkg/codecs/h264/annexb_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ func TestAnnexBUnmarshalEmpty(t *testing.T) {
121121
require.Equal(t, AnnexB{{1}}, dec)
122122
}
123123

124+
func TestAnnexBUnmarshalExceedsMaxNALUs(t *testing.T) {
125+
buf := make([]byte, 0, 5*51)
126+
for range 51 {
127+
buf = append(buf, 0x00, 0x00, 0x00, 0x01, 0xAA)
128+
}
129+
130+
var dec AnnexB
131+
err := dec.Unmarshal(buf)
132+
require.EqualError(t, err, "NALU count (51) exceeds maximum allowed (50)")
133+
}
134+
124135
func TestAnnexBMarshal(t *testing.T) {
125136
for _, ca := range casesAnnexB {
126137
t.Run(ca.name, func(t *testing.T) {

pkg/codecs/h264/avcc_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ func TestAVCCUnmarshalEmpty(t *testing.T) {
7070
}, dec)
7171
}
7272

73+
func TestAVCCUnmarshalExceedsMaxNALUs(t *testing.T) {
74+
buf := make([]byte, 0, 5*51)
75+
for range 51 {
76+
buf = append(buf, 0x00, 0x00, 0x00, 0x01, 0xAA)
77+
}
78+
79+
var dec AVCC
80+
err := dec.Unmarshal(buf)
81+
require.EqualError(t, err, "NALU count (51) exceeds maximum allowed (50)")
82+
}
83+
7384
func TestAVCCMarshal(t *testing.T) {
7485
for _, ca := range casesAVCC {
7586
t.Run(ca.name, func(t *testing.T) {

0 commit comments

Comments
 (0)