Date: Sun, 7 Sep 2008 15:17:00 +0200 SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Wed, 12 Nov 2008 17:23:40 +0000
branchSDL-1.2
changeset 415896ce26f24b01
parent 4157 baf615f9f2a0
child 4159 a1b03ba2fcd0
Date: Sun, 7 Sep 2008 15:17:00 +0200
From: c2woody@gmx.net
Subject: [SDL] SDL 1.2 doube free/pointer zeroing missing

Hello,

this is about a crash/debug breakage for the current SDL 1.2
source tree (today's svn checkout, same problem in 1.2.13 and
before as far as relevant).
In some places memory is free()d but the associated pointer
is not zeroed, leading to for example double free()s.

For me this happened because SDL_StopEventThread() was executed
twice (during restart of the subsystems), once for the close
down in SDL_VideoQuit() and once at the startup, right at the
beginning of SDL_StartEventLoop(). Thus the code
SDL_DestroyMutex(SDL_EventQ.lock);
(see SDL_events.c) was called twice and executed the SDL_free(mutex);
twice as well, leading to a crash (msvc 64bit for which it was noticed).

I've tried to check all other occurrences of SDL_free and similar
code in msvc, see the attached patch (udiff against revision 4082).
Non-windows only codepaths have neither been checked nor touched.

Comments/ideas welcome.

Attached patch: NULLifies some pointers after they have been free()d.
src/audio/SDL_wave.c
src/cdrom/win32/SDL_syscdrom.c
src/events/SDL_events.c
src/joystick/win32/SDL_mmjoystick.c
src/video/SDL_yuv_sw.c
src/video/windx5/SDL_dx5yuv.c
     1.1 --- a/src/audio/SDL_wave.c	Fri Nov 07 04:15:36 2008 +0000
     1.2 +++ b/src/audio/SDL_wave.c	Wed Nov 12 17:23:40 2008 +0000
     1.3 @@ -440,6 +440,7 @@
     1.4  	do {
     1.5  		if ( chunk.data != NULL ) {
     1.6  			SDL_free(chunk.data);
     1.7 +			chunk.data = NULL;
     1.8  		}
     1.9  		lenread = ReadChunk(src, &chunk);
    1.10  		if ( lenread < 0 ) {
    1.11 @@ -522,6 +523,7 @@
    1.12  	do {
    1.13  		if ( *audio_buf != NULL ) {
    1.14  			SDL_free(*audio_buf);
    1.15 +			*audio_buf = NULL;
    1.16  		}
    1.17  		lenread = ReadChunk(src, &chunk);
    1.18  		if ( lenread < 0 ) {
    1.19 @@ -591,6 +593,7 @@
    1.20  	if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
    1.21  		SDL_Error(SDL_EFREAD);
    1.22  		SDL_free(chunk->data);
    1.23 +		chunk->data = NULL;
    1.24  		return(-1);
    1.25  	}
    1.26  	return(chunk->length);
     2.1 --- a/src/cdrom/win32/SDL_syscdrom.c	Fri Nov 07 04:15:36 2008 +0000
     2.2 +++ b/src/cdrom/win32/SDL_syscdrom.c	Wed Nov 12 17:23:40 2008 +0000
     2.3 @@ -377,6 +377,7 @@
     2.4  	if ( SDL_numcds > 0 ) {
     2.5  		for ( i=0; i<SDL_numcds; ++i ) {
     2.6  			SDL_free(SDL_cdlist[i]);
     2.7 +			SDL_cdlist[i] = NULL;
     2.8  		}
     2.9  		SDL_numcds = 0;
    2.10  	}
     3.1 --- a/src/events/SDL_events.c	Fri Nov 07 04:15:36 2008 +0000
     3.2 +++ b/src/events/SDL_events.c	Wed Nov 12 17:23:40 2008 +0000
     3.3 @@ -191,9 +191,11 @@
     3.4  		SDL_WaitThread(SDL_EventThread, NULL);
     3.5  		SDL_EventThread = NULL;
     3.6  		SDL_DestroyMutex(SDL_EventLock.lock);
     3.7 +		SDL_EventLock.lock = NULL;
     3.8  	}
     3.9  #ifndef IPOD
    3.10  	SDL_DestroyMutex(SDL_EventQ.lock);
    3.11 +	SDL_EventQ.lock = NULL;
    3.12  #endif
    3.13  }
    3.14  
     4.1 --- a/src/joystick/win32/SDL_mmjoystick.c	Fri Nov 07 04:15:36 2008 +0000
     4.2 +++ b/src/joystick/win32/SDL_mmjoystick.c	Wed Nov 12 17:23:40 2008 +0000
     4.3 @@ -344,6 +344,7 @@
     4.4  	if (joystick->hwdata != NULL) {
     4.5  		/* free system specific hardware data */
     4.6  		SDL_free(joystick->hwdata);
     4.7 +		joystick->hwdata = NULL;
     4.8  	}
     4.9  }
    4.10  
    4.11 @@ -354,6 +355,7 @@
    4.12  	for (i = 0; i < MAX_JOYSTICKS; i++) {
    4.13  		if ( SYS_JoystickName[i] != NULL ) {
    4.14  			SDL_free(SYS_JoystickName[i]);
    4.15 +			SYS_JoystickName[i] = NULL;
    4.16  		}
    4.17  	}
    4.18  }
     5.1 --- a/src/video/SDL_yuv_sw.c	Fri Nov 07 04:15:36 2008 +0000
     5.2 +++ b/src/video/SDL_yuv_sw.c	Wed Nov 12 17:23:40 2008 +0000
     5.3 @@ -1294,5 +1294,6 @@
     5.4  			SDL_free(swdata->rgb_2_pix);
     5.5  		}
     5.6  		SDL_free(swdata);
     5.7 +		overlay->hwdata = NULL;
     5.8  	}
     5.9  }
     6.1 --- a/src/video/windx5/SDL_dx5yuv.c	Fri Nov 07 04:15:36 2008 +0000
     6.2 +++ b/src/video/windx5/SDL_dx5yuv.c	Wed Nov 12 17:23:40 2008 +0000
     6.3 @@ -290,6 +290,7 @@
     6.4  			IDirectDrawSurface_Release(hwdata->surface);
     6.5  		}
     6.6  		SDL_free(hwdata);
     6.7 +		overlay->hwdata = NULL;
     6.8  	}
     6.9  }
    6.10