From f9820577c1908ce7442ef4cae2b963f9a5ba78e5 Mon Sep 17 00:00:00 2001 From: Martin Konrad Date: Wed, 4 Mar 2020 10:56:25 -0500 Subject: [PATCH] Replace usleep call by nanosleep Also improve behavior in case signals are delivered to the sleeping thread. This fixes a potential security weakness reported by codacy (interaction of usleep with SIGALRM and other timer functions such as sleep(), alarm(), setitimer(), and nanosleep() is unspecified). --- src/libCom/osi/os/posix/epicsAtomicOSD.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libCom/osi/os/posix/epicsAtomicOSD.cpp b/src/libCom/osi/os/posix/epicsAtomicOSD.cpp index 1cc227fcd..a449a69c0 100644 --- a/src/libCom/osi/os/posix/epicsAtomicOSD.cpp +++ b/src/libCom/osi/os/posix/epicsAtomicOSD.cpp @@ -48,8 +48,10 @@ void epicsAtomicLock ( EpicsAtomicLockKey * ) status = pthread_mutex_lock ( & mutex ); if ( status == 0 ) return; assert ( status == EINTR ); - static const useconds_t retryDelayUSec = 100000; - usleep ( retryDelayUSec ); + struct timespec retryDelay = { 0, 100000000 }; + struct timespec remainingDelay; + while (nanosleep(&retryDelay, &remainingDelay) == -1 && errno == EINTR) + retryDelay = remainingDelay; countDown--; assert ( countDown ); }