Skip to content

Commit 3335447

Browse files
author
Victor Stinner
committed
Check for PyUnicode_AS_UNICODE() failure
1 parent 53b33e7 commit 3335447

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

Modules/_ctypes/cfield.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,12 +1472,15 @@ BSTR_set(void *ptr, PyObject *value, Py_ssize_t size)
14721472
/* create a BSTR from value */
14731473
if (value) {
14741474
Py_ssize_t size = PyUnicode_GET_SIZE(value);
1475+
wchar_t* wvalue;
14751476
if ((unsigned) size != size) {
14761477
PyErr_SetString(PyExc_ValueError, "String too long for BSTR");
14771478
return NULL;
14781479
}
1479-
bstr = SysAllocStringLen(PyUnicode_AS_UNICODE(value),
1480-
(unsigned)size);
1480+
wvalue = PyUnicode_AsUnicode(value);
1481+
if (wvalue == NULL)
1482+
return NULL;
1483+
bstr = SysAllocStringLen(wvalue, (unsigned)size);
14811484
Py_DECREF(value);
14821485
} else
14831486
bstr = NULL;

PC/_subprocess.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ sp_CreateProcess(PyObject* self, PyObject* args)
417417
PROCESS_INFORMATION pi;
418418
STARTUPINFOW si;
419419
PyObject* environment;
420+
wchar_t *wenvironment;
420421

421422
Py_UNICODE* application_name;
422423
Py_UNICODE* command_line;
@@ -461,14 +462,25 @@ sp_CreateProcess(PyObject* self, PyObject* args)
461462
return NULL;
462463
}
463464

465+
if (environment) {
466+
wenvironment = PyUnicode_AsUnicode(environment)
467+
if (wenvironment == NULL)
468+
{
469+
Py_XDECREF(environment);
470+
return NULL;
471+
}
472+
}
473+
else
474+
wenvironment = NULL;
475+
464476
Py_BEGIN_ALLOW_THREADS
465477
result = CreateProcessW(application_name,
466478
command_line,
467479
NULL,
468480
NULL,
469481
inherit_handles,
470482
creation_flags | CREATE_UNICODE_ENVIRONMENT,
471-
environment ? PyUnicode_AS_UNICODE(environment) : NULL,
483+
wenvironment,
472484
current_directory,
473485
&si,
474486
&pi);

PC/import_nt.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ _PyWin_FindRegisteredModule(PyObject *moduleName,
3535
wchar_t pathBuf[MAXPATHLEN+1];
3636
int pathLen = MAXPATHLEN+1;
3737
PyObject *path, *moduleKey, *suffix;
38+
wchar_t *wmoduleKey, *wsuffix;
3839
struct filedescr *fdp;
3940
HKEY keyBase;
4041
int modNameSize;
@@ -52,17 +53,22 @@ _PyWin_FindRegisteredModule(PyObject *moduleName,
5253
PyWin_DLLVersionString, moduleName);
5354
if (moduleKey == NULL)
5455
return NULL;
56+
wmoduleKey = PyUnicode_AsUnicode(moduleKey);
57+
if (wmoduleKey == NULL) {
58+
Py_DECREF(moduleKey);
59+
return NULL;
60+
}
5561

5662
keyBase = HKEY_CURRENT_USER;
5763
modNameSize = pathLen;
58-
regStat = RegQueryValueW(keyBase, PyUnicode_AS_UNICODE(moduleKey),
64+
regStat = RegQueryValueW(keyBase, wmoduleKey,
5965
pathBuf, &modNameSize);
6066
if (regStat != ERROR_SUCCESS) {
6167
/* No user setting - lookup in machine settings */
6268
keyBase = HKEY_LOCAL_MACHINE;
6369
/* be anal - failure may have reset size param */
6470
modNameSize = pathLen;
65-
regStat = RegQueryValueW(keyBase, PyUnicode_AS_UNICODE(moduleKey),
71+
regStat = RegQueryValueW(keyBase, wmoduleKey,
6672
pathBuf, &modNameSize);
6773
if (regStat != ERROR_SUCCESS) {
6874
Py_DECREF(moduleKey);
@@ -80,10 +86,15 @@ _PyWin_FindRegisteredModule(PyObject *moduleName,
8086
suffix = PyUnicode_FromString(fdp->suffix);
8187
if (suffix == NULL)
8288
return NULL;
89+
wsuffix = PyUnicode_AsUnicode(suffix);
90+
if (wsuffix == NULL) {
91+
Py_DECREF(suffix);
92+
return NULL;
93+
}
8394
extLen = PyUnicode_GET_SIZE(suffix);
8495
if ((Py_ssize_t)modNameSize > extLen &&
8596
_wcsnicmp(pathBuf + ((Py_ssize_t)modNameSize-extLen-1),
86-
PyUnicode_AS_UNICODE(suffix),
97+
wsuffix,
8798
extLen) == 0)
8899
{
89100
Py_DECREF(suffix);

Python/dynload_win.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,16 @@ dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
176176
{
177177
dl_funcptr p;
178178
char funcname[258], *import_python;
179+
wchar_t *wpathname;
179180

180181
#ifndef _DEBUG
181182
_Py_CheckPython3();
182183
#endif
183184

185+
wpathname = PyUnicode_AsUnicode(pathname);
186+
if (wpathname == NULL)
187+
return NULL;
188+
184189
PyOS_snprintf(funcname, sizeof(funcname), "PyInit_%.200s", shortname);
185190

186191
{
@@ -195,7 +200,7 @@ dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
195200
/* We use LoadLibraryEx so Windows looks for dependent DLLs
196201
in directory of pathname first. */
197202
/* XXX This call doesn't exist in Windows CE */
198-
hDLL = LoadLibraryExW(PyUnicode_AS_UNICODE(pathname), NULL,
203+
hDLL = LoadLibraryExW(wpathname, NULL,
199204
LOAD_WITH_ALTERED_SEARCH_PATH);
200205
_Py_DeactivateActCtx(cookie);
201206

0 commit comments

Comments
 (0)