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

Summary

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:

Properties

Class Name Encapsulation Violation
Significance reliability
Mnemonic LANG.TYPE.EV
Categories
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
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Encapsulation Violation"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

#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
                                            */
}

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/.