posix_*
or pthread_*
return value tested to be negative CXX-W2013The pthread_*
or posix_*
functions (except posix_openpt
) always return a positive values.
When the functions mentioned above are used in negative integer range checks, the expression will always be resolved to false
as these functions return either zero (on success) or a positive err-no on failure.
Identifiers with typographically ambiguous names can be hard to read and adds unnecessary responsibility on the developer to be careful with the exact identifiers they are using.
Having identifiers unintentionally shadowed from the outer scope by the inner scope can possibly lead to bugs in code.
Consider using unique identifier names.
std::accumulate
CXX-W2005std::accumulate
folds the elements to the initial value type (provided as the last argument to std::accumulate
). When the initial value type is narrower than that of the elements to sum, there is a loss in precision.
An inheriting class with a user-defined copy constructor must call its base class's copy constructor.
std::string_view
CXX-W2004The std::string_view
handle outlives it's data source instance.
A pointer to the boolean type is used in the if
condition with implicit conversion. The same pointer is never dereferenced.
Call to "erase-remove" combination without end iterator results in incomplete removal of container elements when more than one element is removed.
When a virtual function is declared in a ancestor base class and is overridden by a derived class, calling the base class's virtual method instead of the direct parent's method is considered a bad practice. This is because the ancestor class
Dividing by zero can cause a runtime error, leading to a crash. Because as such dividing by zero is undefined behaviour.
&&
, and ||
) CXX-W1066Having side-effects in a logical operator's right-hand operand can be harmful as they may or may not be executed. This can lead to unintended bugs in code. Consider using if-else statements to make conditional execution clearer.
Assignment in condition should be parenthesized, when used in a conditional operator. Otherwise there can be issues with precedence, as assignment is the operator with least precedence. Consider putting assignment in condition in round braces.
Function declarations should name all parameters explicitly with the correct type. Functions with no parameters should use the void
type instead of leaving the parameter section empty, as mandated by MISRA C:2012 Rule 8.2.
std
or posix
is an undefined behavior CXX-W2017In C++, one can limit a declaration to the namespace to avoid name resolution conflicts. Also, a namespace declaration is not limited to a single file. This opens up the opportunity to modify the namespace introduced by the standard C++ library namely std
and posix
. According to the standard, such a modification is an undefined behavior. Avoid adding declaration or definition to namespace std
or namespace posix
.
Using reserved identifiers can cause conflicts with the compiler or other system libraries, leading to unexpected behavior or compilation errors. To fix this issue, choose a different identifier that is not reserved by the implementation. It is recommended to follow naming conventions and avoid using names that are reserved by the language or the compiler.
shared_ptr
CXX-E2001Finds initializations of C++ shared pointers to non-array type that are initialized with an array.
If a shared pointer std::shared_ptr<T>
is initialized with a new-expression new T[]
, the memory is not deallocated correctly. The pointer uses plain delete
in this case to deallocate the target memory. Instead, a delete[]
call is needed. A std::shared_ptr<T[]>
calls the correct delete operator.
cnd_wait
, cnd_timedwait
, wait
, wait_for
, or wait_until
function calls CXX-E2002Found cnd_wait
, cnd_timedwait
, wait
, wait_for
, or wait_until
function calls when the function is not invoked from a loop that checks whether a condition predicate holds or the function has a condition parameter. To fix this issue, ensure that the function call is wrapped in a loop that checks the condition predicate or has a condition parameter. This will prevent spurious wake-ups and ensure correct behavior.
User-defined copy assignment operators that do not protect against self-assignment can result in undefined behavior and memory leaks. This check detects such copy assignment operators and suggests two common patterns to handle self-assignment:
std::auto_ptr
instead of std::unique_ptr
CXX-W2026std::unique_ptr
is a smart pointer that was introduced in C++11 to replace std::auto_ptr
. It is used to manage dynamically allocated objects that are not shared by multiple objects.
memset
with possibly unintended behaviour CXX-W2048Found potential mistakes in memset()
calls that can lead to unintended behavior. The following cases will be considered potentially unintended usage of the API memset
: