From 8c9cb6544f87fdfdbb0945aee375bad6aaf24ecb Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Sat, 21 Feb 2015 04:13:56 +0000 Subject: [PATCH] Don't crash when passing empty array Taking the pointer of an array underlying an empty slice is an error in go: &ar[0] tries to resolve ar[0], which is out of bounds. Not pretty, but it makes sense. --- api.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/api.go b/api.go index ea86a52..48de9ff 100644 --- a/api.go +++ b/api.go @@ -56,6 +56,9 @@ func NewEncoder(sample_rate int, channels int, application Application) (*Encode } func (enc *Encoder) EncodeFloat32(pcm []float32) ([]byte, error) { + if pcm == nil || len(pcm) == 0 { + return nil, fmt.Errorf("opus: no data supplied") + } // I never know how much to allocate data := make([]byte, 10000) n := int(C.opus_encode_float( @@ -99,6 +102,9 @@ func NewDecoder(sample_rate int, channels int) (*Decoder, error) { } func (dec *Decoder) DecodeFloat32(data []byte) ([]float32, error) { + if data == nil || len(data) == 0 { + return nil, fmt.Errorf("opus: no data supplied") + } // I don't know how big this frame will be, but this is the limit pcm := make([]float32, xMAX_FRAME_SIZE_MS*dec.sample_rate/1000) n := int(C.opus_decode_float( -- 2.48.1