Skip to content

Commit 1df0461

Browse files
author
Thomas Heller
committed
When importing an extension on Windows, the code reads the PE 'import
table' of the dll, to make sure that the dll really was build for the correct Python version. It does this by looking for an entry 'pythonXY.dll' (X.Y is the Python version number). The code now checks the size of the dll's import table before reading entries from it. Before this patch, the code crashed trying to read the import table when the size was zero (as in Win2k's wmi.dll, for example). Look for imports of 'pythonXY_d.dll' in a debug build instead of 'pythonXY.dll'. Fixes SF 951851: Crash when reading "import table" of certain windows dlls. Already backported to the 2.3 branch.
1 parent 32b8f80 commit 1df0461

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Python/dynload_win.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ static char *GetPythonImport (HINSTANCE hModule)
116116
string constant holding the import name is located. */
117117

118118
if (DWORD_AT(dllbase + opt_offset + num_dict_off) >= 2) {
119+
/* We have at least 2 tables - the import table is the second
120+
one. But still it may be that the table size is zero */
121+
if (0 == DWORD_AT(dllbase + opt_offset + import_off + sizeof(DWORD)))
122+
return NULL;
119123
import_data = dllbase + DWORD_AT(dllbase +
120124
opt_offset +
121125
import_off);
@@ -128,7 +132,11 @@ static char *GetPythonImport (HINSTANCE hModule)
128132
/* Ensure python prefix is followed only
129133
by numbers to the end of the basename */
130134
pch = import_name + 6;
135+
#ifdef _DEBUG
136+
while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') {
137+
#else
131138
while (*pch && *pch != '.') {
139+
#endif
132140
if (*pch >= '0' && *pch <= '9') {
133141
pch++;
134142
} else {
@@ -221,7 +229,11 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
221229
} else {
222230
char buffer[256];
223231

232+
#ifdef _DEBUG
233+
PyOS_snprintf(buffer, sizeof(buffer), "python%d%d_d.dll",
234+
#else
224235
PyOS_snprintf(buffer, sizeof(buffer), "python%d%d.dll",
236+
#endif
225237
PY_MAJOR_VERSION,PY_MINOR_VERSION);
226238
import_python = GetPythonImport(hDLL);
227239

0 commit comments

Comments
 (0)