Unsafe External Calls and Denial of Service
Calls to other contracts and unbounded work can make a contract fail silently or stop working altogether. This lesson explains unchecked return values, non-standard tokens, dangerous delegatecalls and the forms of denial of service that affect contracts.
Learning outcomes
After completing this lesson, you will be able to:
- Explain the risk of unchecked low-level call results.
- Describe the dangers of calling untrusted code and delegatecall.
- Identify forms of denial of service in contracts.
- Choose fixes for denial-of-service designs.
Unchecked return values
Low-level call, send, staticcall and delegatecall return false when the callee fails; they do not revert. If the code ignores the result, execution continues as if the call succeeded.
// VULNERABLE: result ignored
payable(user).send(amount);
paid[user] = true;
// FIXED
(bool ok, ) = payable(user).call{value: amount}("");
if (!ok) revert PaymentFailed();
paid[user] = true;Tokens raise a related issue. Some older ERC-20 tokens return false on failure instead of reverting, or return nothing at all, which breaks code that expects a boolean. OpenZeppelin's SafeERC20 library (safeTransfer, safeTransferFrom) handles both cases and reverts on failure.
Payments that fail silently
Calling untrusted code
Every call to an address the contract does not control hands execution to unknown code, which may re-enter, consume all forwarded gas or return misleading data. delegatecall is the most dangerous form, because the callee runs with the caller's storage and balance: a malicious target can overwrite the owner variable or drain the contract. A contract must never delegatecall an address supplied by users, and proxies must restrict who can change their implementation.
Denial of service
A denial-of-service (DoS) flaw lets an attacker, or ordinary growth, make a contract unusable. Because contracts cannot be patched, a DoS can lock funds permanently. The following comparison shows the common forms.
Other DoS sources include relying on an external contract that can be paused, an oracle that can stop updating, or a privileged account whose key is lost.
What kind of denial of service?
The stuck auction
An auction's bid() refunds the previous highest bidder with require(payable(prev).send(prevBid)) before recording the new bid. An attacker bids from a contract with no receive function. What happens next?
Every later bid must refund the attacker's contract, which rejects ether, so send returns false and the require reverts every new bid. The attacker wins at their price. The fix is the pull pattern: record pendingReturns[prev] += prevBid and let each outbid bidder call withdraw() themselves.
An airdrop that grows too large
A rewards contract stores every participant in an array and pays all of them in distribute(). It worked with 500 participants. At 60,000 participants it always fails. Why?
Gas grows with the number of participants. At 60,000 transfers, the transaction needs far more than 16,777,216 gas, the most one transaction may use, so it can never succeed. The contract should let each participant claim individually (for example with a Merkle proof of eligibility) or process the list in bounded batches across several transactions.
What does a low-level call return when the callee reverts, and what must the caller do?
Show answer
It returns false (with the revert data) instead of reverting. The caller must check the success flag and revert or record the failure.
Exam Tips
Remember for the exam
- Check the success flag of every low-level call; use SafeERC20 for tokens.
- Never delegatecall untrusted or user-supplied addresses.
- Unbounded loops eventually exceed the transaction gas cap.
- Pull payments stop one failing recipient from blocking everyone.