On Fri, 26 Jan 2024, Antonin Décimo wrote:

diff --git a/mingw-w64-libraries/winpthreads/src/misc.c 
b/mingw-w64-libraries/winpthreads/src/misc.c
index 83caf262f..62acb9f50 100644
--- a/mingw-w64-libraries/winpthreads/src/misc.c
+++ b/mingw-w64-libraries/winpthreads/src/misc.c
@@ -24,15 +24,46 @@
#include "pthread.h"
#include "misc.h"

-static ULONGLONG (*GetTickCount64FuncPtr) (VOID);
+void (WINAPI *_pthread_get_system_time_best_as_file_time) (LPFILETIME) = NULL;
+static ULONGLONG (WINAPI *_pthread_get_tick_count_64) (VOID);

-static void __attribute__((constructor)) ctor (void)
+#if defined(__GNUC__) || defined(__clang__)
+__attribute__((constructor))
+#endif
+static void winpthreads_init(void)
{
-  HMODULE mod = GetModuleHandle("kernel32.dll");
-  if (mod)
-    GetTickCount64FuncPtr = (__typeof__(GetTickCount64FuncPtr)) GetProcAddress(mod, 
"GetTickCount64");
+    HMODULE mod = GetModuleHandle("kernel32.dll");
+    if (!mod) return;
+
+    _pthread_get_tick_count_64 =
+        (ULONGLONG (WINAPI *)(VOID))(void*) GetProcAddress(mod, 
"GetTickCount64");
+
+    /* <1us precision on Windows 10 */
+    _pthread_get_system_time_best_as_file_time =
+        (void (WINAPI *)(LPFILETIME))(void*) GetProcAddress(mod, 
"GetSystemTimePreciseAsFileTime");
+
+    if (!_pthread_get_system_time_best_as_file_time)
+        /* >15ms precision on Windows 10 */
+        _pthread_get_system_time_best_as_file_time = GetSystemTimeAsFileTime;
}

If GetModuleHandle returns NULL here, we return and don't initialize _pthread_get_system_time_best_as_file_time at all - which then would crash later.

Instead we need to wrap the two calls to GetProcAddress within an if (mod) {}, and do the final fallback initialization of _pthread_get_system_time_best_as_file_time after that.

Thus, I'd suggest this modification on top of this patch:

diff --git a/mingw-w64-libraries/winpthreads/src/misc.c b/mingw-w64-libraries/winpthreads/src/misc.c
index 62acb9f50..763d477ec 100644
--- a/mingw-w64-libraries/winpthreads/src/misc.c
+++ b/mingw-w64-libraries/winpthreads/src/misc.c
@@ -33,14 +33,15 @@ __attribute__((constructor))
 static void winpthreads_init(void)
 {
     HMODULE mod = GetModuleHandle("kernel32.dll");
-    if (!mod) return;
-
-    _pthread_get_tick_count_64 =
-        (ULONGLONG (WINAPI *)(VOID))(void*) GetProcAddress(mod, 
"GetTickCount64");
-
-    /* <1us precision on Windows 10 */
-    _pthread_get_system_time_best_as_file_time =
-        (void (WINAPI *)(LPFILETIME))(void*) GetProcAddress(mod, 
"GetSystemTimePreciseAsFileTime");
+    if (mod)
+    {
+        _pthread_get_tick_count_64 =
+            (ULONGLONG (WINAPI *)(VOID))(void*) GetProcAddress(mod, 
"GetTickCount64");
+
+        /* <1us precision on Windows 10 */
+        _pthread_get_system_time_best_as_file_time =
+            (void (WINAPI *)(LPFILETIME))(void*) GetProcAddress(mod, 
"GetSystemTimePreciseAsFileTime");
+    }

     if (!_pthread_get_system_time_best_as_file_time)
         /* >15ms precision on Windows 10 */


// Martin

_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to