Skip to content
Last updated: March 11, 2025

SCWE-082: Lack of Proper Gas Management

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

Gas management is crucial in smart contracts to ensure that they do not run out of gas or cause excessive consumption. If gas consumption is not properly controlled, a contract can fail to execute or can be exploited by attackers to cause denial of service (DoS).

Remediation

Properly estimate the gas required for functions and set appropriate gas limits. Use require or other mechanisms to handle gas consumption failures and ensure that gas usage remains within acceptable bounds.

Vulnerable Contract Example

contract Example {
    function execute() public {
        while (true) { 
            // Excessive gas consumption, no limit set
        }
    }
}

Fixed Contract Example

contract Example {
    uint public counter;

    function execute(uint _iterations) public {
        require(_iterations <= 100, "Too many iterations"); // Limit iterations to avoid excessive gas usage
        for (uint i = 0; i < _iterations; i++) {
            counter++;
        }
    }
}