diff --git a/pkg/format/ac3.go b/pkg/format/ac3.go index 3253202a..34c2811a 100644 --- a/pkg/format/ac3.go +++ b/pkg/format/ac3.go @@ -1,6 +1,7 @@ package format import ( + "fmt" "strconv" "strings" @@ -23,15 +24,15 @@ func (f *AC3) unmarshal(ctx *unmarshalContext) error { tmp := strings.SplitN(ctx.clock, "/", 2) tmp1, err := strconv.ParseUint(tmp[0], 10, 31) - if err != nil { - return err + if err != nil || tmp1 == 0 { + return fmt.Errorf("invalid sample rate: '%s'", tmp[0]) } f.SampleRate = int(tmp1) if len(tmp) >= 2 { tmp1, err := strconv.ParseUint(tmp[1], 10, 31) - if err != nil { - return err + if err != nil || tmp1 == 0 { + return fmt.Errorf("invalid channel count: '%s'", tmp[1]) } f.ChannelCount = int(tmp1) } else { diff --git a/pkg/format/format_test.go b/pkg/format/format_test.go index 66ae2abe..ca3195f4 100644 --- a/pkg/format/format_test.go +++ b/pkg/format/format_test.go @@ -1267,19 +1267,45 @@ func FuzzUnmarshal(f *testing.F) { f.Add(ca.in) } - f.Fuzz(func(_ *testing.T, in string) { + f.Fuzz(func(t *testing.T, in string) { var desc sdp.SessionDescription err := desc.Unmarshal([]byte(in)) + if err != nil || len(desc.MediaDescriptions) == 0 || len(desc.MediaDescriptions[0].MediaName.Formats) == 0 { + return + } - if err == nil && len(desc.MediaDescriptions) >= 1 && len(desc.MediaDescriptions[0].MediaName.Formats) >= 1 { - f, err := Unmarshal(desc.MediaDescriptions[0], desc.MediaDescriptions[0].MediaName.Formats[0]) - if err == nil { - f.Codec() - f.ClockRate() - f.PayloadType() - f.RTPMap() - f.FMTP() - } + f, err := Unmarshal(desc.MediaDescriptions[0], desc.MediaDescriptions[0].MediaName.Formats[0]) + if err != nil { + return + } + + // only Generic can return zero ClockRate + if _, ok := f.(*Generic); !ok { + require.NotZero(t, f.ClockRate()) + } else { + f.ClockRate() + } + + f.Codec() + f.PayloadType() + f.RTPMap() + f.FMTP() + + switch f := f.(type) { + case *AC3: + require.NotZero(t, f.ChannelCount) + + case *G711: + require.NotZero(t, f.ChannelCount) + + case *LPCM: + require.NotZero(t, f.ChannelCount) + + case *Opus: + require.NotZero(t, f.ChannelCount) + + case *Vorbis: + require.NotZero(t, f.ChannelCount) } }) } diff --git a/pkg/format/g711.go b/pkg/format/g711.go index f2f93883..f1baa81c 100644 --- a/pkg/format/g711.go +++ b/pkg/format/g711.go @@ -1,6 +1,7 @@ package format import ( + "fmt" "strconv" "strings" @@ -40,15 +41,15 @@ func (f *G711) unmarshal(ctx *unmarshalContext) error { tmp := strings.SplitN(ctx.clock, "/", 2) tmp1, err := strconv.ParseUint(tmp[0], 10, 31) - if err != nil { - return err + if err != nil || tmp1 == 0 { + return fmt.Errorf("invalid sample rate: '%s'", tmp[0]) } f.SampleRate = int(tmp1) if len(tmp) >= 2 { tmp1, err := strconv.ParseUint(tmp[1], 10, 31) - if err != nil { - return err + if err != nil || tmp1 == 0 { + return fmt.Errorf("invalid channel count: '%s'", tmp[1]) } f.ChannelCount = int(tmp1) } else { diff --git a/pkg/format/lpcm.go b/pkg/format/lpcm.go index a83d96a3..a0271d19 100644 --- a/pkg/format/lpcm.go +++ b/pkg/format/lpcm.go @@ -1,6 +1,7 @@ package format import ( + "fmt" "strconv" "strings" @@ -50,15 +51,15 @@ func (f *LPCM) unmarshal(ctx *unmarshalContext) error { tmp := strings.SplitN(ctx.clock, "/", 2) tmp1, err := strconv.ParseUint(tmp[0], 10, 31) - if err != nil { - return err + if err != nil || tmp1 == 0 { + return fmt.Errorf("invalid sample rate: '%s'", tmp[0]) } f.SampleRate = int(tmp1) if len(tmp) >= 2 { tmp1, err := strconv.ParseUint(tmp[1], 10, 31) - if err != nil { - return err + if err != nil || tmp1 == 0 { + return fmt.Errorf("invalid channel count: '%s'", tmp[1]) } f.ChannelCount = int(tmp1) } else { diff --git a/pkg/format/opus.go b/pkg/format/opus.go index 09902a4a..30a3a36a 100644 --- a/pkg/format/opus.go +++ b/pkg/format/opus.go @@ -65,10 +65,9 @@ func (f *Opus) unmarshal(ctx *unmarshalContext) error { } channelCount, err := strconv.ParseUint(tmp[1], 10, 31) - if err != nil { + if err != nil || channelCount == 0 { return fmt.Errorf("invalid channel count: '%s'", tmp[1]) } - f.ChannelCount = int(channelCount) } diff --git a/pkg/format/speex.go b/pkg/format/speex.go index 9dab74b2..ec1e9a24 100644 --- a/pkg/format/speex.go +++ b/pkg/format/speex.go @@ -19,8 +19,8 @@ func (f *Speex) unmarshal(ctx *unmarshalContext) error { f.PayloadTyp = ctx.payloadType sampleRate, err := strconv.ParseUint(ctx.clock, 10, 31) - if err != nil { - return err + if err != nil || sampleRate == 0 { + return fmt.Errorf("invalid sample rate: '%s'", ctx.clock) } f.SampleRate = int(sampleRate) diff --git a/pkg/format/testdata/fuzz/FuzzUnmarshal/2910e512f655ed6b b/pkg/format/testdata/fuzz/FuzzUnmarshal/2910e512f655ed6b new file mode 100644 index 00000000..d5abd780 --- /dev/null +++ b/pkg/format/testdata/fuzz/FuzzUnmarshal/2910e512f655ed6b @@ -0,0 +1,2 @@ +go test fuzz v1 +string("m=audio 0 AVP 96\na=rtpmap:96 VORBIS/0/0\na=fmtp:96 ConfigurAtion=") diff --git a/pkg/format/testdata/fuzz/FuzzUnmarshal/2beaf42d3ab9327e b/pkg/format/testdata/fuzz/FuzzUnmarshal/2beaf42d3ab9327e new file mode 100644 index 00000000..9123e8e4 --- /dev/null +++ b/pkg/format/testdata/fuzz/FuzzUnmarshal/2beaf42d3ab9327e @@ -0,0 +1,2 @@ +go test fuzz v1 +string("m=audio 0 AVP 97\na=rtpmap:97 L8/1/0") diff --git a/pkg/format/testdata/fuzz/FuzzUnmarshal/2c9087410b17869e b/pkg/format/testdata/fuzz/FuzzUnmarshal/2c9087410b17869e new file mode 100644 index 00000000..268776af --- /dev/null +++ b/pkg/format/testdata/fuzz/FuzzUnmarshal/2c9087410b17869e @@ -0,0 +1,2 @@ +go test fuzz v1 +string("m=audio 0 AVP 96\na=rtpmap:96 PCMA/1/0") diff --git a/pkg/format/testdata/fuzz/FuzzUnmarshal/5c81ab28bdb170e5 b/pkg/format/testdata/fuzz/FuzzUnmarshal/5c81ab28bdb170e5 new file mode 100644 index 00000000..f1080142 --- /dev/null +++ b/pkg/format/testdata/fuzz/FuzzUnmarshal/5c81ab28bdb170e5 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("m=audio 0 AVP 97\na=rtpmap:97 AC3/0") diff --git a/pkg/format/testdata/fuzz/FuzzUnmarshal/6d4ccd096ae9afd2 b/pkg/format/testdata/fuzz/FuzzUnmarshal/6d4ccd096ae9afd2 new file mode 100644 index 00000000..dd874637 --- /dev/null +++ b/pkg/format/testdata/fuzz/FuzzUnmarshal/6d4ccd096ae9afd2 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("m=audio 0 AVP 96\na=rtpmap:96 AC3/1/0") diff --git a/pkg/format/testdata/fuzz/FuzzUnmarshal/72052247f0539111 b/pkg/format/testdata/fuzz/FuzzUnmarshal/72052247f0539111 new file mode 100644 index 00000000..c1bb35f2 --- /dev/null +++ b/pkg/format/testdata/fuzz/FuzzUnmarshal/72052247f0539111 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("m=audio 0 AVP 96\na=rtpmap:96 multiopus/48000/0") diff --git a/pkg/format/testdata/fuzz/FuzzUnmarshal/930d7278501087f2 b/pkg/format/testdata/fuzz/FuzzUnmarshal/930d7278501087f2 new file mode 100644 index 00000000..50a8f677 --- /dev/null +++ b/pkg/format/testdata/fuzz/FuzzUnmarshal/930d7278501087f2 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("m=audio 0 AVP 96\na=rtpmap:96 VORBIS/1/0\na=fmtp:96 ConfigurAtion=") diff --git a/pkg/format/testdata/fuzz/FuzzUnmarshal/b1a3816de12e306d b/pkg/format/testdata/fuzz/FuzzUnmarshal/b1a3816de12e306d new file mode 100644 index 00000000..66a56a26 --- /dev/null +++ b/pkg/format/testdata/fuzz/FuzzUnmarshal/b1a3816de12e306d @@ -0,0 +1,2 @@ +go test fuzz v1 +string("m=audio 0 AVP 96\na=rtpmap:96 speeX/0") diff --git a/pkg/format/testdata/fuzz/FuzzUnmarshal/c3f02e1806e34919 b/pkg/format/testdata/fuzz/FuzzUnmarshal/c3f02e1806e34919 new file mode 100644 index 00000000..f228472d --- /dev/null +++ b/pkg/format/testdata/fuzz/FuzzUnmarshal/c3f02e1806e34919 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("m=audio 0 AVP 97\na=rtpmap:97 L16/0") diff --git a/pkg/format/vorbis.go b/pkg/format/vorbis.go index 9d5d8017..d595960d 100644 --- a/pkg/format/vorbis.go +++ b/pkg/format/vorbis.go @@ -27,14 +27,14 @@ func (f *Vorbis) unmarshal(ctx *unmarshalContext) error { } sampleRate, err := strconv.ParseUint(tmp[0], 10, 31) - if err != nil { - return err + if err != nil || sampleRate == 0 { + return fmt.Errorf("invalid sample rate: '%s'", tmp[0]) } f.SampleRate = int(sampleRate) channelCount, err := strconv.ParseUint(tmp[1], 10, 31) - if err != nil { - return err + if err != nil || channelCount == 0 { + return fmt.Errorf("invalid channel count: '%s'", tmp[1]) } f.ChannelCount = int(channelCount)