C and C++ Binaries


CONCURRENCY.TL : 成功しないトライロック

要旨

既にロックされているミューテックスに対し、トライロックを試みました。

プロパティ

クラス名 Try-lock that will never succeed
日本語クラス名 成功しないトライロック
クラス分類 信頼性 (reliability)
ニーモニック CONCURRENCY.TL
カテゴリー
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
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Try-lock that will never succeed"

#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();
}

ワーニングを引き起こす関数

CodeSonar ships with library models that allow it to a number of try-lock functions, across many different libraries. 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()

注釈

このワーニングはプログラムの冗長性(もしくは誤解の可能性)を示しています。

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。