Skip to content

Commit

Permalink
Fixed memory crash loading Ogg Vorbis files on Windows
Browse files Browse the repository at this point in the history
The pointer to the audio data could come from SDL_LoadWAV_RW() or from our other loaders, and we need to use the correct free() to release the memory.  So we'll just use the SDL memory functions for consistency.
This pretty much only matters on Windows where we can't guarantee a certain C runtime so we provide our own malloc() and friends.
  • Loading branch information
slouken committed Jan 13, 2012
1 parent d23e338 commit 62fc349
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 102 deletions.
2 changes: 2 additions & 0 deletions CHANGES
@@ -1,4 +1,6 @@
1.2.12:
Sam Lantinga - Fri Jan 13 03:04:27 EST 2012
* Fixed memory crash loading Ogg Vorbis files on Windows
Nikos Chantziaras - 2012-01-02 17:37:36 PST
* Added Mix_LoadMUSType_RW() so you can tell SDL_mixer what type the music is
Sam Lantinga - Sat Dec 31 19:11:59 EST 2011
Expand Down
16 changes: 8 additions & 8 deletions effect_position.c
Expand Up @@ -85,14 +85,14 @@ void _Eff_PositionDeinit(void)
{
int i;
for (i = 0; i < position_channels; i++) {
free(pos_args_array[i]);
SDL_free(pos_args_array[i]);
}

position_channels = 0;

free(pos_args_global);
SDL_free(pos_args_global);
pos_args_global = NULL;
free(pos_args_array);
SDL_free(pos_args_array);
pos_args_array = NULL;
}

Expand All @@ -102,13 +102,13 @@ static void _Eff_PositionDone(int channel, void *udata)
{
if (channel < 0) {
if (pos_args_global != NULL) {
free(pos_args_global);
SDL_free(pos_args_global);
pos_args_global = NULL;
}
}

else if (pos_args_array[channel] != NULL) {
free(pos_args_array[channel]);
SDL_free(pos_args_array[channel]);
pos_args_array[channel] = NULL;
}
}
Expand Down Expand Up @@ -1149,7 +1149,7 @@ static position_args *get_position_arg(int channel)

