enum as a bitmask or flags is only valid if the members are only assigned values in the power of twos.
pthread_*return value tested to be negative CXX-W2013
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.
posixis an undefined behavior CXX-W2017
In 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
posix. According to the standard, such a modification is an undefined behavior. Avoid adding declaration or definition to namespace
std or namespace
std::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.
Found visually ambiguous integer literal constant. The lowercase letter 'l' (ell) can easily be confused with the digit '1' (one)
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.
Finds 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.
wait_untilfunction calls CXX-E2002
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:
An inheriting class with a user-defined copy constructor must call its base class's copy constructor.
std::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.
memsetwith possibly unintended behaviour CXX-W2048
Found potential mistakes in
memset() calls that can lead to unintended behavior. The following cases will be considered potentially unintended usage of the API
reallocwithout aliasing can lead to memory leaks CXX-W2049
Found realloc assignment to the same expression as passed to the first argument.
The return value of
realloc is assigned to the same expression as passed to
the first argument. The problem with this construct is that if
it returns a null pointer but does not deallocate the original memory. If no
other variable is pointing to it, the original memory block is not available
anymore for the program to use or free. In either case,
p = realloc(p, size)
indicates a bad coding style and can be replaced by
q = realloc(p, size).
Suspicious usage of runtime string comparison functions can lead to unintended behavior and bugs in C and C++ code. This check detects calls to string comparison functions, such as
strcmp, where the result is implicitly compared to zero. It is recommended to explicitly compare the result to a valid constant value, such as
> 0, or
== 0, to ensure the desired behavior.
nullptrwith string_view CXX-W2053
std::string_view constructor that takes a
const CharT* argument does not check for null pointers before dereferencing them. This can lead to undefined behavior. To prevent this, the code should be modified to use the default constructor of
std::string_view in most cases where null pointers are passed.
In addition, the comparison operators for
std::string_view cannot be used with a braced initializer list, so the code should be updated to use
.empty() or the empty string literal for appropriate comparisons.
sizeofoperator with stl containers CXX-W2054
The check finds usages of
sizeof on expressions of STL container types. Most likely the user wanted to use
.size() instead. All class/struct types declared in the namespace
std:: having a
const size() method are considered containers, with the exception of
memcmpover objects CXX-W2057
Potentially incorrect calls to
memcmp() can lead to incorrect comparisons of object representations, especially for non-standard-layout types and types without unique object representations. This can be caused by padding or floating-point types. It is recommended to avoid using object representations for comparisons and instead use special member functions and overloaded operators.
Numeric types can be implicitly casted to character types. This allows for the below overload to be used when assigning a numerical value to string.\