From: Hraban Luyat Date: Sat, 21 Feb 2015 04:13:56 +0000 (+0000) Subject: Don't crash when passing empty array X-Git-Tag: v2.0.0~123 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=8c9cb6544f87fdfdbb0945aee375bad6aaf24ecb;p=go-opus.git 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. --- 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(