界: Code Quality

程式碼品質不佳,會導致無法預料的行為。從使用者的角度來看,這通常表現為可用性不佳。對於攻擊者而言,這提供了以意想不到的方式向系統施加壓力的機會。

Solidity Bad Practices: Hardcoded Gas Amount

Abstract
合約定義了固定燃料量或叫用具有固定燃料量的函數。
Explanation
燃料交易成本可能會根據當前網路狀況而異,例如,硬分叉期間 EVM (以太坊虛擬機器) 指令的燃料成本可能會受到顯著影響。這可能會打破仰賴固定燃料量的現有功能,也可能影響用於價值轉移的交易,例如 transfer()send(),其使用 2300 的固定燃料量。

範例 1:以下程式碼執行呼用並指定固定燃料量。


interface ICallable {
function callMe() external;
}

contract HardcodedNotGood {
function callWithArgs() public {
callable.callMe{gas: 10000}();
}
}
References
[1] Enterprise Ethereum Alliance Gas and Gas Prices
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 2
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 3
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 665
[7] Standards Mapping - Smart Contract Weakness Classification SWC-134
desc.structural.solidity.swc134