在 2026-9-10 21:10, Kirill Makurin 写道:
+static int WinpthreadsNormalMutexInit (WinpthreadsMutex **wMutex, const 
pthread_mutexattr_t *attr) {
+  /**
+   * Using `RTL_SIZEOF_THROUGH_FIELD` for allocation size may result in
+   * `-Walloc-size` diagnositcs.
+   */
+#if defined(__GNUC__) || defined(__clang__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Walloc-size"
+#endif
+
+  *wMutex = malloc (RTL_SIZEOF_THROUGH_FIELD (WinpthreadsMutex, Mutex.Normal));
+
+#if defined(__GNUC__) || defined(__clang__)
+#pragma GCC diagnostic pop
+#endif
+
+  /**
+   * The pthread_mutex_init() function shall fail if:
+   *
+   * [ENOMEM]
+   *   Insufficient memory exists to initialize the mutex.
+   */
+  if (*wMutex == NULL) {
+    return ENOMEM;
+  }
+
+  /**
+   * Direct access through `(*wMutex)->Mutex` may result in `-Warray-bounds`
+   * diagnostics.
+   */
+  WinpthreadsNormalMutex *mutex = &(*wMutex)->Mutex.Normal;

While there are certainly no issues in this code, the pragmas don't look quite neat. Using the COM-flavor approach which I suggested earlier, it could be

   struct WinpthreadsMutex
   {
     const WinpthreadsMutexVtable *vtable;
   };

   struct WinpthreadsNormalMutex
   {
     const WinpthreadsMutexVtable *vtable;
     HANDLE Event;
     LONG LockState;
   };

   static int WinpthreadsNormalMutexInit (WinpthreadsMutex **wMutex, const 
pthread_mutexattr_t *attr) {
     WinpthreadsNormalMutex *mutex = malloc (sizeof (WinpthreadsNormalMutex));
     *wMutex = (WinpthreadsMutex *) mutex;
     if (!mutex) {
       return ENOMEM;
     }

     // ...
   }


+
+  mutex->Event = CreateEventW (NULL, FALSE, FALSE, NULL);
+
+  /**
+   * The pthread_mutex_init() function shall fail if:
+   *
+   * [EAGAIN]
+   *   The system lacked the necessary resources (other than memory) to
+   *   initialize another mutex.
+   */
+  if (mutex->Event == NULL) {
+    free (wMutex);
+    return EAGAIN;
+  }


This should be `free (*wMutex)`, and likewise somewhere else:


diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c 
b/mingw-w64-libraries/winpthreads/src/mutex.c
index be950ee2b..0a58a753d 100644
--- a/mingw-w64-libraries/winpthreads/src/mutex.c
+++ b/mingw-w64-libraries/winpthreads/src/mutex.c
@@ -380,7 +380,7 @@ static int WinpthreadsNormalMutexInit (WinpthreadsMutex 
**wMutex, const pthread_
    *   initialize another mutex.
    */
   if (mutex->Event == NULL) {
-    free (wMutex);
+    free (*wMutex);
     return EAGAIN;
   }

@@ -503,7 +503,7 @@ static int WinpthreadsErrorCheckMutexInit (WinpthreadsMutex 
**wMutex, const pthr
    *   initialize another mutex.
    */
   if (mutex->Event == NULL) {
-    free (wMutex);
+    free (*wMutex);
     return EAGAIN;
   }

@@ -650,7 +650,7 @@ static int WinpthreadsRecursiveMutexInit (WinpthreadsMutex 
**wMutex, const pthre
    *   initialize another mutex.
    */
   if (mutex->Event == NULL) {
-    free (wMutex);
+    free (*wMutex);
     return EAGAIN;
   }


--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to