beep icon indicating copy to clipboard operation
beep copied to clipboard

Preventing int64 out of range panics on the Resampler

Open skuzzymiglet opened this issue 5 years ago • 7 comments

In Resamplers, a slice can be indexed with over 9223372036854775807 unchecked, which causes a panic. An error returned is obviously better than panic()

skuzzymiglet avatar Jul 16 '20 17:07 skuzzymiglet

Do you have a code which actually causes this?

faiface avatar Jul 16 '20 17:07 faiface

beep.ResampleRatio, above a certain value. Really high tempos on my program (https://github.com/skuzzymiglet/metro) cause panic. In one case a 0.29s flac sample, resampled to 1/1000s is the minimum for a panic()

skuzzymiglet avatar Jul 16 '20 17:07 skuzzymiglet

Okay, but I don't think returning an error is the right way. I think we just need to fix the bug.

faiface avatar Jul 16 '20 17:07 faiface

My trace:

panic: runtime error: index out of range [-9223372036854775296]

goroutine 1 [running]:
github.com/faiface/beep.(*Resampler).Stream(0xc0001b8000, 0xc0001ba000, 0x200, 0x200, 0xc0001b6000, 0x741a88)
        /home/skuzzymiglet/.local/go/pkg/mod/github.com/faiface/[email protected]/resample.go:96 +0x3eb
github.com/faiface/beep.(*Buffer).Append(0xc0002b9f28, 0x81a5e0, 0xc0001b8000)
        /home/skuzzymiglet/.local/go/pkg/mod/github.com/faiface/[email protected]/buffer.go:198 +0x87
main.main()
        /home/skuzzymiglet/code/metro/main.go:135 +0x5c8

skuzzymiglet avatar Jul 16 '20 17:07 skuzzymiglet

I don't know about how to fix it. I guess we could use a uint64 rather than int64. But the panic needs to be prevented

skuzzymiglet avatar Jul 16 '20 17:07 skuzzymiglet

It's impossible to have such large slices anyway, they wouldn't fit into memory, it's clearly some kind of a bug. Right now I'm travelling, so I can't take a look at it, but if you get a chance and find the bug, please tell, or submit a PR.

faiface avatar Jul 16 '20 17:07 faiface

Old issue, and I'm not sure what in particular causes it, but I think it may be related to using a Resampler with the same old and new sample rates, e.g. beep.Resample(4, 48000, 48000, stream). Something about the math involved with the resampling may result in funny values when the sample rates match.

edit: Actually, now I'm getting the same panic I was getting before, even though I am resampling from 44.1KHz to 48KHz. I am able to trigger it by replaying from the same stream over and over and over. Here is a screenshot of a debugger window with relevant values listed in case it helps in determining what's happening: https://i.imgur.com/um4xylV.png

ianling avatar Oct 18 '21 20:10 ianling