Skip to content

Commit

Permalink
xm64: avoid use-after-free bug when calling stop before close
Browse files Browse the repository at this point in the history
The following sequence:

  xm64player_stop(&player);
  xm64player_close(&player);

was causing the mixer event to stay registered after deletion of the
player.

(cherry picked from commit 208f4ea)
  • Loading branch information
rasky committed Jul 19, 2024
1 parent 3f08081 commit f3aae88
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/xm64.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ typedef struct xm64player_s {
FILE *fh; ///< open handle of XM64 file
int first_ch; ///< first channel used in the mixer
bool playing; ///< playing flag
bool stop_requested; ///< user requested stop playing
bool looping; ///< true if the XM is configured to loop
struct {
int patidx, row, tick;
Expand Down
5 changes: 3 additions & 2 deletions src/audio/xm64.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ static int tick(void *arg) {
}

// If we're requested to stop playback, do it.
if (!xmp->playing || (!xmp->looping && ctx->loop_count > 0)) {
if (xmp->stop_requested || (!xmp->looping && ctx->loop_count > 0)) {
for (int i=0;i<ctx->module.num_channels;i++)
mixer_ch_stop(xmp->first_ch+i);
xmp->playing = false;
xmp->stop_requested = false;
// Do not reschedule again
return 0;
}
Expand Down Expand Up @@ -197,7 +198,7 @@ void xm64player_play(xm64player_t *player, int first_ch) {

void xm64player_stop(xm64player_t *player) {
// Let the mixer callback stop playing
player->playing = false;
player->stop_requested = true;
}

void xm64player_tell(xm64player_t *player, int *patidx, int *row, float *secs) {
Expand Down

0 comments on commit f3aae88

Please sign in to comment.