CONC.NO_UNLOCKMissing unlockThe CONC.NO_UNLOCK checker reports potential deadlocks due to locked threads that don't have corresponding unlocks. Vulnerability and riskA missing release for a lock can result in a deadlock. If a lock is held and not released, no further calls to acquire the lock can proceed until the lock is released. Mitigation and preventionTo help avoid lock contention:
Vulnerable code example1 #include <pthread.h> 2 3 extern int z(); 4 5 void foo(pthread_mutex_t *mutex) { 6 pthread_mutex_lock(mutex); 7 switch (z()) { 8 case 0: 9 return; 10 case 1: 11 break; 12 } 13 pthread_mutex_unlock(mutex); 14 } Klocwork reports that the mutex is locked at line 6 and not unlocked at line 9 if case 0 is in effect. Fixed code example1 #include <pthread.h> 2 3 extern int z(); 4 5 void foo(pthread_mutex_t *mutex) { 6 pthread_mutex_lock(mutex); 7 switch (z()) { 8 case 0: 9 pthread_mutex_unlock(mutex); 10 return; 11 case 1: 12 break; 13 } 14 pthread_mutex_unlock(mutex); 15 } In the fixed version, the unlock is included at line 9. Related checkersExternal guidanceExtensionThis checker can be extended. The related knowledge base record kinds are: See Tuning C/C++ analysis for more information. |