Reverted most of r3200:3201: a malloc() failure would leave a zero-byte SDL-1.2
authorRyan C. Gordon <icculus@icculus.org>
Tue, 10 Jul 2007 18:56:08 +0000
branchSDL-1.2
changeset 40397fd9a811efc7
parent 4038 eeb0862b9697
child 4040 8515468091e3
Reverted most of r3200:3201: a malloc() failure would leave a zero-byte
file if opening for write.
src/file/SDL_rwops.c
     1.1 --- a/src/file/SDL_rwops.c	Tue Jul 10 16:01:46 2007 +0000
     1.2 +++ b/src/file/SDL_rwops.c	Tue Jul 10 18:56:08 2007 +0000
     1.3 @@ -57,6 +57,11 @@
     1.4  
     1.5  	if (!context)
     1.6  		return -1; /* failed (invalid call) */
     1.7 +		
     1.8 +	context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
     1.9 +	context->hidden.win32io.buffer.data = NULL;
    1.10 +	context->hidden.win32io.buffer.size = 0;
    1.11 +	context->hidden.win32io.buffer.left = 0;
    1.12  
    1.13  	/* "r" = reading, file must exist */
    1.14  	/* "w" = writing, truncate existing, file may not exist */
    1.15 @@ -74,6 +79,12 @@
    1.16  	if (!r_right && !w_right) /* inconsistent mode */
    1.17  		return -1; /* failed (invalid call) */
    1.18  
    1.19 +	context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
    1.20 +	if (!context->hidden.win32io.buffer.data) {
    1.21 +		SDL_OutOfMemory();
    1.22 +		return -1;
    1.23 +	}
    1.24 +
    1.25  #ifdef _WIN32_WCE
    1.26  	{
    1.27  		size_t size = SDL_strlen(filename)+1;
    1.28 @@ -82,6 +93,8 @@
    1.29  		if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
    1.30  			SDL_SetError("Unable to convert filename to Unicode");
    1.31  			SDL_stack_free(filenameW);
    1.32 +			SDL_free(context->hidden.win32io.buffer.data);
    1.33 +			context->hidden.win32io.buffer.data = NULL;
    1.34  			return -1;
    1.35  		}
    1.36  		h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 
    1.37 @@ -101,20 +114,13 @@
    1.38  
    1.39  	if (h==INVALID_HANDLE_VALUE) {
    1.40  		SDL_SetError("Couldn't open %s",filename);
    1.41 +		SDL_free(context->hidden.win32io.buffer.data);
    1.42 +		context->hidden.win32io.buffer.data = NULL;
    1.43  		return -2; /* failed (CreateFile) */
    1.44  	}
    1.45  	context->hidden.win32io.h = h;
    1.46  	context->hidden.win32io.append = a_mode;
    1.47  
    1.48 -	context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
    1.49 -	if (!context->hidden.win32io.buffer.data) {
    1.50 -		SDL_OutOfMemory();
    1.51 -		CloseHandle(context->hidden.win32io.h);
    1.52 -		return -1;
    1.53 -	}
    1.54 -	context->hidden.win32io.buffer.size = 0;
    1.55 -	context->hidden.win32io.buffer.left = 0;
    1.56 -
    1.57  	return 0; /* ok */
    1.58  }
    1.59  static int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence)