summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorZackery Spytz <zspytz@gmail.com>2024-01-11 22:39:47 (GMT)
committerGitHub <noreply@github.com>2024-01-11 22:39:47 (GMT)
commitb4d4aa9e8d61476267951c72321fadffc2d82227 (patch)
tree78e349a3290201e0fc983401e048d917029ba377 /Modules
parent2f126a70f36e36dd90db53ebdcdff9b990cf3452 (diff)
downloadcpython-b4d4aa9e8d61476267951c72321fadffc2d82227.zip
cpython-b4d4aa9e8d61476267951c72321fadffc2d82227.tar.gz
cpython-b4d4aa9e8d61476267951c72321fadffc2d82227.tar.bz2
gh-81489: Use Unicode APIs for mmap tagname on Windows (GH-14133)
Co-authored-by: Erlend E. Aasland <erlend@python.org>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/mmapmodule.c34
1 files changed, 15 insertions, 19 deletions
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 66ed0b8..d0014e3 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -32,10 +32,6 @@
# include <unistd.h> // close()
#endif
-// to support MS_WINDOWS_SYSTEM OpenFileMappingA / CreateFileMappingA
-// need to be replaced with OpenFileMappingW / CreateFileMappingW
-#if !defined(MS_WINDOWS) || defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_GAMES)
-
#ifndef MS_WINDOWS
#define UNIX
# ifdef HAVE_FCNTL_H
@@ -116,7 +112,7 @@ typedef struct {
#ifdef MS_WINDOWS
HANDLE map_handle;
HANDLE file_handle;
- char * tagname;
+ wchar_t * tagname;
#endif
#ifdef UNIX
@@ -534,7 +530,7 @@ mmap_resize_method(mmap_object *self,
CloseHandle(self->map_handle);
/* if the file mapping still exists, it cannot be resized. */
if (self->tagname) {
- self->map_handle = OpenFileMappingA(FILE_MAP_WRITE, FALSE,
+ self->map_handle = OpenFileMappingW(FILE_MAP_WRITE, FALSE,
self->tagname);
if (self->map_handle) {
PyErr_SetFromWindowsErr(ERROR_USER_MAPPED_FILE);
@@ -563,7 +559,7 @@ mmap_resize_method(mmap_object *self,
/* create a new file mapping and map a new view */
/* FIXME: call CreateFileMappingW with wchar_t tagname */
- self->map_handle = CreateFileMappingA(
+ self->map_handle = CreateFileMappingW(
self->file_handle,
NULL,
PAGE_READWRITE,
@@ -845,7 +841,7 @@ mmap__sizeof__method(mmap_object *self, void *Py_UNUSED(ignored))
{
size_t res = _PyObject_SIZE(Py_TYPE(self));
if (self->tagname) {
- res += strlen(self->tagname) + 1;
+ res += (wcslen(self->tagname) + 1) * sizeof(self->tagname[0]);
}
return PyLong_FromSize_t(res);
}
@@ -1400,7 +1396,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
DWORD off_lo; /* lower 32 bits of offset */
DWORD size_hi; /* upper 32 bits of size */
DWORD size_lo; /* lower 32 bits of size */
- const char *tagname = "";
+ PyObject *tagname = Py_None;
DWORD dwErr = 0;
int fileno;
HANDLE fh = 0;
@@ -1410,7 +1406,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
"tagname",
"access", "offset", NULL };
- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "in|ziL", keywords,
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "in|OiL", keywords,
&fileno, &map_size,
&tagname, &access, &offset)) {
return NULL;
@@ -1543,17 +1539,19 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
m_obj->weakreflist = NULL;
m_obj->exports = 0;
/* set the tag name */
- if (tagname != NULL && *tagname != '\0') {
- m_obj->tagname = PyMem_Malloc(strlen(tagname)+1);
+ if (!Py_IsNone(tagname)) {
+ if (!PyUnicode_Check(tagname)) {
+ Py_DECREF(m_obj);
+ return PyErr_Format(PyExc_TypeError, "expected str or None for "
+ "'tagname', not %.200s",
+ Py_TYPE(tagname)->tp_name);
+ }
+ m_obj->tagname = PyUnicode_AsWideCharString(tagname, NULL);
if (m_obj->tagname == NULL) {
- PyErr_NoMemory();
Py_DECREF(m_obj);
return NULL;
}
- strcpy(m_obj->tagname, tagname);
}
- else
- m_obj->tagname = NULL;
m_obj->access = (access_mode)access;
size_hi = (DWORD)(size >> 32);
@@ -1562,7 +1560,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
off_lo = (DWORD)(offset & 0xFFFFFFFF);
/* 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 = CreateFileMappingA(m_obj->file_handle,
+ m_obj->map_handle = CreateFileMappingW(m_obj->file_handle,
NULL,
flProtect,
size_hi,
@@ -1771,5 +1769,3 @@ PyInit_mmap(void)
{
return PyModuleDef_Init(&mmapmodule);
}
-
-#endif /* !MS_WINDOWS || MS_WINDOWS_DESKTOP || MS_WINDOWS_GAMES */