Skip to content

Commit 139b521

Browse files
zoobakuba2k2
authored andcommitted
Revert "bpo-39401: Avoid unsafe DLL load on Windows 7 and earlier (pythonGH-18231)"
This reverts commit 6a65eba.
1 parent 7a2abfb commit 139b521

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

PC/getpathp.c

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

9292
#include <windows.h>
93-
#include <pathcch.h>
9493
#include <shlwapi.h>
9594

9695
#ifdef HAVE_SYS_TYPES_H
@@ -250,14 +249,42 @@ ismodule(wchar_t *filename, int update_filename)
250249
stuff as fits will be appended.
251250
*/
252251

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+
253258
static void
254259
join(wchar_t *buffer, const wchar_t *stuff)
255260
{
256-
if (FAILED(PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
257-
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+
}
258280
}
259281
}
260282

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+
261288
/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
262289
and ".." to produce a direct, well-formed path. */
263290
static PyStatus
@@ -267,8 +294,26 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
267294
return _PyStatus_NO_MEMORY();
268295
}
269296

270-
if (FAILED(PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
271-
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+
}
272317
}
273318
return _PyStatus_OK();
274319
}

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 1 deletion
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;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)