This is an automated email from Gerrit.

"Anatoly P <anatoly.parshint...@syntacore.com>" just uploaded a new patch set 
to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/8927

-- gerrit

commit 4f24554c2908319df0f968ffe8479e84e17e3720
Author: Parshintsev Anatoly <anatoly.parshint...@syntacore.com>
Date:   Tue May 20 10:10:43 2025 +0300

    server: global state updated by signal handlers should have volatile atomic 
lock-free type
    
    Signal handlers currently violate both C language and POSIX requirements:
    1. To avoid undefined behavior (UB), variables accessed or modified by
       signal handlers be of atomic lock-free type.
    2. The respected variables should be marked as volatile.
    3. Signal handlers may only call a very limited subset of standard
       library functions.
    4. Additionally, POSIX restricts signal handlers to signal-safe
       functions.
    
    This patch addresses the first two issues on platformats that support
    lock-free atomic access to int.
    For platforms that do not support lock-free atomic access to int we just
    mark the respected variables as volatile and hope for the best.
    
    Items 3 and 4 must be handled separately but are outside the scope of
    this change.
    
    Change-Id: I9c344e87bab9eefe7d99b0aad300a3ef4712df51
    Signed-off-by: Parshintsev Anatoly <anatoly.parshint...@syntacore.com>

diff --git a/src/server/server.c b/src/server/server.c
index 0649ec942b..0be1d303e2 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -25,6 +25,7 @@
 #include "telnet_server.h"
 #include "ipdbg.h"
 
+#include <stdatomic.h>
 #include <signal.h>
 
 #ifdef HAVE_NETDB_H
@@ -43,10 +44,18 @@ enum shutdown_reason {
        SHUTDOWN_WITH_ERROR_CODE,       /* set by shutdown command; quit with 
non-zero return code */
        SHUTDOWN_WITH_SIGNAL_CODE       /* set by sig_handler; exec shutdown 
then exit with signal as return code */
 };
-static enum shutdown_reason shutdown_openocd = CONTINUE_MAIN_LOOP;
 
+#if (ATOMIC_INT_LOCK_FREE == 2)
+typedef volatile atomic_int signal_updated_int_t;
+#else
+// For platforms that do not have lock-free atomic int type we just fall-back
+// to good old `volatile int` and hope for the best
+typedef volatile int signal_updated_int_t;
+#endif
+
+static signal_updated_int_t shutdown_openocd = CONTINUE_MAIN_LOOP;
 /* store received signal to exit application by killing ourselves */
-static int last_signal;
+static signal_updated_int_t last_signal;
 
 /* set the polling period to 100ms */
 static int polling_period = 100;

-- 

Reply via email to