bpo-36346: Do not use legacy Unicode C API in ctypes. by serhiy-storchaka · Pull Request #21429 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions Modules/_ctypes/_ctypes.c
10 changes: 5 additions & 5 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,6 @@ module. load_flags are as defined for LoadLibraryEx in the\n\
Windows API.\n";
static PyObject *load_library(PyObject *self, PyObject *args)
{
const WCHAR *name;
PyObject *nameobj;
int load_flags = 0;
HMODULE hMod;
Expand All @@ -1309,14 +1308,14 @@ static PyObject *load_library(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "U|i:LoadLibrary", &nameobj, &load_flags))
return NULL;

name = _PyUnicode_AsUnicode(nameobj);
if (!name)
return NULL;

if (PySys_Audit("ctypes.dlopen", "O", nameobj) < 0) {
return NULL;
}

WCHAR *name = PyUnicode_AsWideCharString(nameobj, NULL);
if (!name)
return NULL;

Py_BEGIN_ALLOW_THREADS
/* bpo-36085: Limit DLL search directories to avoid pre-loading
* attacks and enable use of the AddDllDirectory function.
Expand All @@ -1325,6 +1324,7 @@ static PyObject *load_library(PyObject *self, PyObject *args)
err = hMod ? 0 : GetLastError();
Py_END_ALLOW_THREADS

PyMem_Free(name);
if (err == ERROR_MOD_NOT_FOUND) {
PyErr_Format(PyExc_FileNotFoundError,
("Could not find module '%.500S' (or one of its "
Expand Down
7 changes: 2 additions & 5 deletions Modules/_ctypes/cfield.c