[OpenAL] Fix sourceSeek crash on stopped sources - #27426
Conversation
| AL.setSourceState(src, {{{ cDefs.AL_INITIAL }}}); | ||
| } | ||
|
|
||
| if (src.bufQueue[src.bufsProcessed].audioBuf !== null) { |
There was a problem hiding this comment.
The removal of the outer if (src.bufQueue[src.bufsProcessed].audioBuf !== null) here is an important part of the fix.
When an OpenAL source is stopped (or finishes playback), libopenal.js sets src.bufsProcessed = src.bufQueue.length. This would make the outer if condition crash with
TypeError: Cannot read properties of undefined (reading 'audioBuf') crash when attempting to seek on a stopped source.
The spirit of the check is still here but in the format the ? notation in buf.audioBuf?.duration ?? 0.0.
It also important that the src.bufsProcessed get before the loop and out side of the if condition here.
There was a problem hiding this comment.
What this code is basically doing is resetting the seek potions by resetting both bufsProcessed (which buffer are currently playing) and bufOffset (how far into the currnent buffer are we).
So this loops is first resetting the bufsProcessed and then incrementing until we have accumulated enough "duration" to satisfy the desired seek offset.
Fix a crash in `sourceSeek` when called on a stopped source by resetting `bufsProcessed = 0` before evaluating buffer durations. This replaces emscripten-core#26770. Unlike emscripten-core#26770, which added a top-level check on `src.bufQueue[0]`, this change handles zero/dummy buffers and empty queues safely anywhere in `bufQueue` via `for .. of` iteration and `audioBuf?.duration ?? 0.0`. It also adds assertions to `test_openal_error.c` to verify state correctness after seeking. See: emscripten-core#26770
Fix a crash in
sourceSeekwhen called on a stopped source by resettingbufsProcessed = 0before evaluating buffer durations.This replaces #26770. Unlike #26770, which added a top-level check on
src.bufQueue[0], this change handles zero/dummy buffers and empty queues safely anywhere inbufQueueviafor .. ofiteration andaudioBuf?.duration ?? 0.0. It also adds assertions totest_openal_error.cto verify state correctness after seeking.See: #26770