jarikomppa / soloud

Free, easy, portable audio engine for games
http://soloud-audio.com
Other
1.69k stars 270 forks source link

Implement seek for missing codes in wavestream #336

Open lakor64 opened 2 years ago

lakor64 commented 2 years ago

The following code implements seeking for WAV, FLAC and MP3 on the . This also fixes looping on those three file types when enabled.

By looking on the offset code on OGG (with stb_vorbis_get_sample_offset), I am not sure if it's required on dr* libraries as it doesn't seem to jump to a different PCM, I guess stb_vorbis_get_sample_offsets gets the position that we seek?

HailToDodongo commented 1 year ago

@jarikomppa This PR actually fixes a pretty serious memory corruption issue i was facing in my project. I traced this bug down with valgrind, where this exact function (WavStreamInstance::seek) was always the root-cause of invalid memory writes.

Before, this code only checked for mCodec.mOgg != nullptr and then assumed that the codec is ogg-vorbis. However mCodec is a union, so every type (including wave) will have mCodec.mOgg set. This causes stb_vorbis_get_sample_offset (treating *mWav's value as *mOgg) to write to arbitrary memory, which in my case lead to a crash. Writes where also wide-spread, since the struct contains channel- and sample-counts which potentially became pretty large numbers.

After applying this patch, all other related OOB writes also vanished. https://github.com/jarikomppa/soloud/issues/279 seems to be related to this, since i got the exact same behaviour of null-pointers in that array. This stopped too after that change.