WIN32: Fix pGetSystemWindowsDirectory found initialized during FcConfigParseAndLoadFromMemory

Trying to early loading a custom fonts.xml created on the
fly results in the pGetSystemWindowsDirectory function
pointer being found intialized. Normally the initialization
is performed in the default configuration loading.
The commit factorizes the lazy initialization of both
WIN32 getters (together with pSHGetFolderPathA) in a
function and call it when actually needed.
This commit is contained in:
Francesco Pretto 2021-10-26 19:31:23 +02:00 committed by Akira TAGOH
parent 8a150f634f
commit 16bbb5340b
1 changed files with 26 additions and 18 deletions

View File

@ -59,6 +59,10 @@
#ifdef _WIN32
#include <mbstring.h>
extern FcChar8 fontconfig_instprefix[];
pfnGetSystemWindowsDirectory pGetSystemWindowsDirectory = NULL;
pfnSHGetFolderPathA pSHGetFolderPathA = NULL;
static void
_ensureWin32GettersReady();
#endif
static FcChar8 *__fc_userdir = NULL;
@ -1384,6 +1388,7 @@ _get_real_paths_from_prefix(FcConfigParse *parse, const FcChar8 *path, const FcC
{
int rc;
path = buffer;
_ensureWin32GettersReady();
rc = pGetSystemWindowsDirectory ((LPSTR) buffer, sizeof (buffer) - 20);
if (rc == 0 || rc > sizeof (buffer) - 20)
{
@ -3451,11 +3456,6 @@ bail0:
return ret || !complain;
}
#ifdef _WIN32
pfnGetSystemWindowsDirectory pGetSystemWindowsDirectory = NULL;
pfnSHGetFolderPathA pSHGetFolderPathA = NULL;
#endif
static FcBool
FcConfigParseAndLoadFromMemoryInternal (FcConfig *config,
const FcChar8 *filename,
@ -3601,19 +3601,7 @@ _FcConfigParse (FcConfig *config,
FcStrBufInit (&reason, NULL, 0);
#ifdef _WIN32
if (!pGetSystemWindowsDirectory)
{
HMODULE hk32 = GetModuleHandleA("kernel32.dll");
if (!(pGetSystemWindowsDirectory = (pfnGetSystemWindowsDirectory) GetProcAddress(hk32, "GetSystemWindowsDirectoryA")))
pGetSystemWindowsDirectory = (pfnGetSystemWindowsDirectory) GetWindowsDirectory;
}
if (!pSHGetFolderPathA)
{
HMODULE hSh = LoadLibraryA("shfolder.dll");
/* the check is done later, because there is no provided fallback */
if (hSh)
pSHGetFolderPathA = (pfnSHGetFolderPathA) GetProcAddress(hSh, "SHGetFolderPathA");
}
_ensureWin32GettersReady();
#endif
filename = FcConfigGetFilename (config, name);
@ -3736,6 +3724,26 @@ FcConfigParseAndLoadFromMemory (FcConfig *config,
return FcConfigParseAndLoadFromMemoryInternal (config, (const FcChar8 *)"memory", buffer, complain, FcTrue);
}
#ifdef _WIN32
static void
_ensureWin32GettersReady()
{
if (!pGetSystemWindowsDirectory)
{
HMODULE hk32 = GetModuleHandleA("kernel32.dll");
if (!(pGetSystemWindowsDirectory = (pfnGetSystemWindowsDirectory)GetProcAddress(hk32, "GetSystemWindowsDirectoryA")))
pGetSystemWindowsDirectory = (pfnGetSystemWindowsDirectory)GetWindowsDirectory;
}
if (!pSHGetFolderPathA)
{
HMODULE hSh = LoadLibraryA("shfolder.dll");
/* the check is done later, because there is no provided fallback */
if (hSh)
pSHGetFolderPathA = (pfnSHGetFolderPathA)GetProcAddress(hSh, "SHGetFolderPathA");
}
}
#endif // _WIN32
#define __fcxml__
#include "fcaliastail.h"
#undef __fcxml__