audio/vorbis: Bug fix: fail to decode too short ogg

This commit is contained in:
Hajime Hoshi 2018-04-07 22:13:12 +09:00
parent c99b1b57a9
commit 153628b4fd
3 changed files with 30 additions and 7 deletions

BIN
audio/vorbis/test_tooshort.ogg Executable file

Binary file not shown.

View File

@ -172,7 +172,7 @@ func decode(in audio.ReadSeekCloser) (*decoded, int, int, error) {
decoder: r,
}
runtime.SetFinalizer(d, (*decoded).Close)
if _, err := d.Read(make([]uint8, 65536)); err != nil {
if _, err := d.Read(make([]uint8, 65536)); err != nil && err != io.EOF {
return nil, 0, 0, err
}
if _, err := d.Seek(0, io.SeekStart); err != nil {

View File

@ -24,18 +24,23 @@ import (
. "github.com/hajimehoshi/ebiten/audio/vorbis"
)
var audioContext *audio.Context
func init() {
var err error
audioContext, err = audio.NewContext(44100)
if err != nil {
panic(err)
}
}
func TestMono(t *testing.T) {
bs, err := ioutil.ReadFile("test_mono.ogg")
if err != nil {
t.Fatal(err)
}
c, err := audio.NewContext(44100)
if err != nil {
t.Fatal(err)
}
s, err := Decode(c, audio.BytesReadSeekCloser(bs))
s, err := Decode(audioContext, audio.BytesReadSeekCloser(bs))
if err != nil {
t.Fatal(err)
}
@ -57,3 +62,21 @@ func TestMono(t *testing.T) {
t.Errorf("s.Length(): got: %d, want: %d", got, want)
}
}
func TestTooShort(t *testing.T) {
bs, err := ioutil.ReadFile("test_tooshort.ogg")
if err != nil {
t.Fatal(err)
}
s, err := Decode(audioContext, audio.BytesReadSeekCloser(bs))
if err != nil {
t.Fatal(err)
}
got := s.Length()
want := int64(79424)
if got != want {
t.Errorf("s.Length(): got: %d, want: %d", got, want)
}
}