C & C++

C & C++

By DeepSource

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

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.

Improver seeding of pseudorandom number generator CXX-A1000
Bug risk
Minor

Improper seeding or failing to seed a pseudorandom number generator (PRNG) can lead to vulnerabilities, especially in security protocols, as an attacker can predict the sequence of random numbers that will be generated in future runs of the program. The solution is to properly seed the PRNG with an initial seed value that is not predictable or controllable by an attacker to ensure that a different sequence of random numbers is generated each time the program runs.

This issue applies only to algorithmic PRNGs that can be seeded, as true random number generators that rely on hardware to produce completely unpredictable results do not need to be and cannot be seeded.

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.

Suspicious placement of semicolon hinting difference in code behaviour and programmer's intent CXX-A1003
Bug risk
Minor
Autofix

Using a semicolon right after the conditional statement (for example an if, for, while, etc.) indicates that nothing happens if the predicate is true. Though this is sometimes intentional, but at other times this might also

Redundant access specifiers CXX-C2012
Style
Minor

Classes, structs, and unions containing redundant member (field and method) access specifiers can lead to code that is more difficult to read and maintain.

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.