Skip to content

Commit 98558a8

Browse files
authored
bpo-42658: Allow _winapi.LCMapStringEx to handle embedded nulls (GH-93688)
1 parent aee7d3d commit 98558a8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Modules/_winapi.c

+10-4
Original file line numberDiff line numberDiff line change
@@ -1535,13 +1535,19 @@ _winapi_LCMapStringEx_impl(PyObject *module, PyObject *locale, DWORD flags,
15351535
if (!locale_) {
15361536
return NULL;
15371537
}
1538-
wchar_t *src_ = PyUnicode_AsWideCharString(src, NULL);
1538+
Py_ssize_t srcLenAsSsize;
1539+
int srcLen;
1540+
wchar_t *src_ = PyUnicode_AsWideCharString(src, &srcLenAsSsize);
15391541
if (!src_) {
15401542
PyMem_Free(locale_);
15411543
return NULL;
15421544
}
1545+
srcLen = (int)srcLenAsSsize;
1546+
if (srcLen != srcLenAsSsize) {
1547+
srcLen = -1;
1548+
}
15431549

1544-
int dest_size = LCMapStringEx(locale_, flags, src_, -1, NULL, 0,
1550+
int dest_size = LCMapStringEx(locale_, flags, src_, srcLen, NULL, 0,
15451551
NULL, NULL, 0);
15461552
if (dest_size == 0) {
15471553
PyMem_Free(locale_);
@@ -1556,7 +1562,7 @@ _winapi_LCMapStringEx_impl(PyObject *module, PyObject *locale, DWORD flags,
15561562
return PyErr_NoMemory();
15571563
}
15581564

1559-
int nmapped = LCMapStringEx(locale_, flags, src_, -1, dest, dest_size,
1565+
int nmapped = LCMapStringEx(locale_, flags, src_, srcLen, dest, dest_size,
15601566
NULL, NULL, 0);
15611567
if (nmapped == 0) {
15621568
DWORD error = GetLastError();
@@ -1566,7 +1572,7 @@ _winapi_LCMapStringEx_impl(PyObject *module, PyObject *locale, DWORD flags,
15661572
return PyErr_SetFromWindowsErr(error);
15671573
}
15681574

1569-
PyObject *ret = PyUnicode_FromWideChar(dest, dest_size - 1);
1575+
PyObject *ret = PyUnicode_FromWideChar(dest, dest_size);
15701576
PyMem_Free(locale_);
15711577
PyMem_Free(src_);
15721578
PyMem_DEL(dest);

0 commit comments

Comments
 (0)