Kingdom: Code Quality

Poor code quality leads to unpredictable behavior. From a user's perspective that often manifests itself as poor usability. For an attacker it provides an opportunity to stress the system in unexpected ways.

94 items found
Weaknesses
Abstract
The class contains a field and a method with the same name.
Explanation
It is confusing to have a member field and a method with the same name. It makes it easy for a programmer to accidentally call the method when attempting to access the field or vice versa.

Example 1:

public class Totaller {
private int total;
public int total() {
...
}
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398, CWE ID 710
[6] Standards Mapping - Smart Contract Weakness Classification SWC-119
desc.structural.java.poor_style_confusing_naming.member_and_method
Abstract
The contract uses a shadowed variable which is ambiguous and prone to misuse.
Explanation
Solidity allows developers to ambiguously declare state variables. This means that even though two different variables in two different contexts can be declared with the same name, using them can lead to confusion and misuse.

This can happen both at the function level and at the inheritance level. For example, if Contract1 declares var1 and inherits from Contract2, which also declares a variable named var1, then the variable is ambiguous and can easily be confused with each other later in the smart contract execution.

Example 1: The following code uses inheritance and declares a state variable with the same name in both smart contracts. It can be hard to determine which is the actual hardcap of the token sale.


contract Tokensale {
uint hardcap = 10000 ether;

function Tokensale() { }

function fetchCap() public constant returns(uint) {
return hardcap;
}
}

contract Presale is Tokensale {
uint hardcap = 1000 ether;

function Presale() Tokensale() { }
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398, CWE ID 710
[6] Standards Mapping - Smart Contract Weakness Classification SWC-119
desc.structural.solidity.swc119
Abstract
This synchronized block contains no statements; it is unlikely the synchronization achieves the intended effect.
Explanation
Synchronization in Java can be tricky. An empty synchronized block is often a sign that a programmer is wrestling with synchronization but has not yet achieved the result they intend.

Example:

synchronized(this) { }
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 585
desc.structural.java.poor_style_empty_synchronized_block
Abstract
Using a dollar sign ($) as part of an identifier is not recommended.
Explanation
Section 3.8 of the Java Language Specification reserves the dollar sign ($) for identifiers that are used only in mechanically generated source code.

Example:

int un$afe;
References
[1] J. Gosling, B. Joy, G. Steele, G. Bracha The Java Language Specification, Second Edition Addison-Wesley
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 398
desc.structural.java.poor_style_identifier_contains_dollar_symbol
Abstract
The variable's value is assigned but never used, making it a dead store.
Explanation
This variable's initial value is not used. After initialization, the variable is either assigned another value or goes out of scope.

Example: The following code excerpt assigns to the variable r and then overwrites the value without using it.


int r = getNum();
r = getNewNum(buf);
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
[6] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3050 CAT II
[7] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3050 CAT II
[8] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3050 CAT II
[9] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3050 CAT II
[10] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3050 CAT II
[11] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3050 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3050 CAT II
desc.structural.cpp.poor_style_redundant_initialization
Abstract
The variable's value is assigned but never used, making it a dead store.
Explanation
This variable's initial value is not used. After initialization, the variable is either assigned another value or goes out of scope.

Example: The following code excerpt assigns to the variable r and then overwrites the value without using it.


int r = getNum();
r = getNewNum(buf);
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
[6] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3050 CAT II
[7] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3050 CAT II
[8] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3050 CAT II
[9] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3050 CAT II
[10] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3050 CAT II
[11] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3050 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3050 CAT II
desc.structural.java.poor_style_redundant_initialization
Abstract
The variable's value is assigned but never used, making it a dead store.
Explanation
This variable's value is not used. After the assignment, the variable is either assigned another value or goes out of scope.

Example: The following code excerpt assigns to the variable r and then overwrites the value without using it.


r = getName();
r = getNewBuffer(buf);
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 563
[6] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3050 CAT II
[7] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3050 CAT II
[8] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3050 CAT II
[9] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3050 CAT II
[10] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3050 CAT II
[11] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3050 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3050 CAT II
desc.structural.cpp.poor_style_value_never_read
Abstract
The variable's value is assigned but never used, making it a dead store.
Explanation
This variable's value is not used. After the assignment, the variable is either assigned another value or goes out of scope.

Example: The following code excerpt assigns to the variable r and then overwrites the value without using it.


r = getName();
r = getNewBuffer(buf);
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 563
[6] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3050 CAT II
[7] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3050 CAT II
[8] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3050 CAT II
[9] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3050 CAT II
[10] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3050 CAT II
[11] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3050 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3050 CAT II
desc.structural.java.poor_style_value_never_read
Abstract
This variable is never used.
Explanation
This variable is never used. It is likely that the variable is simply vestigial, but it is also possible that the unused variable points out a bug.

Example: In the following code, a copy-and-paste error has led to the same loop iterator (i) being used twice. The variable j is never used.


int i,j;

for (i=0; i < outer; i++) {
for (i=0; i < inner; i++) {
...
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 563
[6] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2008 Rule 0-1-3
[7] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3050 CAT II
[8] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3050 CAT II
[9] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3050 CAT II
[10] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3050 CAT II
[11] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3050 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3050 CAT II
[13] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3050 CAT II
[14] Standards Mapping - Smart Contract Weakness Classification SWC-131
desc.structural.cpp.poor_style_variable_never_used
Abstract
The contract defines a variable but never uses it.
Explanation
Solidity permits variables to be declared and never used and although most of the time this does not directly point to a security vulnerability, it is a bad practice. It can cause noise and unnecessary gas consumption due to the required increased computation cycles.

Example 1: The following code declares the variable var1 of type A but never uses it.


contract Base {
struct A { uint a; }
}

contract DerivedA is Base {
A var1 = A(1);
int internal j = 500;

function call(int a) public {
assign1(a);
}

function assign3(A memory x) public returns (uint) {
return g[1] + x.a + uint(j);
}
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 563
[6] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2008 Rule 0-1-3
[7] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3050 CAT II
[8] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3050 CAT II
[9] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3050 CAT II
[10] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3050 CAT II
[11] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3050 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3050 CAT II
[13] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3050 CAT II
[14] Standards Mapping - Smart Contract Weakness Classification SWC-131
desc.structural.solidity.swc131
Abstract
Functions with inconsistent implementations across operating systems and operating system versions cause portability problems.
Explanation
The behavior of functions in this category varies by operating system, and at times, even by operating system version. Implementation differences can include:

- Slight differences in the way parameters are interpreted leading to inconsistent results.

- Some implementations of the function carry significant security risks.

- The function might not be defined on all platforms.
desc.semantic.cpp.portability_flaw