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.0p0 Hot Tips | CONFIDENTIAL | CodeSecure Inc |
assert() statements can cause problems for the CodeSonar analysis if the particular implementation of assert() is such that that CodeSonar cannot determine that execution ends when the statement condition fails.
This section outlines the various issues you may encounter when using assert(), and describes ways to handle these issues.
In some cases users may get unexpected results when analyzing code that invokes the assert() macro. These unexpected results fall into two main categories: unexpected reachability after an assertion and unexpected unreachability after an assertion.
| Example |
Unexpected Null Pointer
Dereference warning in line 3 of the following.
/* 1 */ int *p = malloc(4); /* 2 */ assert(p); /* 3 */ *p = 42; |
|---|---|
| Issue |
There are two major causes of this phenomenon: usually either
assert() is being preprocessed away or
the assert() definition calls an undefined
abort function.
|
| Resolution |
If you do not want assert()
invocations to be preprocessed away, make sure NDEBUG is not defined in the build system.
If the assert() definition calls an undefined abort function, either
|
| Example |
Unexpected Unreachable Data
Flow warning in line 2 of the following.
/* 1 */ assert(p); /* 2 */ i = 42; |
|---|---|
| Issue | This typically occurs when the <assert.h> header inclusion has been unintentionally omitted from the source file and so assert() is instead resolved to the lesser-known libc assert() function (as opposed to the macro), which has different behavior: in particular, it always aborts execution. Not all libc implementations contain this function, but some do, and CodeSonar is not aware of which libc version is being used. |
| Resolution | #include the <assert.h> header as necessary. |
In some cases, you may prefer to avoid use of assert() entirely. If you still wish to make use of assertion functionality and have CodeSonar understand that functionality correctly, there are two main options:
CodeSonar provides the csonar_assert() macro for use in cases where assert() is not suitable.
If your code includes invocations of csonar_assert() make sure you do all of the following.
EDG_FRONTEND_OPTIONS_APPEND += -Icsonar_libmodels_path
For more information, see Implementing and Including Custom Checks with the Extension API.
You can define your own assertion macro, with one definition to be used by the CodeSonar build/analysis and another definition to be used otherwise. For example, if you choose to name the macro my_csonar_assert():
#ifdef __CODESONAR__ /* terminate execution when the assertion does not hold */ #define my_csonar_assert(c) do{ if( !(c) ) for(;;){} }while(0) #else /* no-op */ #define my_csonar_assert(c) ((void)0) #endif
Manipulating the Input to the Build and Analysis describes and provides examples of several methods suitable for incorporating this code into your project: