Don't leak the readahead buffer if win32 rwops file open fails. SDL-1.2
authorRyan C. Gordon <icculus@icculus.org>
Tue, 10 Jul 2007 15:03:19 +0000
branchSDL-1.2
changeset 40353ccd06fc22e5
parent 4034 4ea82f5e7e2c
child 4036 eb60dd682e5e
Don't leak the readahead buffer if win32 rwops file open fails.
src/file/SDL_rwops.c
test/Makefile.in
test/README
     1.1 --- a/src/file/SDL_rwops.c	Tue Jul 10 14:16:44 2007 +0000
     1.2 +++ b/src/file/SDL_rwops.c	Tue Jul 10 15:03:19 2007 +0000
     1.3 @@ -59,12 +59,7 @@
     1.4  		return -1;
     1.5  		
     1.6  	context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
     1.7 -
     1.8 -	context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
     1.9 -	if (!context->hidden.win32io.buffer.data) {
    1.10 -		SDL_OutOfMemory();
    1.11 -		return -1;
    1.12 -	}
    1.13 +	context->hidden.win32io.buffer.data = NULL;
    1.14  	context->hidden.win32io.buffer.size = 0;
    1.15  	context->hidden.win32io.buffer.left = 0;
    1.16  
    1.17 @@ -84,6 +79,12 @@
    1.18  	if (!r_right && !w_right) /* inconsistent mode */
    1.19  		return -1; /* failed (invalid call)*/
    1.20  
    1.21 +	context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
    1.22 +	if (!context->hidden.win32io.buffer.data) {
    1.23 +		SDL_OutOfMemory();
    1.24 +		return -1;
    1.25 +	}
    1.26 +
    1.27  #ifdef _WIN32_WCE
    1.28  	{
    1.29  		size_t size = SDL_strlen(filename)+1;
    1.30 @@ -92,6 +93,8 @@
    1.31  		if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
    1.32  			SDL_SetError("Unable to convert filename to Unicode");
    1.33  			SDL_stack_free(filenameW);
    1.34 +			SDL_free(context->hidden.win32io.buffer.data);
    1.35 +			context->hidden.win32io.buffer.data = NULL;
    1.36  			return -1;
    1.37  		}
    1.38  		h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 
    1.39 @@ -111,11 +114,13 @@
    1.40  
    1.41  	if (h==INVALID_HANDLE_VALUE) {
    1.42  		SDL_SetError("Couldn't open %s",filename);
    1.43 +		SDL_free(context->hidden.win32io.buffer.data);
    1.44 +		context->hidden.win32io.buffer.data = NULL;
    1.45  		return -2; /* failed (CreateFile) */
    1.46  	}
    1.47  	context->hidden.win32io.h = h;
    1.48  	context->hidden.win32io.append = a_mode;
    1.49 -	
    1.50 +
    1.51  	return 0; /* ok */
    1.52  }
    1.53  static int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence)
     2.1 --- a/test/Makefile.in	Tue Jul 10 14:16:44 2007 +0000
     2.2 +++ b/test/Makefile.in	Tue Jul 10 15:03:19 2007 +0000
     2.3 @@ -7,7 +7,7 @@
     2.4  CFLAGS  = @CFLAGS@
     2.5  LIBS	= @LIBS@
     2.6  
     2.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE)
     2.8 +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testrwops$(EXE) testsem$(EXE) testsprite$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE)
     2.9  
    2.10  all: $(TARGETS)
    2.11  
    2.12 @@ -77,6 +77,9 @@
    2.13  testplatform$(EXE): $(srcdir)/testplatform.c
    2.14  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    2.15  
    2.16 +testrwops$(EXE): $(srcdir)/testrwops.c
    2.17 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    2.18 +
    2.19  testsem$(EXE): $(srcdir)/testsem.c
    2.20  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    2.21  
     3.1 --- a/test/README	Tue Jul 10 14:16:44 2007 +0000
     3.2 +++ b/test/README	Tue Jul 10 15:03:19 2007 +0000
     3.3 @@ -24,6 +24,7 @@
     3.4  	testoverlay2	Tests the overlay flickering/scaling during playback.
     3.5  	testpalette	Tests palette color cycling
     3.6  	testplatform	Tests types, endianness and cpu capabilities
     3.7 +	testrwops	Stress-test file i/o API
     3.8  	testsem		Tests SDL's semaphore implementation
     3.9  	testsprite	Example of fast sprite movement on the screen
    3.10  	testtimer	Test the timer facilities