Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 071864a

Browse files
committed
More Windows changes.
* After discussion with Trent, all INT_PTR references have been removed in favour of the HANDLE it should always have been. Trent can see no 64bit issues here. * In this process, I noticed that the close operation was dangerous, in that we could end up passing bogus results to the Win32 API. These result of the API functions passed the bogus values were never (and still are not) checked, but this is closer to "the right thing" (tm) than before. Tested on Windows and Linux.
1 parent 2cbed00 commit 071864a

1 file changed

Lines changed: 38 additions & 24 deletions

File tree

Modules/mmapmodule.c

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
/ Note: This module currently only deals with 32-bit file
1313
/ sizes.
1414
/
15-
/ The latest version of mmapfile is maintained by Sam at
15+
/ This version of mmapmodule.c has been changed significantly
16+
/ from the original mmapfile.c on which it was based.
17+
/ The original version of mmapfile is maintained by Sam at
1618
/ ftp://squirl.nightmare.com/pub/python/python-ext.
1719
*/
1820

@@ -24,9 +26,6 @@
2426

2527
#ifdef MS_WIN32
2628
#include <windows.h>
27-
#if _MSC_VER < 1200
28-
#define INT_PTR unsigned long
29-
#endif
3029
#endif
3130

3231
#ifdef UNIX
@@ -48,7 +47,7 @@ typedef struct {
4847

4948
#ifdef MS_WIN32
5049
HANDLE map_handle;
51-
INT_PTR file_handle;
50+
HANDLE file_handle;
5251
char * tagname;
5352
#endif
5453

@@ -65,8 +64,8 @@ mmap_object_dealloc(mmap_object *m_obj)
6564
UnmapViewOfFile (m_obj->data);
6665
if (m_obj->map_handle != INVALID_HANDLE_VALUE)
6766
CloseHandle (m_obj->map_handle);
68-
if ((HANDLE)m_obj->file_handle != INVALID_HANDLE_VALUE)
69-
CloseHandle ((HANDLE)m_obj->file_handle);
67+
if (m_obj->file_handle != INVALID_HANDLE_VALUE)
68+
CloseHandle (m_obj->file_handle);
7069
if (m_obj->tagname)
7170
PyMem_Free(m_obj->tagname);
7271
#endif /* MS_WIN32 */
@@ -87,10 +86,25 @@ mmap_close_method(mmap_object *self, PyObject *args)
8786
if (!PyArg_ParseTuple(args, ":close"))
8887
return NULL;
8988
#ifdef MS_WIN32
90-
UnmapViewOfFile (self->data);
91-
CloseHandle (self->map_handle);
92-
CloseHandle ((HANDLE)self->file_handle);
93-
self->map_handle = (HANDLE) NULL;
89+
/* For each resource we maintain, we need to check
90+
the value is valid, and if so, free the resource
91+
and set the member value to an invalid value so
92+
the dealloc does not attempt to resource clearing
93+
again.
94+
TODO - should we check for errors in the close operations???
95+
*/
96+
if (self->data != NULL) {
97+
UnmapViewOfFile (self->data);
98+
self->data = NULL;
99+
}
100+
if (self->map_handle != INVALID_HANDLE_VALUE) {
101+
CloseHandle (self->map_handle);
102+
self->map_handle = INVALID_HANDLE_VALUE;
103+
}
104+
if (self->file_handle != INVALID_HANDLE_VALUE) {
105+
CloseHandle (self->file_handle);
106+
self->file_handle = INVALID_HANDLE_VALUE;
107+
}
94108
#endif /* MS_WIN32 */
95109

96110
#ifdef UNIX
@@ -263,10 +277,10 @@ mmap_size_method(mmap_object *self,
263277
return NULL;
264278

265279
#ifdef MS_WIN32
266-
if (self->file_handle != (INT_PTR) -1) {
280+
if (self->file_handle != INVALID_HANDLE_VALUE) {
267281
return (Py_BuildValue (
268282
"l", (long)
269-
GetFileSize ((HANDLE)self->file_handle, NULL)));
283+
GetFileSize (self->file_handle, NULL)));
270284
} else {
271285
return (Py_BuildValue ("l", (long) self->size) );
272286
}
@@ -307,15 +321,15 @@ mmap_resize_method(mmap_object *self,
307321
/* First, unmap the file view */
308322
UnmapViewOfFile (self->data);
309323
/* Close the mapping object */
310-
CloseHandle ((HANDLE)self->map_handle);
324+
CloseHandle (self->map_handle);
311325
/* Move to the desired EOF position */
312-
SetFilePointer ((HANDLE)self->file_handle,
326+
SetFilePointer (self->file_handle,
313327
new_size, NULL, FILE_BEGIN);
314328
/* Change the size of the file */
315-
SetEndOfFile ((HANDLE)self->file_handle);
329+
SetEndOfFile (self->file_handle);
316330
/* Create another mapping object and remap the file view */
317331
self->map_handle = CreateFileMapping (
318-
(HANDLE) self->file_handle,
332+
self->file_handle,
319333
NULL,
320334
PAGE_READWRITE,
321335
0,
@@ -802,7 +816,7 @@ new_mmap_object(PyObject *self, PyObject *args)
802816

803817
DWORD dwErr = 0;
804818
int fileno;
805-
INT_PTR fh = 0;
819+
HANDLE fh = 0;
806820

807821
/* Patch the object type */
808822
mmap_object_type.ob_type = &PyType_Type;
@@ -821,8 +835,8 @@ new_mmap_object(PyObject *self, PyObject *args)
821835

822836
/* if an actual filename has been specified */
823837
if (fileno != 0) {
824-
fh = _get_osfhandle(fileno);
825-
if (fh==-1) {
838+
fh = (HANDLE)_get_osfhandle(fileno);
839+
if (fh==(HANDLE)-1) {
826840
PyErr_SetFromErrno(mmap_module_error);
827841
return NULL;
828842
}
@@ -836,7 +850,7 @@ new_mmap_object(PyObject *self, PyObject *args)
836850
/* Set every field to an invalid marker, so we can safely
837851
destruct the object in the face of failure */
838852
m_obj->data = NULL;
839-
m_obj->file_handle = (INT_PTR)INVALID_HANDLE_VALUE;
853+
m_obj->file_handle = INVALID_HANDLE_VALUE;
840854
m_obj->map_handle = INVALID_HANDLE_VALUE;
841855
m_obj->tagname = NULL;
842856

@@ -845,7 +859,7 @@ new_mmap_object(PyObject *self, PyObject *args)
845859
Python code can close it on us */
846860
if (!DuplicateHandle(
847861
GetCurrentProcess(), /* source process handle */
848-
(HANDLE)fh, /* handle to be duplicated */
862+
fh, /* handle to be duplicated */
849863
GetCurrentProcess(), /* target proc handle */
850864
(LPHANDLE)&m_obj->file_handle, /* result */
851865
0, /* access - ignored due to options value */
@@ -857,7 +871,7 @@ new_mmap_object(PyObject *self, PyObject *args)
857871
return NULL;
858872
}
859873
if (!map_size) {
860-
m_obj->size = GetFileSize ((HANDLE)fh, NULL);
874+
m_obj->size = GetFileSize (fh, NULL);
861875
} else {
862876
m_obj->size = map_size;
863877
}
@@ -882,7 +896,7 @@ new_mmap_object(PyObject *self, PyObject *args)
882896
else
883897
m_obj->tagname = NULL;
884898

885-
m_obj->map_handle = CreateFileMapping ((HANDLE) m_obj->file_handle,
899+
m_obj->map_handle = CreateFileMapping (m_obj->file_handle,
886900
NULL,
887901
PAGE_READWRITE,
888902
0,

0 commit comments

Comments
 (0)