Skip to content

Commit

Permalink
Fix descriptor panic (#53)
Browse files Browse the repository at this point in the history
* Provide fuzzer for descriptor

* Don't panic when the length of a descriptor is 0
  • Loading branch information
eric authored Aug 8, 2023
1 parent 5a86eb1 commit b0b1924
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
8 changes: 8 additions & 0 deletions descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,10 @@ func calcDescriptorLength(d *Descriptor) uint8 {
return calcDescriptorUserDefinedLength(d.UserDefined)
}

if d.Length == 0 {
return 0
}

switch d.Tag {
case DescriptorTagAC3:
return calcDescriptorAC3Length(d.AC3)
Expand Down Expand Up @@ -2068,6 +2072,10 @@ func writeDescriptor(w *astikit.BitsWriter, d *Descriptor) (int, error) {

written := int(length) + 2

if d.Length == 0 {
return written, nil
}

if d.Tag >= 0x80 && d.Tag <= 0xfe {
return written, writeDescriptorUserDefined(w, d.UserDefined)
}
Expand Down
31 changes: 30 additions & 1 deletion descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package astits

import (
"bytes"
"testing"

"github.com/asticode/go-astikit"
"github.com/stretchr/testify/assert"
"testing"
)

var descriptors = []*Descriptor{{
Expand Down Expand Up @@ -703,3 +704,31 @@ func BenchmarkParseDescriptor(b *testing.B) {
})
}
}

func FuzzDescriptor(f *testing.F) {
bufExpected := bytes.Buffer{}
bufExpected.Write([]byte{0x00, 0x00}) // reserve two bytes for length
wExpected := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufExpected})

for _, tc := range descriptorTestTable {
tc.bytesFunc(wExpected)
}

descLen := uint16(bufExpected.Len() - 2)
descBytes := bufExpected.Bytes()
descBytes[0] = byte(descLen>>8) | 0b11110000 // program_info_length is preceded by 4 reserved bits
descBytes[1] = byte(descLen & 0xff)

f.Add(descBytes)

f.Fuzz(func(t *testing.T, b []byte) {
ds, err := parseDescriptors(astikit.NewBytesIterator(b))

if err == nil {
bufActual := bytes.Buffer{}
wActual := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufActual})

writeDescriptorsWithLength(wActual, ds)
}
})
}
2 changes: 2 additions & 0 deletions testdata/fuzz/FuzzDescriptor/6ae8bb726335d15a
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0\x060\x02000\x00")
2 changes: 2 additions & 0 deletions testdata/fuzz/FuzzDescriptor/c18ae01ae2349b75
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0\x060\x0200X\x00")

0 comments on commit b0b1924

Please sign in to comment.