JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++
Binaries


CONCURRENCY.TL : Try-lock that will never succeed

Summary

A try-lock on a mutex that has already been locked with no intervening unlock.

Properties

Class Name Try-lock that will never succeed
Significance reliability
Mnemonic CONCURRENCY.TL
Categories
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CWE CWE:413 Improper Resource Locking
CERT-CPP CERT-CPP:CON56-CPP Do not speculatively lock a non-recursive mutex that is already owned by the calling thread
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="Try-lock that will never succeed"

Example

#include <pthread.h>
#include <stdlib.h>

void concurrency_tl_bad(void){
    pthread_mutex_t mut;
    if( pthread_mutex_init( &mut, NULL ) !=  0 ) abort();
    if( pthread_mutex_lock( &mut ) != 0 ) abort();
    if( pthread_mutex_trylock( &mut ) != 0 ) abort(); /* 'Try-lock that will never succeed' warning issued here
                                                       * - &mut is already locked by the previous call
                                                       * - execution will never reach the following line
                                                       */
    if( pthread_mutex_unlock( &mut ) != 0 ) abort();
    if( pthread_mutex_destroy( &mut ) != 0 ) abort();
}

void concurrency_tl_ok(void){
    pthread_mutex_t mut;
    if( pthread_mutex_init( &mut, NULL ) !=  0 ) abort();
    if( pthread_mutex_trylock( &mut ) != 0 ) abort();                   /* ok: &mut is not already locked */
    if( pthread_mutex_unlock( &mut ) != 0 ) abort();
    if( pthread_mutex_destroy( &mut ) != 0 ) abort();
}

Triggering Functions

CodeSonar ships with library models that allow it to recognize a number of try-lock functions, across many different libraries. Some examples are shown in the table below. If one of these functions is called when the mutex is always already locked, a warning will be issued.

If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering Try-lock that will never succeed warnings.

Functions that can trigger warnings include...
Apache Portable Runtime (APR) apr_global_mutex_trylock()
libc pthread_mutex_trylock()
Linux Kernel down_trylock()
Mac OS X hw_lock_try()
OpenMP omp_test_lock()
Qt QMutex::tryLock()
VxWorks semBCreate()
Win32 TryEnterCriticalSection()
wxWidgets wxMutex::TryLock()

Notes

This warning indicates a redundant (and potentially misleading) program statement.

Relevant Configuration File Parameters

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

 

To report problems with this documentation, please visit https://support.codesecure.com/.