← 返回
安全合规
中文
Solidity
Avoid common Solidity mistakes — reentrancy, gas traps, storage collisions, and security pitfalls.
避免常见的Solidity错误:重入攻击、燃气陷阱、存储冲突及安全漏洞。
ivangdavila
安全合规
clawhub
v1.0.0 1 版本 100000 Key: 无需
#latest
概述
Reentrancy
- External calls before state updates — attacker can re-enter before state changes
- Checks-Effects-Interactions pattern — validate, update state, THEN external call
ReentrancyGuard from OpenZeppelin — use nonReentrant modifier on vulnerable functionstransfer() and send() have 2300 gas limit — but don't rely on this for security
Integer Handling
- Solidity 0.8+ reverts on overflow — but
unchecked {} blocks bypass this - Division truncates toward zero —
5 / 2 = 2, no decimals - Use fixed-point math for precision — multiply before divide, or use libraries
type(uint256).max for max value — don't hardcode large numbers
Gas Gotchas
- Unbounded loops can exceed block gas limit — paginate or limit iterations
- Storage writes cost 20k gas — memory/calldata much cheaper
delete refunds gas but has limits — refund capped, don't rely on it- Reading storage in loop — cache in memory variable first
Visibility and Access
- State variables default to
internal — not private, derived contracts see them private doesn't mean hidden — all blockchain data is public, just not accessible from other contractstx.origin is original sender — use msg.sender, tx.origin enables phishing attacksexternal can't be called internally — use public or this.func() (wastes gas)
Ether Handling
payable required to receive ether — non-payable functions reject etherselfdestruct sends ether bypassing fallback — contract can receive ether without receive function- Check return value of
send() — returns false on failure, doesn't revert call{value: x}("") preferred over transfer() — forward all gas, check return value
Storage vs Memory
storage persists, memory is temporary — storage costs gas, memory doesn't persist- Structs/arrays parameter default to
memory — explicit storage to modify state calldata for external function inputs — read-only, cheaper than memory- Storage layout matters for upgrades — never reorder or remove storage variables
Upgradeable Contracts
- Constructors don't run in proxies — use
initialize() with initializer modifier - Storage collision between proxy and impl — use EIP-1967 storage slots
- Never
selfdestruct implementation — breaks all proxies pointing to it delegatecall uses caller's storage — impl contract storage layout must match proxy
Common Mistakes
- Block timestamp can be manipulated slightly — don't use for randomness or precise timing
require for user errors, assert for invariants — assert failures indicate bugs- String comparison with
== doesn't work — use keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b)) - Events not indexed — first 3 params can be
indexed for efficient filtering
版本历史
共 1 个版本
-
v1.0.0
当前
2026-03-29 00:59 安全 安全
安全检测
腾讯云安全 (Sanbu)
安全,无风险
查看报告
🔗 相关推荐
productivity
ivangdavila
创建、检查和编辑 Microsoft Word 文档及 DOCX 文件,支持样式、编号、修订记录、表格、分节符及兼容性检查等功能。
★ 438
📥 147,209
security-compliance
spclaudehome
AI智能体技能安全预审工具。安装ClawdHub、GitHub等来源技能前,检查风险信号、权限范围及可疑模式。
★ 1,211
📥 266,209
security-compliance
thomaslwang
MoltGuard — OpenClaw 安全守卫,由 OpenGuardrails 提供。安装 MoltGuard,保护您和您的用户免受提示注入、数据泄露和恶意攻击。
★ 116
📥 30,701