[Python-checkins] r69942 - sandbox/trunk/mmap/Modules/mmapmodule.c

hirokazu.yamamoto python-checkins at python.org
Tue Feb 24 21:18:29 CET 2009


Author: hirokazu.yamamoto
Date: Tue Feb 24 21:18:28 2009
New Revision: 69942

Log:
Fixed crash bug bellow. After mmap.resize raised error, self->map_handle
becomes NULL, but it should become INVALID_HANDLE_VALUE otherwise
CHECK_VALID is passed through.

import mmap
m = mmap.mmap(-1, 5)
try:
    m.resize(-1)
except WindowsError:
    pass
m[0] = '1' # crash

Modified:
   sandbox/trunk/mmap/Modules/mmapmodule.c

Modified: sandbox/trunk/mmap/Modules/mmapmodule.c
==============================================================================
--- sandbox/trunk/mmap/Modules/mmapmodule.c	(original)
+++ sandbox/trunk/mmap/Modules/mmapmodule.c	Tue Feb 24 21:18:28 2009
@@ -432,10 +432,13 @@
 	} else {
 		DWORD dwErrCode = 0;
 		DWORD off_hi, off_lo, newSizeLow, newSizeHigh;
+		HANDLE map_handle;
 		/* First, unmap the file view */
 		UnmapViewOfFile(self->data);
+		self->data = NULL;
 		/* Close the mapping object */
 		CloseHandle(self->map_handle);
+		self->map_handle = INVALID_HANDLE_VALUE;
 		/* Move to the desired EOF position */
 #if SIZEOF_SIZE_T > 4
 		newSizeHigh = (DWORD)((self->offset + new_size) >> 32);
@@ -453,25 +456,27 @@
 		/* Change the size of the file */
 		SetEndOfFile(self->file_handle);
 		/* Create another mapping object and remap the file view */
-		self->map_handle = CreateFileMapping(
+		map_handle = CreateFileMapping(
 			self->file_handle,
 			NULL,
 			PAGE_READWRITE,
 			0,
 			0,
 			self->tagname);
-		if (self->map_handle != NULL) {
-			self->data = (char *) MapViewOfFile(self->map_handle,
+		if (map_handle != NULL) {
+			self->data = (char *) MapViewOfFile(map_handle,
 							    FILE_MAP_WRITE,
 							    off_hi,
 							    off_lo,
 							    new_size);
 			if (self->data != NULL) {
+				self->map_handle = map_handle;
 				self->size = new_size;
 				Py_INCREF(Py_None);
 				return Py_None;
 			} else {
 				dwErrCode = GetLastError();
+				CloseHandle(map_handle);
 			}
 		} else {
 			dwErrCode = GetLastError();
@@ -1212,6 +1217,7 @@
 	HANDLE fh = 0;
 	int access = (access_mode)ACCESS_DEFAULT;
 	DWORD flProtect, dwDesiredAccess;
+	HANDLE map_handle;
 	static char *keywords[] = { "fileno", "length",
                                           "tagname",
                                           "access", "offset", NULL };
@@ -1355,22 +1361,26 @@
 #endif
 	/* For files, it would be sufficient to pass 0 as size.
 	   For anonymous maps, we have to pass the size explicitly. */
-	m_obj->map_handle = CreateFileMapping(m_obj->file_handle,
-					      NULL,
-					      flProtect,
-					      size_hi,
-					      size_lo,
-					      m_obj->tagname);
-	if (m_obj->map_handle != NULL) {
-		m_obj->data = (char *) MapViewOfFile(m_obj->map_handle,
+	map_handle = CreateFileMapping(m_obj->file_handle,
+				       NULL,
+				       flProtect,
+				       size_hi,
+				       size_lo,
+				       m_obj->tagname);
+	if (map_handle != NULL) {
+		m_obj->data = (char *) MapViewOfFile(map_handle,
 						     dwDesiredAccess,
 						     off_hi,
 						     off_lo,
 						     m_obj->size);
-		if (m_obj->data != NULL)
+		if (m_obj->data != NULL) {
+			m_obj->map_handle = map_handle;
 			return (PyObject *)m_obj;
-		else
+		}
+		else {
 			dwErr = GetLastError();
+			CloseHandle(map_handle);
+		}
 	} else
 		dwErr = GetLastError();
 	Py_DECREF(m_obj);


More information about the Python-checkins mailing list