]> Sergey Matveev's repositories - go-opus.git/commitdiff
Don't crash when passing empty array
authorHraban Luyat <hraban@0brg.net>
Sat, 21 Feb 2015 04:13:56 +0000 (04:13 +0000)
committerHraban Luyat <hraban@0brg.net>
Sat, 21 Feb 2015 04:14:49 +0000 (04:14 +0000)
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

diff --git a/api.go b/api.go
index ea86a5286cf67ebe337082dac109ab565fa424af..48de9ff0bc2ef6718f89642ea4f471f7c2bb137b 100644 (file)
--- 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(