From 3f60f0b4e3d9fd45f67b1dbf868677355687e578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=93=88=F0=9D=92=BD=F0=9D=93=8E=F0=9D=93=83?= =?UTF-8?q?=F0=9D=93=8A=F0=9D=93=87?= Date: Thu, 2 Jul 2026 14:19:52 +0800 Subject: [PATCH] fix UB MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only safe asynchronous functions are allowed to be called in signal handlers Signed-off-by: 𝓈𝒽𝓎𝓃𝓊𝓇 --- .../src/CalculatorServer.cpp | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/code/Examples/C++/RpcClientServerBasic/src/CalculatorServer.cpp b/code/Examples/C++/RpcClientServerBasic/src/CalculatorServer.cpp index 49c7da38b..f4fabab40 100644 --- a/code/Examples/C++/RpcClientServerBasic/src/CalculatorServer.cpp +++ b/code/Examples/C++/RpcClientServerBasic/src/CalculatorServer.cpp @@ -12,8 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include +#include #include -#include #include #include #include @@ -143,12 +144,13 @@ class Server //!-- }; -std::function stop_handler; +volatile std::sig_atomic_t stop_requested = 0; void signal_handler( int signum) { - stop_handler(signum); + (void)signum; + stop_requested = 1; } //!--MAIN @@ -163,12 +165,6 @@ int main( std::thread thread(&Server::run, server); - stop_handler = [&](int signum) - { - std::cout << "Signal received, stopping execution." << std::endl; - server->stop(); - }; - signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); #ifndef _WIN32 @@ -178,8 +174,12 @@ int main( std::cout << "Server running. Please press Ctrl+C to stop the server at any time." << std::endl; + while (!stop_requested) + { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + std::cout << "Signal received, stopping execution." << std::endl; + server->stop(); thread.join(); - - return 0; } //!--