C and C++ Binaries


CONCURRENCY.DU : Double Unlock

Summary

A mutex has been unlocked twice but there is no intervening lock, or is unlocked having never been locked.

For broader checking for double-unlocking phenomena, enable warning class Missing Lock Acquisition.

Properties

Class Name Double Unlock
Significance reliability
Mnemonic CONCURRENCY.DU
Categories
MisraC2023 MisraC2023:D.4.1 Run-time failures shall be minimized
Misra2012 Misra2012:D.4.1 Run-time failures shall be minimized
CWE CWE:765 Multiple Unlocks of a Critical Resource
  CWE:832 Unlock of a Resource that is not Locked
CERT-C CERT-C:POS48-C Do not unlock or destroy another POSIX thread's mutex
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Double Unlock"

Triggering Functions

A warning is triggered if a lock release function is called to acquire a mutex that is not currently locked: either it has never been locked or it has previously been unlocked without an intervening lock.

For the purpose of this check:

Example

#include <pthread.h>

void du_1(pthread_mutex_t lock){
  pthread_mutex_lock(&lock);
  pthread_mutex_unlock(&lock);
  pthread_mutex_unlock(&lock);   /* 'Double Unlock' warning issued here */
}

void du_2(pthread_mutex_t lock){
  pthread_mutex_unlock(&lock);
  pthread_mutex_lock(&lock);
  pthread_mutex_unlock(&lock);   /* 'Double Unlock' warning issued here
                                  *  - the second call to pthread_mutex_lock()
                                  *    may have failed to acquire the lock.
                                  */
}

int no_du(pthread_mutex_t lock){
  if (!pthread_mutex_unlock(&lock)){return -1;}
  if (!pthread_mutex_lock(&lock)){return -1;}
  return pthread_mutex_unlock(&lock);                          /* ok: if control reaches this line,
                                                                * the call to pthread_mutex_lock() must have succeeded
                                                                */
}

Notes

Attempting to unlock an already-unlocked mutex can lead to undefined behavior.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.