if (channel < 0) {
if (pos_args_global == NULL) {
pos_args_global = malloc(sizeof (position_args));
pos_args_global = SDL_malloc(sizeof (position_args));
if (pos_args_global == NULL) {
Mix_SetError("Out of memory");
return(NULL);
Expand All @@ -1161,7 +1161,7 @@ static position_args *get_position_arg(int channel)
}

if (channel >= position_channels) {
rc = realloc(pos_args_array, (channel + 1) * sizeof (position_args *));
rc = SDL_realloc(pos_args_array, (channel + 1) * sizeof (position_args *));
if (rc == NULL) {
Mix_SetError("Out of memory");
return(NULL);
Expand All @@ -1174,7 +1174,7 @@ static position_args *get_position_arg(int channel)
}

if (pos_args_array[channel] == NULL) {
pos_args_array[channel] = (position_args *)malloc(sizeof(position_args));
pos_args_array[channel] = (position_args *)SDL_malloc(sizeof(position_args));
if (pos_args_array[channel] == NULL) {
Mix_SetError("Out of memory");
return(NULL);
Expand Down
4 changes: 2 additions & 2 deletions effects_internal.c
Expand Up @@ -73,7 +73,7 @@ void *_Eff_build_volume_table_u8(void)
}

if (!_Eff_volume_table) {
rc = malloc(256 * 256);
rc = SDL_malloc(256 * 256);
if (rc) {
_Eff_volume_table = (void *) rc;
for (volume = 0; volume < 256; volume++) {
Expand Down Expand Up @@ -104,7 +104,7 @@ void *_Eff_build_volume_table_s8(void)
Sint8 *rc;

if (!_Eff_volume_table) {
rc = malloc(256 * 256);
rc = SDL_malloc(256 * 256);
if (rc) {
_Eff_volume_table = (void *) rc;
for (volume = 0; volume < 256; volume++) {
Expand Down
14 changes: 7 additions & 7 deletions fluidsynth.c
Expand Up @@ -75,7 +75,7 @@ static FluidSynthMidiSong *fluidsynth_loadsong_common(int (*function)(FluidSynth
return NULL;
}

if ((song = malloc(sizeof(FluidSynthMidiSong)))) {
if ((song = SDL_malloc(sizeof(FluidSynthMidiSong)))) {
memset(song, 0, sizeof(FluidSynthMidiSong));

if (SDL_BuildAudioCVT(&song->convert, AUDIO_S16, 2, freq, format, channels, freq) >= 0) {
Expand All @@ -102,7 +102,7 @@ static FluidSynthMidiSong *fluidsynth_loadsong_common(int (*function)(FluidSynth
} else {
Mix_SetError("Failed to set up audio conversion");
}
free(song);
SDL_free(song);
} else {
Mix_SetError("Insufficient memory for song");
}
Expand All @@ -121,7 +121,7 @@ static int fluidsynth_loadsong_RW_internal(FluidSynthMidiSong *song, void *data)
size = SDL_RWtell(rw) - offset;
SDL_RWseek(rw, offset, RW_SEEK_SET);

if ((buffer = (char*) malloc(size))) {
if ((buffer = (char*) SDL_malloc(size))) {
if(SDL_RWread(rw, buffer, size, 1) == 1) {
if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) {
return 1;
Expand All @@ -131,7 +131,7 @@ static int fluidsynth_loadsong_RW_internal(FluidSynthMidiSong *song, void *data)
} else {
Mix_SetError("Failed to read in-memory song");
}
free(buffer);
SDL_free(buffer);
} else {
Mix_SetError("Insufficient memory for song");
}
Expand All @@ -155,7 +155,7 @@ void fluidsynth_freesong(FluidSynthMidiSong *song)
fluidsynth.delete_fluid_player(song->player);
fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(song->synth));
fluidsynth.delete_fluid_synth(song->synth);
free(song);
SDL_free(song);
}

void fluidsynth_start(FluidSynthMidiSong *song)
Expand Down Expand Up @@ -188,7 +188,7 @@ int fluidsynth_playsome(FluidSynthMidiSong *song, void *dest, int dest_len)
void *src = dest;

if (dest_len < src_len) {
if (!(src = malloc(src_len))) {
if (!(src = SDL_malloc(src_len))) {
Mix_SetError("Insufficient memory for audio conversion");
return result;
}
Expand All @@ -214,7 +214,7 @@ int fluidsynth_playsome(FluidSynthMidiSong *song, void *dest, int dest_len)

finish:
if (src != dest)
free(src);
SDL_free(src);

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion load_aiff.c
Expand Up @@ -224,7 +224,7 @@ SDL_AudioSpec *Mix_LoadAIFF_RW (SDL_RWops *src, int freesrc,
spec->samples = 4096; /* Good default buffer size */

*audio_len = channels * numsamples * (samplesize / 8);
*audio_buf = (Uint8 *)malloc(*audio_len);
*audio_buf = (Uint8 *)SDL_malloc(*audio_len);
if ( *audio_buf == NULL ) {
SDL_SetError("Out of memory");
return(NULL);
Expand Down
10 changes: 5 additions & 5 deletions load_flac.c
Expand Up @@ -176,7 +176,7 @@ static FLAC__StreamDecoderWriteStatus flac_write_load_cb(
if (frame->header.number.sample_number == 0) {
*(data->sdl_audio_len) = data->sdl_spec->size;
data->sdl_audio_read = 0;
*(data->sdl_audio_buf) = malloc (*(data->sdl_audio_len));
*(data->sdl_audio_buf) = SDL_malloc (*(data->sdl_audio_len));

if (*(data->sdl_audio_buf) == NULL) {
SDL_SetError
Expand Down Expand Up @@ -274,7 +274,7 @@ SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc,

// create the client data passing information
FLAC_SDL_Data* client_data;
client_data = (FLAC_SDL_Data *)malloc (sizeof (FLAC_SDL_Data));
client_data = (FLAC_SDL_Data *)SDL_malloc (sizeof (FLAC_SDL_Data));

if ((!src) || (!audio_buf) || (!audio_len)) /* sanity checks. */
goto done;
Expand Down Expand Up @@ -313,9 +313,9 @@ SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc,

was_error = 0;

/* Don't return a buffer that isn't a multiple of samplesize */
samplesize = ((spec->format & 0xFF) / 8) * spec->channels;
*audio_len &= ~(samplesize - 1);
/* Don't return a buffer that isn't a multiple of samplesize */
samplesize = ((spec->format & 0xFF) / 8) * spec->channels;
*audio_len &= ~(samplesize - 1);

done:
if (was_init && decoder) {
Expand Down
2 changes: 1 addition & 1 deletion load_ogg.c
Expand Up @@ -113,7 +113,7 @@ SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc,
samples = (long)vorbis.ov_pcm_total(&vf, -1);

*audio_len = spec->size = samples * spec->channels * 2;
*audio_buf = malloc(*audio_len);
*audio_buf = SDL_malloc(*audio_len);
if (*audio_buf == NULL)
goto done;

Expand Down
6 changes: 3 additions & 3 deletions load_voc.c
Expand Up @@ -411,7 +411,7 @@ SDL_AudioSpec *Mix_LoadVOC_RW (SDL_RWops *src, int freesrc,
spec->channels = v.channels;

*audio_len = v.rest;
*audio_buf = malloc(v.rest);
*audio_buf = SDL_malloc(v.rest);
if (*audio_buf == NULL)
goto done;

Expand All @@ -423,10 +423,10 @@ SDL_AudioSpec *Mix_LoadVOC_RW (SDL_RWops *src, int freesrc,
goto done;

*audio_len += v.rest;
ptr = realloc(*audio_buf, *audio_len);
ptr = SDL_realloc(*audio_buf, *audio_len);
if (ptr == NULL)
{
free(*audio_buf);
SDL_free(*audio_buf);
*audio_buf = NULL;
*audio_len = 0;
goto done;
Expand Down

0 comments on commit 62fc349

Please sign in to comment.