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++


LANG.TYPE.EV : Encapsulation Violation

要旨

A type should be treated as opaque, but an object of that type is being used in a way that violates encapsulation conventions.

For this warning class, we say a type T is:

プロパティ

クラス名 Encapsulation Violation
日本語クラス名 Encapsulation Violation
クラス分類 信頼性 (reliability)
ニーモニック LANG.TYPE.EV
カテゴリー
MisraC2025 MisraC2025:22.12 Thread objects, thread synchronization objects, and thread-specific storage pointers shall only be accessed by the appropriate Standard Library functions
MisraC2023 MisraC2023:22.12 Thread objects, thread synchronization objects, and thread-specific storage pointers shall only be accessed by the appropriate Standard Library functions
Misra2012 Misra2012:22.12 Thread objects, thread synchronization objects, and thread-specific storage pointers shall only be accessed by the appropriate Standard Library functions
CWE CWE:662 Improper Synchronization
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Encapsulation Violation"
注:非正規化された AST を継続して使用した場合、使用ディスク容量が増加し解析時間が長くなる可能性があります。

#include <threads.h>
#include <string.h>

extern mtx_t lock1;
extern mtx_t lock2;
extern thrd_t thread1;
extern thrd_t thread2;

/* CodeSonar factory settings include the following configuration rules.
 * OPAQUE_TYPE_NONCOPYABLE_REGEXES += ^(cnd_t|mtx_t)$
 * OPAQUE_TYPE_COPYABLE_REGEXES += ^(thrd_t|tss_t)$
 *
 * - Values of type mtx_t are treated as opaque and uncopyable.
 * - Values of type thread_t are treated as opaque but copyable.
 */

int same_thread(void){
    return thread1 == thread2;             /* 'Encapsulation Violation' warning issued here
                                            * - thread_t treated as opaque, so cannot be compared with ==
                                            * - resolve by using thrd_equal() instead
                                            */
}

void copy_opaques(void){
    lock1 = lock2;                         /* 'Encapsulation Violation' warning issued here
                                            *  - mtx_t is not copyable */
    thread1 = thread2;                                 /* ok: thrd_t is copyable */
}


extern void my_voidptr_fn(void *v);                    /* a user function without a CodeSonar library model */

void * my_thread_fn(mtx_t m,               /* 'Encapsulation Violation' warning issued here
                                            * - the actual parameter is implicitly copied at call time, but mtx_t is not copyable.
                                            * There are two options to resolve this.
                                            * - Define a CodeSonar library model for my_thread_fn.
                                            * - Adjust my_thread_fn so that the first parameter is mtx_t* rather than mtx_t.
                                            */
                 mtx_t *mptr,                          /* ok: mtx_t* rather than mtx_t */
                 thrd_t t                              /* ok: thrd_t is copyable */
                 ){
    if (!mptr) {return NULL;}
    my_voidptr_fn(mptr);                              /* ok: my_voidptr_fn() expects void* argument rather than mtx_t*,
                                                       * but is not a library function
                                                       */
    mtx_lock(mptr);                                   /* ok: mtx_lock() is a library function and expects a mtx_t* */
    mtx_unlock(mptr);                                 /* ok: mtx_unlock() is a library function and expects a mtx_t* */

    return memset(mptr, 0, sizeof(mtx_t)); /* 'Encapsulation Violation' warning issued here
                                            * - memset() expects void* first argument rather than mtx_t*, and is a library function
                                            */
}

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

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

 

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