winpthreads: Check GetModuleHandle return value before calling GetProcAddress

In Windows Store builds with the winstorecompat library,
GetModuleHandle returns null, and GetProcAddress is not documented
to allow passing null.

Signed-off-by: Martin Storsjö <martin@martin.st>
diff --git a/mingw-w64-libraries/winpthreads/src/clock.c b/mingw-w64-libraries/winpthreads/src/clock.c
index ba983c9..322b7e5 100644
--- a/mingw-w64-libraries/winpthreads/src/clock.c
+++ b/mingw-w64-libraries/winpthreads/src/clock.c
@@ -37,9 +37,11 @@
 static GetSystemTimeAsFileTime_t try_load_GetSystemPreciseTimeAsFileTime()
 {
     /* Use GetSystemTimePreciseAsFileTime() if available (Windows 8 or later) */
-    GetSystemTimeAsFileTime_t get_time = (GetSystemTimeAsFileTime_t)(intptr_t)GetProcAddress(
-        GetModuleHandle ("kernel32.dll"),
-        "GetSystemTimePreciseAsFileTime"); /* <1us precision on Windows 10 */
+    HMODULE mod = GetModuleHandle("kernel32.dll");
+    GetSystemTimeAsFileTime_t get_time = NULL;
+    if (mod)
+        get_time = (GetSystemTimeAsFileTime_t)(intptr_t)GetProcAddress(mod,
+            "GetSystemTimePreciseAsFileTime"); /* <1us precision on Windows 10 */
     if (get_time == NULL)
         get_time = GetSystemTimeAsFileTime; /* >15ms precision on Windows 10 */
     __atomic_store_n(&GetSystemTimeAsFileTime_p, get_time, __ATOMIC_RELAXED);
diff --git a/mingw-w64-libraries/winpthreads/src/misc.c b/mingw-w64-libraries/winpthreads/src/misc.c
index 79c01f2..8003478 100644
--- a/mingw-w64-libraries/winpthreads/src/misc.c
+++ b/mingw-w64-libraries/winpthreads/src/misc.c
@@ -28,7 +28,9 @@
 
 static void __attribute__((constructor)) ctor (void)
 {
-  GetTickCount64FuncPtr = (__typeof__(GetTickCount64FuncPtr)) GetProcAddress(GetModuleHandle("kernel32.dll"), "GetTickCount64");
+  HMODULE mod = GetModuleHandle("kernel32.dll");
+  if (mod)
+    GetTickCount64FuncPtr = (__typeof__(GetTickCount64FuncPtr)) GetProcAddress(mod, "GetTickCount64");
 }
 
 unsigned long long _pthread_time_in_ms(void)
diff --git a/mingw-w64-libraries/winpthreads/src/thread.c b/mingw-w64-libraries/winpthreads/src/thread.c
index 2eb76db..d5c2acf 100644
--- a/mingw-w64-libraries/winpthreads/src/thread.c
+++ b/mingw-w64-libraries/winpthreads/src/thread.c
@@ -82,8 +82,10 @@
 ctor (void)
 {
   HMODULE module = GetModuleHandle("kernel32.dll");
-  AddVectoredExceptionHandlerFuncPtr = (__typeof__(AddVectoredExceptionHandlerFuncPtr)) GetProcAddress(module, "AddVectoredExceptionHandler");
-  RemoveVectoredExceptionHandlerFuncPtr = (__typeof__(RemoveVectoredExceptionHandlerFuncPtr)) GetProcAddress(module, "RemoveVectoredExceptionHandler");
+  if (module) {
+    AddVectoredExceptionHandlerFuncPtr = (__typeof__(AddVectoredExceptionHandlerFuncPtr)) GetProcAddress(module, "AddVectoredExceptionHandler");
+    RemoveVectoredExceptionHandlerFuncPtr = (__typeof__(RemoveVectoredExceptionHandlerFuncPtr)) GetProcAddress(module, "RemoveVectoredExceptionHandler");
+  }
 }
 #endif