Skip to content

Commit 0c48918

Browse files
zanadomanslouken
authored andcommitted
Mix_AllocateChannels realloc fix (#620)
(cherry picked from commit 2c28b9d)
1 parent 924f530 commit 0c48918

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

src/mixer.c

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ void Mix_PauseAudio(int pause_on)
561561
*/
562562
int Mix_AllocateChannels(int numchans)
563563
{
564+
struct _Mix_Channel *mix_channel_tmp;
565+
564566
if (numchans<0 || numchans==num_channels)
565567
return num_channels;
566568

@@ -573,27 +575,42 @@ int Mix_AllocateChannels(int numchans)
573575
}
574576
}
575577
Mix_LockAudio();
576-
mix_channel = (struct _Mix_Channel *) SDL_realloc(mix_channel, numchans * sizeof(struct _Mix_Channel));
577-
if (numchans > num_channels) {
578-
/* Initialize the new channels */
579-
int i;
580-
for (i = num_channels; i < numchans; i++) {
581-
mix_channel[i].chunk = NULL;
582-
mix_channel[i].playing = 0;
583-
mix_channel[i].looping = 0;
584-
mix_channel[i].volume = MIX_MAX_VOLUME;
585-
mix_channel[i].fade_volume = MIX_MAX_VOLUME;
586-
mix_channel[i].fade_volume_reset = MIX_MAX_VOLUME;
587-
mix_channel[i].fading = MIX_NO_FADING;
588-
mix_channel[i].tag = -1;
589-
mix_channel[i].expire = 0;
590-
mix_channel[i].effects = NULL;
591-
mix_channel[i].paused = 0;
592-
}
593-
}
594-
num_channels = numchans;
578+
/* Allocate channels into temporary pointer */
579+
if (numchans) {
580+
mix_channel_tmp = (struct _Mix_Channel *) SDL_realloc(mix_channel, numchans * sizeof(struct _Mix_Channel));
581+
} else {
582+
/* Handle 0 numchans */
583+
SDL_free(mix_channel);
584+
mix_channel_tmp = NULL;
585+
}
586+
/* Check the allocation */
587+
if (mix_channel_tmp || !numchans) {
588+
/* Apply the temporary pointer on success */
589+
mix_channel = mix_channel_tmp;
590+
if (numchans > num_channels) {
591+
/* Initialize the new channels */
592+
int i;
593+
for (i = num_channels; i < numchans; i++) {
594+
mix_channel[i].chunk = NULL;
595+
mix_channel[i].playing = 0;
596+
mix_channel[i].looping = 0;
597+
mix_channel[i].volume = MIX_MAX_VOLUME;
598+
mix_channel[i].fade_volume = MIX_MAX_VOLUME;
599+
mix_channel[i].fade_volume_reset = MIX_MAX_VOLUME;
600+
mix_channel[i].fading = MIX_NO_FADING;
601+
mix_channel[i].tag = -1;
602+
mix_channel[i].expire = 0;
603+
mix_channel[i].effects = NULL;
604+
mix_channel[i].paused = 0;
605+
}
606+
}
607+
num_channels = numchans;
608+
} else {
609+
/* On error mix_channel remains intact */
610+
Mix_SetError("Channel allocation failed");
611+
}
595612
Mix_UnlockAudio();
596-
return num_channels;
613+
return num_channels; /* If the return value equals numchans the allocation was successful */
597614
}
598615

599616
/* Return the actual mixer parameters */

0 commit comments

Comments
 (0)