Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article explains extcodesize and the vulnerability that arises when contracts rely on it for access control.
You may not have encountered this opcode before. Although it is relatively uncommon in everyday Solidity code, it is worth understanding once you have learned the basics, particularly if you want to improve your Solidity skills or participate in bug bounty programs.
For an introduction to bug bounties, see the following article.
[Solidity Bug Bounty] An Introduction to Bug Bounties
What is extcodesize?

Solidity contracts can inspect the code size of an address using the EVM opcode extcodesize. Historically, developers have used this operation to try to distinguish externally owned accounts (EOAs) from contract accounts.
Externally owned accounts
An EOA is controlled by a private key rather than by ordinary deployed contract logic. A typical account created through a wallet such as MetaMask has an address beginning with 0x.
Contract accounts
A contract account has smart contract code associated with its address. Each contract deployed from Solidity or another EVM-compatible language has its own address.
A contract account does not have a corresponding private key used to authorize transactions in the way an EOA does. Its behavior is governed by its code.
extcodesize
The extcodesize opcode returns the size of the code associated with a given address, in bytes.
An ordinary deployed contract with nonempty runtime code returns a positive value, while a conventional EOA without code returns 0.
This is why developers have tried to use extcodesize to distinguish the two account types.
One possible motivation is to permit operations only from EOAs and prevent other contracts from calling a function.
However, code size is not a reliable indication of account authority or whether a call originates from a contract. This article was originally published in 2023, before EIP-7702 introduced delegated code for EOAs, which further undermines the distinction.
Implementing extcodesize

The following example demonstrates how to inspect an address's code size.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.13;
contract Codesize {
// コードのサイズチェック結果が格納される。
uint public sizeCheck;
/// @notice コードのサイズチェックを行う関数。
function checkCodeSize(address _address) public {
uint size;
assembly {
size := extcodesize(_address)
}
sizeCheck = size;
}
}
contract ContractAccount {
}Lines 12–14 use extcodesize to check the code size.
Assembly
The example uses an assembly block. In Solidity, inline assembly uses Yul to provide lower-level access to EVM operations. Unlike assembly for a physical CPU, this code targets the Ethereum Virtual Machine.
Inline assembly can offer several benefits.
Benefits
- It permits operations beyond some of the constraints of high-level Solidity.
- It can reduce gas consumption when used appropriately.
- It exposes operations that may not have a direct high-level Solidity equivalent.
This example uses inline assembly to access the low-level operation directly. Modern Solidity also exposes code size through address.code.length, which has the same relevant limitation.
For more information about assembly, see:
Execution

Passing the addresses of two EOAs to checkCodeSize leaves the value of sizeCheck at 0.
Passing the address of the ContractAccount contract, however, stores the value 63 in sizeCheck.
As expected for these examples, the deployed contract has a positive code size, while the EOAs have a code size of zero.
The extcodesize vulnerability

Despite its apparent usefulness, extcodesize is unsafe as a mechanism for restricting access to EOAs.
Why the vulnerability occurs
During contract construction, the constructor is running, but the contract's runtime code has not yet been installed at its address. Consequently, querying that address with extcodesize returns 0.
A contract under construction can therefore be classified incorrectly as an EOA.
Code walkthrough
Codesize
contract Codesize {
bool public isEOA;
/// @notice コードのサイズチェックを行う関数。
function isContract(address _accountAddress) public view returns (bool) {
uint size;
assembly {
size := extcodesize(_accountAddress)
}
return size > 0;
}
/// @notice isContract関数を実行して、EOAアカウントのアドレスかチェック。
/// EOAアカウントのアドレスであれば、isEOA変数にtrueを格納。
function checkAddress() public {
require(!isContract(msg.sender), "no contract allowed");
isEOA = true;
}
}isContract
Checks the code size of the supplied address to determine whether it has deployed code.
checkAddress
Calls isContract. If the address is classified as an EOA, the function sets isEOA to true. Otherwise, it reverts.
Attack1
contract Attack1 {
/// @notice CodesizeコントラクトのcheckAddress関数を実行する関数。
/// コントラクトアカウントのため実行に失敗する。
function hack(address _targetAddress) external {
Codesize(_targetAddress).checkAddress();
}
}hack
Calls the checkAddress function on Codesize.
The call reverts because the calling contract already has deployed runtime code.
Attack2
contract Attack2 {
bool public isContract;
address public addr;
// デプロイ時に呼ばれる。
//
constructor(address _targetAddress) {
isContract = Codesize(_targetAddress).isContract(address(this));
addr = address(this);
Codesize(_targetAddress).checkAddress();
}
}Calls the isContract and checkAddress functions of Codesize during contract construction.
Because runtime code has not yet been installed when the constructor executes, isContract sees a code size of 0.
Execution

The call from Attack1 fails.
Deploying Attack2, in contrast, sets the isEOA variable in Codesize to true, confirming that extcodesize returned 0 during construction.
The isContract variable in Attack2 is false because the isContract function in Codesize only returns true when extcodesize returns a positive value.
Complete code
Here is the complete example.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.13;
contract Codesize {
bool public isEOA;
/// @notice コードのサイズチェックを行う関数。
function isContract(address _accountAddress) public view returns (bool) {
uint size;
assembly {
size := extcodesize(_accountAddress)
}
return size > 0;
}
/// @notice isContract関数を実行して、EOAアカウントのアドレスかチェック。
/// EOAアカウントのアドレスであれば、isEOA変数にtrueを格納。
function checkAddress() public {
require(!isContract(msg.sender), "no contract allowed");
isEOA = true;
}
}
contract Attack1 {
/// @notice CodesizeコントラクトのcheckAddress関数を実行する関数。
/// コントラクトアカウントのため実行に失敗する。
function hack(address _targetAddress) external {
Codesize(_targetAddress).checkAddress();
}
}
contract Attack2 {
bool public isContract;
address public addr;
// デプロイ時に呼ばれる。
//
constructor(address _targetAddress) {
isContract = Codesize(_targetAddress).isContract(address(this));
addr = address(this);
Codesize(_targetAddress).checkAddress();
}
}Conclusion

The important distinction is between an address having deployed runtime code and an account being authorized to perform an operation. Test the examples yourself in a local development environment to see how that distinction affects the result.
Key points
- What
extcodesizemeasures. - How to inspect code size in Solidity.
- Why a zero code size is not proof that the caller is an EOA.
Do not use code size as an authentication mechanism. Define access control around the actual authorization requirements of the application.
Questions are welcome on Twitter, where I share articles about Solidity, blockchain, and Web3.
References
https://coinsbench.com/contract-with-zero-code-size-hack-solidity-14-20360e06d778


