Skip to content

Commit

Permalink
SDL_LoadMUS_RW() now takes an argument telling whether or not the dat…
Browse files Browse the repository at this point in the history
…a source should be freed when done.

Fixes crashes cleaning up the data source for music when loading fails.
  • Loading branch information
slouken committed Jun 2, 2013
1 parent 4920a39 commit 852f831
Show file tree
Hide file tree
Showing 37 changed files with 268 additions and 316 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
@@ -1,3 +1,8 @@
2.0.0:
Sam Lantinga - Sat Jun 1 19:11:08 PDT 2013
* Updated for SDL 2.0 release
* SDL_LoadMUS_RW() now takes an argument telling whether or not the data source should be freed when done.

1.2.13:
Paul P Komkoff Jr - Sun Jul 22 16:12:28 PDT 2012
* Fixed malloc/free mismatch in the MikMod driver
Expand Down
4 changes: 2 additions & 2 deletions SDL_mixer.h
Expand Up @@ -151,10 +151,10 @@ extern DECLSPEC Mix_Music * SDLCALL Mix_LoadMUS(const char *file);

/* Load a music file from an SDL_RWop object (Ogg and MikMod specific currently)
Matt Campbell (matt@campbellhome.dhs.org) April 2000 */
extern DECLSPEC Mix_Music * SDLCALL Mix_LoadMUS_RW(SDL_RWops *rw);
extern DECLSPEC Mix_Music * SDLCALL Mix_LoadMUS_RW(SDL_RWops *src, int freesrc);

/* Load a music file from an SDL_RWop object assuming a specific format */
extern DECLSPEC Mix_Music * SDLCALL Mix_LoadMUSType_RW(SDL_RWops *rw, Mix_MusicType type, int freesrc);
extern DECLSPEC Mix_Music * SDLCALL Mix_LoadMUSType_RW(SDL_RWops *src, Mix_MusicType type, int freesrc);

/* Load a wave file of the mixer format from a memory buffer */
extern DECLSPEC Mix_Chunk * SDLCALL Mix_QuickLoad_WAV(Uint8 *mem);
Expand Down
6 changes: 4 additions & 2 deletions VisualC/external/include/smpeg.h
Expand Up @@ -102,8 +102,10 @@ extern DECLSPEC SMPEG* SMPEG_new_descr(int file, SMPEG_Info* info, int sdl_audio
*/
extern DECLSPEC SMPEG* SMPEG_new_data(void *data, int size, SMPEG_Info* info, int sdl_audio);

/* The same for a generic SDL_RWops structure. */
extern DECLSPEC SMPEG* SMPEG_new_rwops(SDL_RWops *src, SMPEG_Info* info, int sdl_audio);
/* The same for a generic SDL_RWops structure.
'freesrc' should be non-zero if SMPEG should close the source when it's done.
*/
extern DECLSPEC SMPEG* SMPEG_new_rwops(SDL_RWops *src, SMPEG_Info* info, int freesrc, int sdl_audio);

/* Get current information about an SMPEG object */
extern DECLSPEC void SMPEG_getinfo( SMPEG* mpeg, SMPEG_Info* info );
Expand Down
Binary file modified VisualC/external/lib/x64/smpeg2.dll
Binary file not shown.
Binary file modified VisualC/external/lib/x86/smpeg2.dll
Binary file not shown.
6 changes: 4 additions & 2 deletions Xcode/Frameworks/smpeg2.framework/Versions/A/Headers/smpeg.h
Expand Up @@ -102,8 +102,10 @@ extern DECLSPEC SMPEG* SMPEG_new_descr(int file, SMPEG_Info* info, int sdl_audio
*/
extern DECLSPEC SMPEG* SMPEG_new_data(void *data, int size, SMPEG_Info* info, int sdl_audio);

/* The same for a generic SDL_RWops structure. */
extern DECLSPEC SMPEG* SMPEG_new_rwops(SDL_RWops *src, SMPEG_Info* info, int sdl_audio);
/* The same for a generic SDL_RWops structure.
'freesrc' should be non-zero if SMPEG should close the source when it's done.
*/
extern DECLSPEC SMPEG* SMPEG_new_rwops(SDL_RWops *src, SMPEG_Info* info, int freesrc, int sdl_audio);

/* Get current information about an SMPEG object */
extern DECLSPEC void SMPEG_getinfo( SMPEG* mpeg, SMPEG_Info* info );
Expand Down
Binary file modified Xcode/Frameworks/smpeg2.framework/Versions/A/smpeg2
Binary file not shown.
2 changes: 1 addition & 1 deletion dynamic_mp3.c
Expand Up @@ -66,7 +66,7 @@ int Mix_InitMP3()
return -1;
}
smpeg.SMPEG_new_rwops =
(SMPEG* (*)(SDL_RWops *, SMPEG_Info*, int))
(SMPEG* (*)(SDL_RWops *, SMPEG_Info*, int, int))
SDL_LoadFunction(smpeg.handle, "SMPEG_new_rwops");
if ( smpeg.SMPEG_new_rwops == NULL ) {
SDL_UnloadObject(smpeg.handle);
Expand Down
2 changes: 1 addition & 1 deletion dynamic_mp3.h
Expand Up @@ -29,7 +29,7 @@ typedef struct {
void (*SMPEG_delete)( SMPEG* mpeg );
void (*SMPEG_enableaudio)( SMPEG* mpeg, int enable );
void (*SMPEG_enablevideo)( SMPEG* mpeg, int enable );
SMPEG* (*SMPEG_new_rwops)(SDL_RWops *src, SMPEG_Info* info, int sdl_audio);
SMPEG* (*SMPEG_new_rwops)(SDL_RWops *src, SMPEG_Info* info, int freesrc, int sdl_audio);
void (*SMPEG_play)( SMPEG* mpeg );
int (*SMPEG_playAudio)( SMPEG *mpeg, Uint8 *stream, int len );
void (*SMPEG_rewind)( SMPEG* mpeg );
Expand Down
22 changes: 11 additions & 11 deletions external/smpeg2-2.0.0/MPEG.cpp
Expand Up @@ -28,7 +28,7 @@ MPEG::MPEG(const char * name, bool SDLaudio) :
SetError(SDL_GetError());
return;
}
Init(source, SDLaudio);
Init(source, SDL_TRUE, SDLaudio);
}

MPEG::MPEG(int Mpeg_FD, bool SDLaudio) :
Expand All @@ -47,13 +47,13 @@ MPEG::MPEG(int Mpeg_FD, bool SDLaudio) :
return;
}

