Rule Definition
Calling this type of function with parameters corresponding to infinite timeout can lead to program blocking and/or deadlock.
Even if the logical behavior would be to wait infinitely until some event occurs, it can be good practice to add a timeout to detect a bad situation (such as a probable deadlock).
Remediation
Correct this violation by adding logic that deals with a situation when the mutex is never released.
Violation Code Sample
void f(HANDLE mutex)
{
WaitForSingleObject(mutex, INFINITE);
mySharedData = 10;
ReleaseMutex(mutex);
}
Fixed Code Sample
void f(HANDLE mutex)
{
int const TIMEOUT = 10000; // Value depends on your application
DWORD result = WaitForSingleObject(mutex, TIMEOUT);
if (result == WAIT_TIMEOUT)
{
throw std::runtime_error("Cannot access mySharedData. Deadlock?");
}
mySharedData = 10;
ReleaseMutex(mutex);
}
Related Technologies
C++
Technical Criterion
CWE-1088 - Synchronous Access of Remote Resource without Timeout
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.