-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-106844: Fix null-bytes handling in LCMapStringEx in _winapi #107832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dff8b2f
46be70b
13ce79c
88d4193
73551b9
26161c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix integer overflow and truncating by the null character in :func:`!_winapi.LCMapStringEx` which affects :func:`ntpath.normcase`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1539,40 +1539,56 @@ _winapi.LCMapStringEx | |
|
||
locale: LPCWSTR | ||
flags: DWORD | ||
src: LPCWSTR | ||
src: unicode | ||
|
||
[clinic start generated code]*/ | ||
|
||
static PyObject * | ||
_winapi_LCMapStringEx_impl(PyObject *module, LPCWSTR locale, DWORD flags, | ||
LPCWSTR src) | ||
/*[clinic end generated code: output=cf4713d80e2b47c9 input=9fe26f95d5ab0001]*/ | ||
PyObject *src) | ||
/*[clinic end generated code: output=b90e6b26e028ff0a input=3e3dcd9b8164012f]*/ | ||
{ | ||
if (flags & (LCMAP_SORTHANDLE | LCMAP_HASH | LCMAP_BYTEREV | | ||
LCMAP_SORTKEY)) { | ||
return PyErr_Format(PyExc_ValueError, "unsupported flags"); | ||
} | ||
|
||
int dest_size = LCMapStringEx(locale, flags, src, -1, NULL, 0, | ||
Py_ssize_t src_size; | ||
wchar_t *src_ = PyUnicode_AsWideCharString(src, &src_size); | ||
if (!src_) { | ||
return NULL; | ||
} | ||
if (src_size > INT_MAX) { | ||
PyMem_Free(src_); | ||
PyErr_SetString(PyExc_OverflowError, "input string is too long"); | ||
return NULL; | ||
} | ||
|
||
int dest_size = LCMapStringEx(locale, flags, src_, (int)src_size, NULL, 0, | ||
NULL, NULL, 0); | ||
if (dest_size == 0) { | ||
return PyErr_SetFromWindowsErr(0); | ||
if (dest_size <= 0) { | ||
DWORD error = GetLastError(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other code calls |
||
PyMem_Free(src_); | ||
return PyErr_SetFromWindowsErr(error); | ||
} | ||
|
||
wchar_t* dest = PyMem_NEW(wchar_t, dest_size); | ||
if (dest == NULL) { | ||
PyMem_Free(src_); | ||
return PyErr_NoMemory(); | ||
} | ||
|
||
int nmapped = LCMapStringEx(locale, flags, src, -1, dest, dest_size, | ||
int nmapped = LCMapStringEx(locale, flags, src_, (int)src_size, dest, dest_size, | ||
NULL, NULL, 0); | ||
if (nmapped == 0) { | ||
if (nmapped <= 0) { | ||
DWORD error = GetLastError(); | ||
PyMem_Free(src_); | ||
PyMem_DEL(dest); | ||
return PyErr_SetFromWindowsErr(error); | ||
} | ||
|
||
PyObject *ret = PyUnicode_FromWideChar(dest, dest_size - 1); | ||
PyMem_Free(src_); | ||
PyObject *ret = PyUnicode_FromWideChar(dest, nmapped); | ||
Comment on lines
+1590
to
+1591
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It reduces the maximal total memory consumption if free this memory before allocating new memory in |
||
PyMem_DEL(dest); | ||
|
||
return ret; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can return negative value. At least if
src_size
is negative. But the second call returns 0 anyway.