...
Blocking calls include, but are not limited to: network, file, and console I/O. This rule is a specific instance of CON36CON05-C. Do not perform operations that can block while holding a lock using POSIX threads.
Noncompliant Code Example
...
This compliant solution performs the recv() call with the parameter oMSG_nonblockDONTWAIT, which causes the call to fail if no messages are available on the socket:
| Code Block | ||||
|---|---|---|---|---|
| ||||
void thread_foo(void *ptr) {
uint32_t num;
int result;
/* sock is a connected TCP socket */
if ((result = recv(sock, (void *)&num, sizeof(uint32_t), OMSG_NONBLOCKDONTWAIT)) < 0) {
/* Handle Error */
}
if ((result = pthread_mutex_lock(&mutex)) != 0) {
/* Handle Error */
}
/* ... */
if ((result = pthread_mutex_unlock(&mutex)) != 0) {
/* Handle Error */
}
}
|
...
Blocking or lengthy operations performed within synchronized regions could result in a deadlocked or an unresponsive system.
Rule | Severity | Likelihood | Detectable |
|---|
Repairable | Priority | Level | |
|---|---|---|---|
POS52-C | Low | Probable | No |
No | P2 | L3 |
Automated Detection
| Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| CodeSonar |
| CONCURRENCY.STARVE.BLOCKING | Blocking in Critical Section | ||||||
| Helix QAC |
| DF4966, DF4967 | |||||||
| Klocwork |
| CONC.SLEEP | |||||||
| Parasoft C/C++test |
Related Guidelines
| CERT_C-POS52-a | Do not use blocking functions while holding a lock | |||||||
| Polyspace Bug Finder |
| CERT C: Rule POS52-C | Checks for blocking operation while holding lock (rule fully covered) | ||||||
| Security Reviewer - Static Reviewer |
| RTOS_20 | Fully implemented |
Related Guidelines
Key here (explains table format and definitions)
Taxonomy | Taxonomy item | Relationship |
|---|---|---|
| CERT C |
| LCK09-J. Do not perform operations that can block while holding a lock | Prior to 2018-01-12: CERT: Unspecified Relationship | |
| CWE 2.11 | CWE-557 | 2017-07-10: CERT: Rule subset of CWE |
CERT-CWE Mapping Notes
Key here for mapping notes
CWE-557 and POS52-C
CWE-557 = Union( POS52-C, list) where list =
- Concurrency issues besides blocking while holding a POSIX lock
Bibliography
...
...