source = SDL_RWFromFP(file,(SDL_bool) false);
source = SDL_RWFromFP(file, SDL_FALSE);
if (!source) {
InitErrorState();
SetError(SDL_GetError());
return;
}
Init(source, SDLaudio);
Init(source, SDL_TRUE, SDLaudio);
}

MPEG::MPEG(void *data, int size, bool SDLaudio) :
Expand All @@ -72,19 +72,20 @@ MPEG::MPEG(void *data, int size, bool SDLaudio) :
SetError(SDL_GetError());
return;
}
Init(source, SDLaudio);
Init(source, SDL_TRUE, SDLaudio);
}

MPEG::MPEG(SDL_RWops *mpeg_source, bool SDLaudio) :
MPEG::MPEG(SDL_RWops *mpeg_source, int mpeg_freesrc, bool SDLaudio) :
MPEGerror()
{
mpeg_mem = 0;
Init(mpeg_source, SDLaudio);
Init(mpeg_source, mpeg_freesrc, SDLaudio);
}

void MPEG::Init(SDL_RWops *mpeg_source, bool SDLaudio)
void MPEG::Init(SDL_RWops *mpeg_source, int mpeg_freesrc, bool SDLaudio)
{
source = mpeg_source;
freesrc = mpeg_freesrc;
sdlaudio = SDLaudio;

/* Create the system that will parse the MPEG stream */
Expand Down Expand Up @@ -135,6 +136,7 @@ void MPEG::InitErrorState() {
system = NULL;
error = NULL;
source = NULL;
freesrc = 0;

audiostream = videostream = NULL;
audioaction = NULL;
Expand All @@ -152,10 +154,8 @@ MPEG::~MPEG()
if(video) delete video;
if(audio) delete audio;
if(system) delete system;

if(source) SDL_RWclose(source);
if ( mpeg_mem )
delete[] mpeg_mem;
if(source && freesrc) SDL_RWclose(source);
if (mpeg_mem) delete[] mpeg_mem;
}

bool MPEG::AudioEnabled(void) {
Expand Down
5 changes: 3 additions & 2 deletions external/smpeg2-2.0.0/MPEG.h
Expand Up @@ -58,11 +58,11 @@ class MPEG : public MPEGerror
MPEG(const char * name, bool SDLaudio = true);
MPEG(int Mpeg_FD, bool SDLaudio = true);
MPEG(void *data, int size, bool SDLaudio = true);
MPEG(SDL_RWops *mpeg_source,bool SDLaudio = true);
MPEG(SDL_RWops *mpeg_source, int mpeg_freesrc, bool SDLaudio = true);
virtual ~MPEG();

/* Initialize the MPEG */
void Init(SDL_RWops *mpeg_source, bool SDLaudio);
void Init(SDL_RWops *mpeg_source, int mpeg_freesrc, bool SDLaudio);
void InitErrorState();

/* Enable/Disable audio and video */
Expand Down Expand Up @@ -108,6 +108,7 @@ class MPEG : public MPEGerror
protected:
char *mpeg_mem; // Used to copy MPEG passed in as memory
SDL_RWops *source;
int freesrc;
MPEGaudioaction *audioaction;
MPEGvideoaction *videoaction;

Expand Down
2 changes: 1 addition & 1 deletion external/smpeg2-2.0.0/g++-fat.sh
Expand Up @@ -2,7 +2,7 @@
#
# Build Universal binaries on Mac OS X, thanks Ryan!
#
# Usage: ./configure CC="sh g++-fat.sh" && make && rm -rf x86 x64
# Usage: ./configure CXX="sh g++-fat.sh" && make && rm -rf x86 x64

DEVELOPER="`xcode-select -print-path`/Platforms/MacOSX.platform/Developer"

Expand Down
4 changes: 2 additions & 2 deletions external/smpeg2-2.0.0/smpeg.cpp
Expand Up @@ -131,7 +131,7 @@ SMPEG* SMPEG_new_data(void *data, int size, SMPEG_Info* info, int sdl_audio)
return(mpeg);
}

SMPEG* SMPEG_new_rwops(SDL_RWops *src, SMPEG_Info* info, int sdl_audio)
SMPEG* SMPEG_new_rwops(SDL_RWops *src, SMPEG_Info* info, int freesrc, int sdl_audio)
{
SMPEG *mpeg;

Expand All @@ -140,7 +140,7 @@ SMPEG* SMPEG_new_rwops(SDL_RWops *src, SMPEG_Info* info, int sdl_audio)

/* Create a new SMPEG object! */
mpeg = new SMPEG;
mpeg->obj = new MPEG(src, sdl_audio ? true : false);
mpeg->obj = new MPEG(src, freesrc, sdl_audio ? true : false);

/* Find out the details of the stream, if requested */
SMPEG_getinfo(mpeg, info);
Expand Down
6 changes: 4 additions & 2 deletions external/smpeg2-2.0.0/smpeg.h
Expand Up @@ -102,8 +102,10 @@ extern DECLSPEC SMPEG* SMPEG_new_descr(int file, SMPEG_Info* info, int sdl_audio
*/
extern DECLSPEC SMPEG* SMPEG_new_data(void *data, int size, SMPEG_Info* info, int sdl_audio);

/* The same for a generic SDL_RWops structure. */
extern DECLSPEC SMPEG* SMPEG_new_rwops(SDL_RWops *src, SMPEG_Info* info, int sdl_audio);
/* The same for a generic SDL_RWops structure.
'freesrc' should be non-zero if SMPEG should close the source when it's done.
*/
extern DECLSPEC SMPEG* SMPEG_new_rwops(SDL_RWops *src, SMPEG_Info* info, int freesrc, int sdl_audio);

/* Get current information about an SMPEG object */
extern DECLSPEC void SMPEG_getinfo( SMPEG* mpeg, SMPEG_Info* info );
Expand Down
22 changes: 11 additions & 11 deletions fluidsynth.c
Expand Up @@ -111,18 +111,18 @@ static FluidSynthMidiSong *fluidsynth_loadsong_common(int (*function)(FluidSynth

static int fluidsynth_loadsong_RW_internal(FluidSynthMidiSong *song, void *data)
{
off_t offset;
Sint64 offset;
size_t size;
char *buffer;
SDL_RWops *rw = (SDL_RWops*) data;
SDL_RWops *src = (SDL_RWops*) data;

offset = SDL_RWtell(rw);
SDL_RWseek(rw, 0, RW_SEEK_END);
size = SDL_RWtell(rw) - offset;
SDL_RWseek(rw, offset, RW_SEEK_SET);
offset = SDL_RWtell(src);
SDL_RWseek(src, 0, RW_SEEK_END);
size = (size_t)(SDL_RWtell(src) - offset);
SDL_RWseek(src, offset, RW_SEEK_SET);

if ((buffer = (char*) SDL_malloc(size))) {
if(SDL_RWread(rw, buffer, size, 1) == 1) {
if(SDL_RWread(src, buffer, size, 1) == 1) {
if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) {
return 1;
} else {
Expand All @@ -138,13 +138,13 @@ static int fluidsynth_loadsong_RW_internal(FluidSynthMidiSong *song, void *data)
return 0;
}

FluidSynthMidiSong *fluidsynth_loadsong_RW(SDL_RWops *rw, int freerw)
FluidSynthMidiSong *fluidsynth_loadsong_RW(SDL_RWops *src, int freesrc)
{
FluidSynthMidiSong *song;

song = fluidsynth_loadsong_common(fluidsynth_loadsong_RW_internal, (void*) rw);
if (freerw) {
SDL_RWclose(rw);
song = fluidsynth_loadsong_common(fluidsynth_loadsong_RW_internal, (void*) src);
if (song && freesrc) {
SDL_RWclose(src);
}
return song;
}
Expand Down
4 changes: 2 additions & 2 deletions load_aiff.c
Expand Up @@ -239,10 +239,10 @@ SDL_AudioSpec *Mix_LoadAIFF_RW (SDL_RWops *src, int freesrc,
*audio_len &= ~((samplesize / 8) - 1);

done:
if ( freesrc && src ) {
if (freesrc && src) {
SDL_RWclose(src);
}
if ( was_error ) {
if (was_error) {
spec = NULL;
}
return(spec);
Expand Down
10 changes: 4 additions & 6 deletions load_flac.c
Expand Up @@ -315,15 +315,13 @@ SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc,
flac.FLAC__stream_decoder_delete (decoder);
}

if (src) {
if (freesrc)
SDL_RWclose (src);
else
SDL_RWseek (src, 0, RW_SEEK_SET);
if (freesrc && src) {
SDL_RWclose (src);
}

if (was_error)
if (was_error) {
spec = NULL;
}

return spec;
}
Expand Down
11 changes: 4 additions & 7 deletions load_ogg.c
Expand Up @@ -144,16 +144,13 @@ SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc,
*audio_len &= ~(samplesize-1);

done:
if (src && must_close)
{
if (freesrc)
SDL_RWclose(src);
else
SDL_RWseek(src, 0, RW_SEEK_SET);
if (freesrc && src && must_close) {
SDL_RWclose(src);
}

if ( was_error )
if (was_error) {
spec = NULL;
}

return(spec);
} /* Mix_LoadOGG_RW */
Expand Down
11 changes: 4 additions & 7 deletions load_voc.c
Expand Up @@ -445,16 +445,13 @@ SDL_AudioSpec *Mix_LoadVOC_RW (SDL_RWops *src, int freesrc,
*audio_len &= ~(samplesize-1);

done:
if (src)
{
if (freesrc)
SDL_RWclose(src);
else
SDL_RWseek(src, 0, RW_SEEK_SET);
if (freesrc && src) {
SDL_RWclose(src);
}

if ( was_error )
if (was_error) {
spec = NULL;
}

return(spec);
} /* Mix_LoadVOC_RW */
Expand Down
2 changes: 1 addition & 1 deletion mixer.c
Expand Up @@ -561,7 +561,7 @@ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
/* Make sure audio has been opened */
if ( ! audio_opened ) {
SDL_SetError("Audio device hasn't been opened");
if ( freesrc && src ) {
if ( freesrc ) {
SDL_RWclose(src);
}
return(NULL);
Expand Down

0 comments on commit 852f831

Please sign in to comment.