Christoph Reiter reported on the Git for Windows issue tracker[1], that
mingw_strftime() imports strftime() from ucrtbase.dll with the wrong
calling convention. It should be __cdecl instead of WINAPI, which we
always use in DECLARE_PROC_ADDR().
The MSYS2 project encountered cmake sefaults on x86 Windows caused by
the same issue in the cmake source. [2] There are no known git crashes
that where caused by this, yet, but we should try to prevent them.
We import two other non-WINAPI functions via DECLARE_PROC_ADDR(), too.
* NtSetSystemInformation() (NTAPI)
* GetUserNameExW() (SEC_ENTRY)
NTAPI, SEC_ENTRY and WINAPI are all ususally defined as __stdcall,
but there are circumstances where they're defined differently.
Teach DECLARE_PROC_ADDR() about calling conventions and be explicit
about when we want to use which calling convention.
Import winnt.h for the definition of NTAPI and sspi.h for SEC_ENTRY
near their respective only users.
[1] https://github.com/git-for-windows/git/issues/3560
[2] https://github.com/msys2/MINGW-packages/issues/10152
Reported-By: Christoph Reiter <reiter.christoph@gmail.com>
Signed-off-by: Matthias Aßhauer <mha1993@live.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Matthias Aßhauer committedJan 8, 2022 at 16:02 UTC4a9b204920152c668228a9d43a63be39b0c32f45
5 files changed+15-10
compat/mingw.c
+4-2
index 9e0cd1e097..f38c438112 100644--- a/compat/mingw.c+++ b/compat/mingw.c@@ -8,6 +8,8 @@ #include "win32/lazyload.h" #include "../config.h" #include "dir.h"+#define SECURITY_WIN32+#include <sspi.h> #define HCAST(type, handle) ((type)(intptr_t)handle)@@ -1008,7 +1010,7 @@ size_t mingw_strftime(char *s, size_t max, /* a pointer to the original strftime in case we can't find the UCRT version */ static size_t (*fallback)(char *, size_t, const char *, const struct tm *) = strftime; size_t ret;- DECLARE_PROC_ADDR(ucrtbase.dll, size_t, strftime, char *, size_t,+ DECLARE_PROC_ADDR(ucrtbase.dll, size_t, __cdecl, strftime, char *, size_t, const char *, const struct tm *); if (INIT_PROC_ADDR(strftime))@@ -2183,7 +2185,7 @@ enum EXTENDED_NAME_FORMAT { static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type) {- DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,+ DECLARE_PROC_ADDR(secur32.dll, BOOL, SEC_ENTRY, GetUserNameExW, enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG); static wchar_t wbuffer[1024]; DWORD len;
compat/win32/lazyload.h
+3-3
index 2b3637135f..f2bb96c89c 100644--- a/compat/win32/lazyload.h+++ b/compat/win32/lazyload.h@@ -4,7 +4,7 @@ /* * A pair of macros to simplify loading of DLL functions. Example: *- * DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateHardLinkW,+ * DECLARE_PROC_ADDR(kernel32.dll, BOOL, WINAPI, CreateHardLinkW, * LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES); * * if (!INIT_PROC_ADDR(CreateHardLinkW))@@ -25,10 +25,10 @@ struct proc_addr { }; /* Declares a function to be loaded dynamically from a DLL. */-#define DECLARE_PROC_ADDR(dll, rettype, function, ...) \+#define DECLARE_PROC_ADDR(dll, rettype, convention, function, ...) \ static struct proc_addr proc_addr_##function = \ { #dll, #function, NULL, 0 }; \- typedef rettype (WINAPI *proc_type_##function)(__VA_ARGS__); \+ typedef rettype (convention *proc_type_##function)(__VA_ARGS__); \ static proc_type_##function function /*