From 3de2f13b89ce1f6671488c4b6c0b23f4b825531b Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 24 Apr 2021 17:15:47 +0100 Subject: [PATCH] Factor out Windows utility function get_system_dir(). The code to find out the location of the c:\windows\system32 directory was already present, in load_system32_dll(). Now it's moved out into a function of its own, so it can be called in other contexts. --- windows/CMakeLists.txt | 1 + windows/platform.h | 1 + windows/utils/get_system_dir.c | 21 +++++++++++++++++++++ windows/utils/load_system32_dll.c | 10 +--------- 4 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 windows/utils/get_system_dir.c diff --git a/windows/CMakeLists.txt b/windows/CMakeLists.txt index 288827cb..fb2b3028 100644 --- a/windows/CMakeLists.txt +++ b/windows/CMakeLists.txt @@ -10,6 +10,7 @@ add_sources_from_current_dir(utils utils/filename.c utils/fontspec.c utils/getdlgitemtext_alloc.c + utils/get_system_dir.c utils/get_username.c utils/is_console_handle.c utils/load_system32_dll.c diff --git a/windows/platform.h b/windows/platform.h index f84f4236..5c307cb0 100644 --- a/windows/platform.h +++ b/windows/platform.h @@ -566,6 +566,7 @@ HWND event_log_window(void); extern DWORD osMajorVersion, osMinorVersion, osPlatformId; void init_winver(void); void dll_hijacking_protection(void); +const char *get_system_dir(void); HMODULE load_system32_dll(const char *libname); const char *win_strerror(int error); void restrict_process_acl(void); diff --git a/windows/utils/get_system_dir.c b/windows/utils/get_system_dir.c new file mode 100644 index 00000000..049cd7fc --- /dev/null +++ b/windows/utils/get_system_dir.c @@ -0,0 +1,21 @@ +/* + * Wrapper function around GetSystemDirectory that deals with + * allocating the output buffer, and also caches the result for future + * calls. + */ + +#include "putty.h" + +const char *get_system_dir(void) +{ + static char *sysdir = NULL; + static size_t sysdirsize = 0; + + if (!sysdir) { + size_t len; + while ((len = GetSystemDirectory(sysdir, sysdirsize)) >= sysdirsize) + sgrowarray(sysdir, sysdirsize, len); + } + + return sysdir; +} diff --git a/windows/utils/load_system32_dll.c b/windows/utils/load_system32_dll.c index e00d7c34..d227a264 100644 --- a/windows/utils/load_system32_dll.c +++ b/windows/utils/load_system32_dll.c @@ -8,18 +8,10 @@ HMODULE load_system32_dll(const char *libname) { - static char *sysdir = NULL; - static size_t sysdirsize = 0; char *fullpath; HMODULE ret; - if (!sysdir) { - size_t len; - while ((len = GetSystemDirectory(sysdir, sysdirsize)) >= sysdirsize) - sgrowarray(sysdir, sysdirsize, len); - } - - fullpath = dupcat(sysdir, "\\", libname); + fullpath = dupcat(get_system_dir(), "\\", libname); ret = LoadLibrary(fullpath); sfree(fullpath); return ret;