Last updated: March 11, 2025
SCWE-070: Incorrect Constructor Name
Stable Version v0.0.1
This content is in the version-(v0.0.1) and still under active development, so it is subject to change any time (e.g. structure, IDs, content, URLs, etc.).
Send Feedback
Relationships
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.
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
contract Example {
uint public value;
// Incorrect constructor name (for Solidity <0.4.22)
function Example() public { // Constructor name must match the contract name in older Solidity versions
value = 10;
}
}
Fixed Contract Example
contract Example {
uint public value;
// Correct constructor definition (Solidity >=0.4.22)
constructor() public { // Use "constructor" instead of the contract name
value = 10;
}
}