C and C++


CONCURRENCY.LOCK.TWICE : Locked Twice

Summary

An attempt to acquire a lock when the lock is already held. This can lead to deadlock or other undefined behavior.

Properties

Class Name Locked Twice
Significance reliability
Mnemonic CONCURRENCY.LOCK.TWICE
Categories
MisraC2023 MisraC2023:22.18 Non-recursive mutexes shall not be recursively locked
Misra2012 Misra2012:22.18 Non-recursive mutexes shall not be recursively locked
CWE CWE:764 Multiple Locks of a Critical Resource
JPL JPL:9 Place restrictions on the use of semaphores and locks.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Locked Twice"

Triggering Functions

Locked Twice warnings can be triggered by the following functions.

Example

#include <pthread.h>

pthread_mutex_t LockA;
pthread_mutex_t LockB;

void LOCK_TWICE(void){
    pthread_mutex_lock(&LockA);
    pthread_mutex_lock(&LockB);
    pthread_mutex_lock(&LockA); /* 'Locked Twice' warning issued here */
                                /* 'Double Lock' warning also issued if enabled */
    /* ... operate on locked data */
    pthread_mutex_unlock(&LockA);
    pthread_mutex_unlock(&LockB);
}

Relevant Configuration File Parameters

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