C & C++

C & C++

Made by DeepSource
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.

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.

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.

Dangling references in value handles std::string_view CXX-W2004
Bug risk
Minor

The std::string_view handle outlives it's data source instance.

Unintended implicit conversion of a boolean pointer in a condition CXX-W2002
Bug risk
Major
Autofix

A pointer to the boolean type is used in the if condition with implicit conversion. The same pointer is never dereferenced.

Incorrect usage of erase-remove pattern CXX-W2007
Bug risk
Major
Autofix

Call to "erase-remove" combination without end iterator results in incomplete removal of container elements when more than one element is removed.

Found a call to ancestor's virtual method instead of direct parent's method CXX-W2008
Bug risk
Major

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

Potential divide by zero CXX-W1264
Bug risk
Major

Dividing by zero can cause a runtime error, leading to a crash. Because as such dividing by zero is undefined behaviour.

Side-effects in the right-hand operand of logical operators (&&, and ||) CXX-W1066
Bug risk
Major

Having 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 CXX-W1161
Bug risk
Major

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.

Found incomplete function parameters in declaration CXX-W3032
Bug risk
Major

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.

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.

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:

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: