audio/vorbis: bug fix: error when a non-seeker source is given to Decode*

Closes #2252
This commit is contained in:
Hajime Hoshi 2022-08-18 15:31:47 +09:00
parent f8395e234a
commit 1251bd5792
2 changed files with 26 additions and 5 deletions

View File

@ -130,12 +130,14 @@ func decode(in io.Reader) (*decoded, int, int, error) {
posInBytes: 0, posInBytes: 0,
decoder: r, decoder: r,
} }
if _, ok := in.(io.Seeker); ok {
if _, err := d.Read(make([]byte, 65536)); err != nil && err != io.EOF { if _, err := d.Read(make([]byte, 65536)); err != nil && err != io.EOF {
return nil, 0, 0, err return nil, 0, 0, err
} }
if _, err := d.Seek(0, io.SeekStart); err != nil { if _, err := d.Seek(0, io.SeekStart); err != nil {
return nil, 0, 0, err return nil, 0, 0, err
} }
}
return d, r.Channels(), r.SampleRate(), nil return d, r.Channels(), r.SampleRate(), nil
} }

View File

@ -16,6 +16,7 @@ package vorbis_test
import ( import (
"bytes" "bytes"
"io"
"testing" "testing"
"github.com/jfreymuth/oggvorbis" "github.com/jfreymuth/oggvorbis"
@ -66,3 +67,21 @@ func TestTooShort(t *testing.T) {
t.Errorf("s.Length(): got: %d, want: %d", got, want) t.Errorf("s.Length(): got: %d, want: %d", got, want)
} }
} }
type reader struct {
r io.Reader
}
func (r *reader) Read(buf []byte) (int, error) {
return r.r.Read(buf)
}
func TestNonSeeker(t *testing.T) {
bs := test_tooshort_ogg
s, err := vorbis.DecodeWithSampleRate(audioContext.SampleRate(), &reader{r: bytes.NewReader(bs)})
if err != nil {
t.Fatal(err)
}
_ = s
}