SCWE-070: Incorrect Constructor Name
Stable Version v1.0
This content is in the version-(v1.0) and still under active development, so it is subject to change any time (e.g. structure, IDs, content, URLs, etc.).
Relationships¶
- CWE-1001: Variable Shadowing
https://cwe.mitre.org/data/definitions/1001.html
Description¶
In Solidity, the constructor is a special function used to initialize a contract's state variables when it is deployed. If a constructor is incorrectly named, it will not function as expected, leading to issues such as failing to initialize state variables or triggering unexpected behavior. The constructor must have the exact name of the contract and no return type.
If the constructor name is not correct, it will not be executed as intended, and the contract may not behave as expected, potentially leaving it in an uninitialized or inconsistent state.
Remediation¶
Ensure that the constructor has the correct name, which must match the contract name and contain no return type. In newer versions of Solidity (0.4.22 and later), the constructor keyword is used instead of the contract name for constructor functions.
Vulnerable Contract Example¶
pragma solidity ^0.4.0;
contract Example {
uint public value;
// In Solidity <0.4.22, constructor had to match contract name. In 0.5+, this is a regular function, not a constructor.
function Example() public {
value = 10;
}
}