From 5bc14606484de141900afff8269cf525c4a231c9 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 18 Aug 2022 15:31:47 +0900 Subject: [PATCH] audio/vorbis: bug fix: error when a non-seeker source is given to Decode* Closes #2252 --- audio/vorbis/vorbis.go | 12 +++++++----- audio/vorbis/vorbis_test.go | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/audio/vorbis/vorbis.go b/audio/vorbis/vorbis.go index e6b54469f..a115999c7 100644 --- a/audio/vorbis/vorbis.go +++ b/audio/vorbis/vorbis.go @@ -130,11 +130,13 @@ func decode(in io.Reader) (*decoded, int, int, error) { posInBytes: 0, decoder: r, } - if _, err := d.Read(make([]byte, 65536)); err != nil && err != io.EOF { - return nil, 0, 0, err - } - if _, err := d.Seek(0, io.SeekStart); err != nil { - return nil, 0, 0, err + if _, ok := in.(io.Seeker); ok { + if _, err := d.Read(make([]byte, 65536)); err != nil && err != io.EOF { + return nil, 0, 0, err + } + if _, err := d.Seek(0, io.SeekStart); err != nil { + return nil, 0, 0, err + } } return d, r.Channels(), r.SampleRate(), nil } diff --git a/audio/vorbis/vorbis_test.go b/audio/vorbis/vorbis_test.go index ef47bf0f1..ca3881962 100644 --- a/audio/vorbis/vorbis_test.go +++ b/audio/vorbis/vorbis_test.go @@ -16,6 +16,7 @@ package vorbis_test import ( "bytes" + "io" "testing" "github.com/jfreymuth/oggvorbis" @@ -66,3 +67,21 @@ func TestTooShort(t *testing.T) { 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 +}