summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@python.org>2022-06-10 16:09:37 (GMT)
committerGitHub <noreply@github.com>2022-06-10 16:09:37 (GMT)
commit98558a83976b0279b1404fcb67bc72e7fcf3fe8a (patch)
tree85a2dfc82e3386f0d84379c4595780a50256f8c9
parentaee7d3df1c69129c6f2b88e1631bcdbac2aa34a8 (diff)
downloadcpython-98558a83976b0279b1404fcb67bc72e7fcf3fe8a.zip
cpython-98558a83976b0279b1404fcb67bc72e7fcf3fe8a.tar.gz
cpython-98558a83976b0279b1404fcb67bc72e7fcf3fe8a.tar.bz2
bpo-42658: Allow _winapi.LCMapStringEx to handle embedded nulls (GH-93688)
-rw-r--r--Modules/_winapi.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/Modules/_winapi.c b/Modules/_winapi.c
index a3c30f2..9b30a90 100644
--- a/Modules/_winapi.c
+++ b/Modules/_winapi.c
@@ -1535,13 +1535,19 @@ _winapi_LCMapStringEx_impl(PyObject *module, PyObject *locale, DWORD flags,
if (!locale_) {
return NULL;
}
- wchar_t *src_ = PyUnicode_AsWideCharString(src, NULL);
+ Py_ssize_t srcLenAsSsize;
+ int srcLen;
+ wchar_t *src_ = PyUnicode_AsWideCharString(src, &srcLenAsSsize);
if (!src_) {
PyMem_Free(locale_);
return NULL;
}
+ srcLen = (int)srcLenAsSsize;
+ if (srcLen != srcLenAsSsize) {
+ srcLen = -1;
+ }
- int dest_size = LCMapStringEx(locale_, flags, src_, -1, NULL, 0,
+ int dest_size = LCMapStringEx(locale_, flags, src_, srcLen, NULL, 0,
NULL, NULL, 0);
if (dest_size == 0) {
PyMem_Free(locale_);
@@ -1556,7 +1562,7 @@ _winapi_LCMapStringEx_impl(PyObject *module, PyObject *locale, DWORD flags,
return PyErr_NoMemory();
}
- int nmapped = LCMapStringEx(locale_, flags, src_, -1, dest, dest_size,
+ int nmapped = LCMapStringEx(locale_, flags, src_, srcLen, dest, dest_size,
NULL, NULL, 0);
if (nmapped == 0) {
DWORD error = GetLastError();
@@ -1566,7 +1572,7 @@ _winapi_LCMapStringEx_impl(PyObject *module, PyObject *locale, DWORD flags,
return PyErr_SetFromWindowsErr(error);
}
- PyObject *ret = PyUnicode_FromWideChar(dest, dest_size - 1);
+ PyObject *ret = PyUnicode_FromWideChar(dest, dest_size);
PyMem_Free(locale_);
PyMem_Free(src_);
PyMem_DEL(dest);