# 初始化失败

By [web3zoom](https://paragraph.com/@web3zoom) · 2025-11-14

---

描述：

合约初始化时，执行构造函数，构造函数书写错误将会给合约带来重大安全漏洞。

0.4.22 版本之前的Solidity 使用合约名称创建构造函数，0.4.22 版本之后使用contractor创建构造函数。

漏洞合约：

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.30;
    
    contract NotInitialized {
        address public owner;
    
        bool public initialized;
    
        mapping (address => uint256) funds;
    
        function constructor1() external {
            owner = msg.sender;
            initialized = true;
        }
    
        function start() external payable {
            require(initialized);
            funds[msg.sender] = msg.value;
        }
    
    }
    

由于书写错误，将构造函数书写成一般函数，导致状态变量没有初始化。

真正执行的空的构造函数，始终没有初始化变量。

防范措施：

*   检查编译器版本，并以正确的形式书写构造函数；

---

*Originally published on [web3zoom](https://paragraph.com/@web3zoom/clUIRWOkM5TEMfvoPic8)*
