changeset 4039:7fd9a811efc7 SDL-1.2

Reverted most of r3200:3201: a malloc() failure would leave a zero-byte file if opening for write.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 10 Jul 2007 18:56:08 +0000
parents eeb0862b9697
children 8515468091e3
files src/file/SDL_rwops.c
diffstat 1 files changed, 15 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/file/SDL_rwops.c	Tue Jul 10 16:01:46 2007 +0000
+++ b/src/file/SDL_rwops.c	Tue Jul 10 18:56:08 2007 +0000
@@ -57,6 +57,11 @@
 
 	if (!context)
 		return -1; /* failed (invalid call) */
+		
+	context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
+	context->hidden.win32io.buffer.data = NULL;
+	context->hidden.win32io.buffer.size = 0;
+	context->hidden.win32io.buffer.left = 0;
 
 	/* "r" = reading, file must exist */
 	/* "w" = writing, truncate existing, file may not exist */
@@ -74,6 +79,12 @@
 	if (!r_right && !w_right) /* inconsistent mode */
 		return -1; /* failed (invalid call) */
 
+	context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
+	if (!context->hidden.win32io.buffer.data) {
+		SDL_OutOfMemory();
+		return -1;
+	}
+
 #ifdef _WIN32_WCE
 	{
 		size_t size = SDL_strlen(filename)+1;
@@ -82,6 +93,8 @@
 		if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
 			SDL_SetError("Unable to convert filename to Unicode");
 			SDL_stack_free(filenameW);
+			SDL_free(context->hidden.win32io.buffer.data);
+			context->hidden.win32io.buffer.data = NULL;
 			return -1;
 		}
 		h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 
@@ -101,20 +114,13 @@
 
 	if (h==INVALID_HANDLE_VALUE) {
 		SDL_SetError("Couldn't open %s",filename);
+		SDL_free(context->hidden.win32io.buffer.data);
+		context->hidden.win32io.buffer.data = NULL;
 		return -2; /* failed (CreateFile) */
 	}
 	context->hidden.win32io.h = h;
 	context->hidden.win32io.append = a_mode;
 
-	context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
-	if (!context->hidden.win32io.buffer.data) {
-		SDL_OutOfMemory();
-		CloseHandle(context->hidden.win32io.h);
-		return -1;
-	}
-	context->hidden.win32io.buffer.size = 0;
-	context->hidden.win32io.buffer.left = 0;
-
 	return 0; /* ok */
 }
 static int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence)