1936
Common Consequences
Scope Impact Likelihood
Integrity
Confidentiality
Other
Modify Application Data
Read Application Data
Alter Execution Logic
Potential Mitigations
Phase: Implementation
Use industry standard APIs to synchronize your code.
Demonstrative Examples
Example 1:
The following function attempts to acquire a lock in order to perform operations on a shared
resource.
Example Language: C (Bad)
void f(pthread_mutex_t *mutex) {
pthread_mutex_lock(mutex);
/* access shared resource */
pthread_mutex_unlock(mutex);
}
However, the code does not check the value returned by pthread_mutex_lock() for errors. If
pthread_mutex_lock() cannot acquire the mutex for any reason, the function may introduce a race
condition into the program and result in undefined behavior.
In order to avoid data races, correctly written programs must check the result of thread
synchronization functions and appropriately handle all errors, either by attempting to recover from
them or reporting them to higher levels.
Example Language: C (Good)
int f(pthread_mutex_t *mutex) {
int result;
result = pthread_mutex_lock(mutex);
if (0 != result)
return result;
/* access shared resource */
return pthread_mutex_unlock(mutex);
}
CWE Version 4.14
CWE-662: Improper Synchronization
CWE-662: Improper Synchronization