Skip to content

Commit 6c1e5ec

Browse files
committed
Revert "bpo-39401: Avoid unsafe DLL load on Windows 7 and earlier (pythonGH-18231)"
This reverts commit 6a65eba.
1 parent 3c3aca9 commit 6c1e5ec

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

PC/getpathp.c

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
#endif
9191

9292
#include <windows.h>
93-
#include <pathcch.h>
93+
#include <shlwapi.h>
9494

9595
#ifdef HAVE_SYS_TYPES_H
9696
#include <sys/types.h>
@@ -249,14 +249,42 @@ ismodule(wchar_t *filename, int update_filename)
249249
stuff as fits will be appended.
250250
*/
251251

252+
static int _PathCchCombineEx_Initialized = 0;
253+
typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
254+
PCWSTR pszPathIn, PCWSTR pszMore,
255+
unsigned long dwFlags);
256+
static PPathCchCombineEx _PathCchCombineEx;
257+
252258
static void
253259
join(wchar_t *buffer, const wchar_t *stuff)
254260
{
255-
if (FAILED(PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
256-
Py_FatalError("buffer overflow in getpathp.c's join()");
261+
if (_PathCchCombineEx_Initialized == 0) {
262+
HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
263+
if (pathapi) {
264+
_PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
265+
}
266+
else {
267+
_PathCchCombineEx = NULL;
268+
}
269+
_PathCchCombineEx_Initialized = 1;
270+
}
271+
272+
if (_PathCchCombineEx) {
273+
if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
274+
Py_FatalError("buffer overflow in getpathp.c's join()");
275+
}
276+
} else {
277+
if (!PathCombineW(buffer, buffer, stuff)) {
278+
Py_FatalError("buffer overflow in getpathp.c's join()");
279+
}
257280
}
258281
}
259282

283+
static int _PathCchCanonicalizeEx_Initialized = 0;
284+
typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut,
285+
PCWSTR pszPathIn, unsigned long dwFlags);
286+
static PPathCchCanonicalizeEx _PathCchCanonicalizeEx;
287+
260288
/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
261289
and ".." to produce a direct, well-formed path. */
262290
static PyStatus
@@ -266,8 +294,26 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
266294
return _PyStatus_NO_MEMORY();
267295
}
268296

269-
if (FAILED(PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
270-
return INIT_ERR_BUFFER_OVERFLOW();
297+
if (_PathCchCanonicalizeEx_Initialized == 0) {
298+
HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
299+
if (pathapi) {
300+
_PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx");
301+
}
302+
else {
303+
_PathCchCanonicalizeEx = NULL;
304+
}
305+
_PathCchCanonicalizeEx_Initialized = 1;
306+
}
307+
308+
if (_PathCchCanonicalizeEx) {
309+
if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
310+
return INIT_ERR_BUFFER_OVERFLOW();
311+
}
312+
}
313+
else {
314+
if (!PathCanonicalizeW(buffer, path)) {
315+
return INIT_ERR_BUFFER_OVERFLOW();
316+
}
271317
}
272318
return _PyStatus_OK();
273319
}

PCbuild/pythoncore.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup Label="ProjectConfigurations">
44
<ProjectConfiguration Include="Debug|ARM">
@@ -106,7 +106,7 @@
106106
<PreprocessorDefinitions Condition="$(IncludeExternals)">_Py_HAVE_ZLIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
107107
</ClCompile>
108108
<Link>
109-
<AdditionalDependencies>version.lib;ws2_32.lib;pathcch.lib;%(AdditionalDependencies)</AdditionalDependencies>
109+
<AdditionalDependencies>version.lib;shlwapi.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
110110
</Link>
111111
</ItemDefinitionGroup>
112112
<ItemGroup>

0 commit comments

Comments
 (0)