Function that can spuriously wake up not wrapped in loop
Loop checks wake-up condition after possible spurious wake-up
Description
This defect occurs when the following wait-on-condition functions are called from outside a loop:
C functions:
cnd_wait()
cnd_timedwait()
POSIX functions:
pthread_cond_wait()
pthread_cond_timedwait()
C++
std::condition_variable
andstd::condition_variable_any
class member functions:wait()
wait_until()
wait_for()
Wait-on-condition functions pause the execution of the calling thread when a
specified condition is met. The thread wakes up and resumes once another thread notifies it
with cnd_broadcast()
or an equivalent function. The wake-up notification
can be spurious or malicious.
Risk
If a thread receives a spurious wake-up notification and the condition of the wait-on-condition function is not checked, the thread can wake up prematurely. The wake-up can cause unexpected control flow, indefinite blocking of other threads, or denial of service.
Fix
Wrap wait-on-condition functions that can wake up spuriously in a loop. The loop checks the wake-up condition after a possible spurious wake-up notification.
Examples
Result Information
Group: Concurrency |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
SPURIOUS_WAKEUP_NOT_WRAPPED_IN_LOOP |
Impact: Low |
Version History
Introduced in R2018b
See Also
Function that can
spuriously fail not wrapped in loop
| Returned value of a sensitive function not
checked
| Find defects (-checkers)
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)