Skip to content

Commit b0b1924

Browse files
authored
Fix descriptor panic (#53)
* Provide fuzzer for descriptor * Don't panic when the length of a descriptor is 0
1 parent 5a86eb1 commit b0b1924

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

descriptor.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,10 @@ func calcDescriptorLength(d *Descriptor) uint8 {
20022002
return calcDescriptorUserDefinedLength(d.UserDefined)
20032003
}
20042004

2005+
if d.Length == 0 {
2006+
return 0
2007+
}
2008+
20052009
switch d.Tag {
20062010
case DescriptorTagAC3:
20072011
return calcDescriptorAC3Length(d.AC3)
@@ -2068,6 +2072,10 @@ func writeDescriptor(w *astikit.BitsWriter, d *Descriptor) (int, error) {
20682072

20692073
written := int(length) + 2
20702074

2075+
if d.Length == 0 {
2076+
return written, nil
2077+
}
2078+
20712079
if d.Tag >= 0x80 && d.Tag <= 0xfe {
20722080
return written, writeDescriptorUserDefined(w, d.UserDefined)
20732081
}

descriptor_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package astits
22

33
import (
44
"bytes"
5+
"testing"
6+
57
"github.com/asticode/go-astikit"
68
"github.com/stretchr/testify/assert"
7-
"testing"
89
)
910

1011
var descriptors = []*Descriptor{{
@@ -703,3 +704,31 @@ func BenchmarkParseDescriptor(b *testing.B) {
703704
})
704705
}
705706
}
707+
708+
func FuzzDescriptor(f *testing.F) {
709+
bufExpected := bytes.Buffer{}
710+
bufExpected.Write([]byte{0x00, 0x00}) // reserve two bytes for length
711+
wExpected := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufExpected})
712+
713+
for _, tc := range descriptorTestTable {
714+
tc.bytesFunc(wExpected)
715+
}
716+
717+
descLen := uint16(bufExpected.Len() - 2)
718+
descBytes := bufExpected.Bytes()
719+
descBytes[0] = byte(descLen>>8) | 0b11110000 // program_info_length is preceded by 4 reserved bits
720+
descBytes[1] = byte(descLen & 0xff)
721+
722+
f.Add(descBytes)
723+
724+
f.Fuzz(func(t *testing.T, b []byte) {
725+
ds, err := parseDescriptors(astikit.NewBytesIterator(b))
726+
727+
if err == nil {
728+
bufActual := bytes.Buffer{}
729+
wActual := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufActual})
730+
731+
writeDescriptorsWithLength(wActual, ds)
732+
}
733+
})
734+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
[]byte("0\x060\x02000\x00")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
[]byte("0\x060\x0200X\x00")

0 commit comments

Comments
 (0)