Skip to content

Commit 6a65eba

Browse files
authored
bpo-39401: Avoid unsafe DLL load on Windows 7 and earlier (GH-18231)
As Windows 7 is not supported by Python 3.9, we just replace the dynamic load with a static import. Backports will have a different fix to ensure they continue to behave the same.
1 parent 0cd5bff commit 6a65eba

File tree

3 files changed

+7
-51
lines changed

3 files changed

+7
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid unsafe DLL load at startup on Windows 7 and earlier.

PC/getpathp.c

+5-50
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
#endif
9292

9393
#include <windows.h>
94+
#include <pathcch.h>
9495
#include <shlwapi.h>
9596

9697
#ifdef HAVE_SYS_TYPES_H
@@ -242,42 +243,14 @@ ismodule(wchar_t *filename, int update_filename)
242243
stuff as fits will be appended.
243244
*/
244245

245-
static int _PathCchCombineEx_Initialized = 0;
246-
typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
247-
PCWSTR pszPathIn, PCWSTR pszMore,
248-
unsigned long dwFlags);
249-
static PPathCchCombineEx _PathCchCombineEx;
250-
251246
static void
252247
join(wchar_t *buffer, const wchar_t *stuff)
253248
{
254-
if (_PathCchCombineEx_Initialized == 0) {
255-
HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
256-
if (pathapi) {
257-
_PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
258-
}
259-
else {
260-
_PathCchCombineEx = NULL;
261-
}
262-
_PathCchCombineEx_Initialized = 1;
263-
}
264-
265-
if (_PathCchCombineEx) {
266-
if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
267-
Py_FatalError("buffer overflow in getpathp.c's join()");
268-
}
269-
} else {
270-
if (!PathCombineW(buffer, buffer, stuff)) {
271-
Py_FatalError("buffer overflow in getpathp.c's join()");
272-
}
249+
if (FAILED(PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
250+
Py_FatalError("buffer overflow in getpathp.c's join()");
273251
}
274252
}
275253

276-
static int _PathCchCanonicalizeEx_Initialized = 0;
277-
typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut,
278-
PCWSTR pszPathIn, unsigned long dwFlags);
279-
static PPathCchCanonicalizeEx _PathCchCanonicalizeEx;
280-
281254
/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
282255
and ".." to produce a direct, well-formed path. */
283256
static PyStatus
@@ -287,26 +260,8 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
287260
return _PyStatus_NO_MEMORY();
288261
}
289262

290-
if (_PathCchCanonicalizeEx_Initialized == 0) {
291-
HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
292-
if (pathapi) {
293-
_PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx");
294-
}
295-
else {
296-
_PathCchCanonicalizeEx = NULL;
297-
}
298-
_PathCchCanonicalizeEx_Initialized = 1;
299-
}
300-
301-
if (_PathCchCanonicalizeEx) {
302-
if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
303-
return INIT_ERR_BUFFER_OVERFLOW();
304-
}
305-
}
306-
else {
307-
if (!PathCanonicalizeW(buffer, path)) {
308-
return INIT_ERR_BUFFER_OVERFLOW();
309-
}
263+
if (FAILED(PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
264+
return INIT_ERR_BUFFER_OVERFLOW();
310265
}
311266
return _PyStatus_OK();
312267
}

PCbuild/pythoncore.vcxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
<PreprocessorDefinitions Condition="$(IncludeExternals)">_Py_HAVE_ZLIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
107107
</ClCompile>
108108
<Link>
109-
<AdditionalDependencies>version.lib;shlwapi.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
109+
<AdditionalDependencies>version.lib;shlwapi.lib;ws2_32.lib;pathcch.lib;%(AdditionalDependencies)</AdditionalDependencies>
110110
</Link>
111111
</ItemDefinitionGroup>
112112
<ItemGroup>

0 commit comments

Comments
 (0)