comparison src/file/SDL_rwops.c @ 4035:3ccd06fc22e5 SDL-1.2

Don't leak the readahead buffer if win32 rwops file open fails.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 10 Jul 2007 15:03:19 +0000
parents 407ecf3ad3b3
children eeb0862b9697
comparison
equal deleted inserted replaced
4034:4ea82f5e7e2c 4035:3ccd06fc22e5
57 57
58 if (!context) 58 if (!context)
59 return -1; 59 return -1;
60 60
61 context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */ 61 context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
62 62 context->hidden.win32io.buffer.data = NULL;
63 context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
64 if (!context->hidden.win32io.buffer.data) {
65 SDL_OutOfMemory();
66 return -1;
67 }
68 context->hidden.win32io.buffer.size = 0; 63 context->hidden.win32io.buffer.size = 0;
69 context->hidden.win32io.buffer.left = 0; 64 context->hidden.win32io.buffer.left = 0;
70 65
71 /* "r" = reading, file must exist */ 66 /* "r" = reading, file must exist */
72 /* "w" = writing, truncate existing, file may not exist */ 67 /* "w" = writing, truncate existing, file may not exist */
82 w_right = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0; 77 w_right = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0;
83 78
84 if (!r_right && !w_right) /* inconsistent mode */ 79 if (!r_right && !w_right) /* inconsistent mode */
85 return -1; /* failed (invalid call)*/ 80 return -1; /* failed (invalid call)*/
86 81
82 context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
83 if (!context->hidden.win32io.buffer.data) {
84 SDL_OutOfMemory();
85 return -1;
86 }
87
87 #ifdef _WIN32_WCE 88 #ifdef _WIN32_WCE
88 { 89 {
89 size_t size = SDL_strlen(filename)+1; 90 size_t size = SDL_strlen(filename)+1;
90 wchar_t *filenameW = SDL_stack_alloc(wchar_t, size); 91 wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
91 92
92 if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) { 93 if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
93 SDL_SetError("Unable to convert filename to Unicode"); 94 SDL_SetError("Unable to convert filename to Unicode");
94 SDL_stack_free(filenameW); 95 SDL_stack_free(filenameW);
96 SDL_free(context->hidden.win32io.buffer.data);
97 context->hidden.win32io.buffer.data = NULL;
95 return -1; 98 return -1;
96 } 99 }
97 h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 100 h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
98 NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL); 101 NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
99 SDL_stack_free(filenameW); 102 SDL_stack_free(filenameW);
109 SetErrorMode(old_error_mode); 112 SetErrorMode(old_error_mode);
110 #endif /* _WIN32_WCE */ 113 #endif /* _WIN32_WCE */
111 114
112 if (h==INVALID_HANDLE_VALUE) { 115 if (h==INVALID_HANDLE_VALUE) {
113 SDL_SetError("Couldn't open %s",filename); 116 SDL_SetError("Couldn't open %s",filename);
117 SDL_free(context->hidden.win32io.buffer.data);
118 context->hidden.win32io.buffer.data = NULL;
114 return -2; /* failed (CreateFile) */ 119 return -2; /* failed (CreateFile) */
115 } 120 }
116 context->hidden.win32io.h = h; 121 context->hidden.win32io.h = h;
117 context->hidden.win32io.append = a_mode; 122 context->hidden.win32io.append = a_mode;
118 123
119 return 0; /* ok */ 124 return 0; /* ok */
120 } 125 }
121 static int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence) 126 static int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence)
122 { 127 {
123 DWORD win32whence; 128 DWORD win32whence;