C & C++

C & C++

Made by DeepSource
Using storage specifier static inside an anonymous namespace is redundant CXX-C2021
Anti-pattern
Minor
Autofix

Using the storage specifier static inside an anonymous namespace is redundant. The anonymous namespace already limits the visibility of definitions to a single translation unit. To fix the issue, simply remove the static specifier from the definitions inside the anonymous namespace. This will make the code more concise.

Missing unconditional break statement in switch clause CXX-C1001
Anti-pattern
Minor

Fall-through in switch cases is often considered risky. Hence consider adding an unconditional break for each switch clause.

Found the use of static member variable through class instance rather then class name CXX-C2022
Style
Minor

Accessing static member variables through class instances can give the impression that the member belongs to the instance, which is incorrect. This can lead to confusion and make the code harder to understand and maintain. To fix this issue, simply replace the instance access with the class name followed by the member variable. This clearly indicates that the member is a static member of the class and avoids any confusion.

Risky cast after possibly misaligned pointer offset CXX-S1014
Security
Major

Pointer offset(or any other arithmetic operation) on a pointer casted to a different type (than its original type) is risky and can result in undefined behaviour. The reason for such behaviour is that the memory alignment may change for types on every targeted platform.

Unnecessary copy of non-trivially copyable type CXX-P2005
Performance
Major

Copying an object is an expensive operation, especially if the type of the object is not trivial to copy. To avoid creating an unnecessary copy of such object, use const references.

Found copy-on-return when move-on-return is faster CXX-P2006
Performance
Major

The analyser has found a local variable that can't be automatically moved. The reason for this behavior is the constness of the variable. Consider declaring the variable without the qualifier const.

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.

Inefficient use of std::vector in loop CXX-P2007
Performance
Major

Manually appending a significant number of elements to a std::vector without reserving the memory upfront can cause performance issues as the memory needs to be reallocated and copied every time the std::vector's size increases. This can be costly if many items need to be pushed into the std::vector. Consider using the std::vector::reserve() function to preallocate memory for the std::vector before a loop containing a std::vector::push_back.

Potential buffer overrun CXX-S1005
Security
Major

While writing data to a buffer, the program can overrun the buffer's boundary and overwrite adjacent memory locations. These can either cause a crash if the memory region is inaccessible to the process for writing, or in the worst case produce a vulnerability to overwrite parts of the memory with untrusted user code.

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.

Missing default case in switch statement CXX-W1164
Anti-pattern
Minor

Default case provides switch statements with fallback, and in general is a good to have. Hence consider adding default case to switch.

Special symbols like *, ", ', \ and /* found in header names CXX-W1207
Anti-pattern
Minor

Using special-meaning characters in header names can produce errors in parsing.

Consider cleaning up header 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.

Control variable of for loop modified in body CXX-W1241
Anti-pattern
Minor

Modifying the control variable of a for loop in its body can make the code harder to read.

Consider using a while loop, or move the modification into the for loop's update expression.

Multiple declarations in single statement CXX-C2013
Style
Minor

Local variable declarations with more than one variable in a single statement can lead to confusion and make the code harder to read. It is recommended to refactor the code to have one declaration per statement, with each statement on a separate line.

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.

Boolean expression can be simplified CXX-C2015
Anti-pattern
Minor

Boolean expressions involving boolean constants can be simplified to use the appropriate boolean expression directly, using DeMorgan's Theorem. This helps in improving code readability and reducing unnecessary complexity.

Using std::string::compare over equality operators CXX-C2017
Anti-pattern
Minor

Found using equality string comparison method std::string::compare instead of using == which makes the code less readable.