C & C++

C & C++

Made by DeepSource
Misuse of enum as an improper bitmask CXX-W2056
Bug risk
Major

Using enum as a bitmask or flags is only valid if the members are only assigned values in the power of twos.

Found posix_* or pthread_* return value tested to be negative CXX-W2013
Bug risk
Major

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

Identifier names are typographically ambiguous CXX-W1022
Bug risk
Major

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.

Found shadowing of identifiers from outer scope CXX-W1023
Bug risk
Major

Having identifiers unintentionally shadowed from the outer scope by the inner scope can possibly lead to bugs in code.

Consider using unique identifier names.

Modify namespace std or posix is an undefined behavior CXX-W2017
Bug risk
Major

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

Possible loss of precision due to iterator element type-casting in std::accumulate CXX-W2005
Bug risk
Major

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 ambigious integer literal constant CXX-C2018
Bug risk
Major

Found visually ambiguous integer literal constant. The lowercase letter 'l' (ell) can easily be confused with the digit '1' (one)

Found use of possibly reserved identifier CXX-E2000
Bug risk
Major

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.

Possibly unintended type used in shared_ptr CXX-E2001
Bug risk
Major

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.

Possibly bad use of cnd_wait, cnd_timedwait, wait, wait_for, or wait_until function calls CXX-E2002
Bug risk
Major

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

Unhandled self-assignment from user-defined copy constructor CXX-E2003
Bug risk
Minor

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:

Missing or incorrect invocation of base class copy constructor CXX-W2003
Bug risk
Major
Autofix

An inheriting class with a user-defined copy constructor must call its base class's copy constructor.

Found usage of std::auto_ptr instead of std::unique_ptr CXX-W2026
Bug risk
Minor
Autofix

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.

Use of memset with possibly unintended behaviour CXX-W2048
Bug risk
Major

Found potential mistakes in memset() calls that can lead to unintended behavior. The following cases will be considered potentially unintended usage of the API memset:

Use of realloc without aliasing can lead to memory leaks CXX-W2049
Bug risk
Major

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 realloc fails, 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).

Incorrect use of strcmp CXX-W2050
Bug risk
Major

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, > 0, or == 0, to ensure the desired behavior.

Use of nullptr with string_view CXX-W2053
Bug risk
Major

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

Use of sizeof operator with stl containers CXX-W2054
Bug risk
Major

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 std::bitset and std::array.

Potentially incorrect memcmp over objects CXX-W2057
Bug risk
Major

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.

Buggy or lossy integer to string assignment CXX-W2060
Bug risk
Major

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