This commit revises `pthread_cleanup_push` and `pthread_cleanup_pop` macros to
use a `do { ... } while(0)` wrapper, preventing syntax errors when used in
certain contexts. The original code could fail if used within a conditional
statement without braces, causing unintended behavior or compilation issues.
Example of error:

  if (condition)
      pthread_cleanup_push(cleanup_fn, arg);
  pthread_cleanup_pop(1);

This would fail due to unmatched braces in the macro expansion. The new
structure ensures the macro expands correctly in all cases.

Signed-off-by: Shaobo Song <shnusongsha...@gmail.com>
---
 winsup/cygwin/include/pthread.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/include/pthread.h b/winsup/cygwin/include/pthread.h
index 66d367d62..cf2fcb04b 100644
--- a/winsup/cygwin/include/pthread.h
+++ b/winsup/cygwin/include/pthread.h
@@ -110,10 +110,13 @@ typedef struct _pthread_cleanup_handler
 void _pthread_cleanup_push (__pthread_cleanup_handler *handler);
 void _pthread_cleanup_pop (int execute);
 
-#define pthread_cleanup_push(_fn, _arg) { __pthread_cleanup_handler 
__cleanup_handler = \
-                                        { _fn, _arg, NULL }; \
-                                        _pthread_cleanup_push( 
&__cleanup_handler );
-#define pthread_cleanup_pop(_execute) _pthread_cleanup_pop( _execute ); }
+#define pthread_cleanup_push(_fn, _arg) \
+  do { \
+    __pthread_cleanup_handler __cleanup_handler = { _fn, _arg, NULL }; \
+    _pthread_cleanup_push(&__cleanup_handler)
+#define pthread_cleanup_pop(_execute) \
+    _pthread_cleanup_pop(_execute); \
+  } while (0)
 
 /* Condition variables */
 int pthread_cond_broadcast (pthread_cond_t *);
-- 
2.25.1

Reply via email to