How do you secure smart contracts against reentrancy, overflows?
dApp Developer
answer
Smart contract security relies on defense in depth: prevent reentrancy with the checks-effects-interactions pattern, reentrancy guards, and pull-payments. Avoid integer overflow by using Solidity ≥0.8 (built-in checks) or SafeMath libraries. Minimize external calls, restrict privileges via access control, and validate all inputs. Add automated analysis (Slither, MythX), fuzzing, unit tests, and audits. Immutable contracts need extra care: once deployed, bugs can’t be patched.
Long Answer
Securing smart contracts is a top priority for any dApp developer, as vulnerabilities can directly lead to the theft of funds, manipulation of state, or permanent denial of service. Unlike traditional software, contracts are immutable once deployed, so you must adopt defense-in-depth strategies that anticipate both known and novel attack vectors.
1) Core vulnerability classes
- Reentrancy: An external call lets a malicious contract re-enter before state updates, draining funds (DAO hack).
- Integer overflow/underflow: Arithmetic wraps around if not checked.
- Access control flaws: Privileged functions not properly restricted.
- Unchecked external calls: Ignoring call return values leads to hidden failures.
- Front-running: Miners or bots exploit transaction ordering.
2) Preventing reentrancy
- Adopt the checks-effects-interactions pattern: update contract state first, then perform external calls.
- Use reentrancy guards (nonReentrant modifier from OpenZeppelin).
- Prefer pull over push payments: let users withdraw funds instead of sending automatically.
- Avoid complex logic after external calls.
3) Preventing overflows and underflows
- Use Solidity ≥0.8, which reverts on overflow automatically.
- In older versions, rely on SafeMath libraries (add, sub, mul) to enforce safe arithmetic.
- Validate user-supplied amounts (e.g., cap maximum deposits).
4) General secure coding practices
- Minimize the contract’s attack surface by keeping contracts small and modular.
- Implement role-based access control using Ownable or AccessControl.
- Avoid hardcoded addresses and magic numbers.
- Always check external call return values and handle errors gracefully.
5) Testing and automated analysis
- Static analysis tools: Slither, MythX, Oyente catch common issues.
- Property-based testing/fuzzing: Echidna, Foundry fuzz inputs to expose corner cases.
- Unit/integration tests: Validate invariants (balances never negative, sum of deposits = total supply).
- Formal verification: Prove critical properties (e.g., token total supply invariant).
6) Deployment and monitoring
- Use testnets before mainnet deployment; simulate attack scenarios.
- Add circuit breakers (pausable contracts) to stop execution in emergencies.
- Monitor deployed contracts with tools like Tenderly, Forta, or OpenZeppelin Defender to detect anomalies.
7) Real-world example
The DAO hack in 2016 exploited reentrancy to drain 3.6M ETH. Mitigation today would involve checks-effects-interactions, pull-payments, and nonReentrant. Similarly, earlier ERC20 tokens suffered from overflow/underflow, fixed now by Solidity 0.8’s safe arithmetic. More recent DeFi exploits highlight the need for upgradable proxies, continuous monitoring, and layered defenses.
8) Governance and audits
No security strategy is complete without external eyes. Schedule third-party audits before production. Adopt bug bounty programs to crowdsource vulnerability discovery.
In short, securing smart contracts means assuming hostile actors will probe every function. Use secure patterns, language features, testing, monitoring, and audits to minimize attack surfaces and preserve user trust.
Table
Common Mistakes
Common mistakes include sending Ether before updating state (classic reentrancy bug), using outdated Solidity versions vulnerable to overflow, or writing privileged functions without modifiers. Developers often ignore return values of low-level call, leading to silent failures. Overcomplicated contracts expand attack surfaces. Lack of fuzzing or automated analysis leaves corner cases unchecked. Another mistake: skipping third-party audits, assuming in-house testing is enough. Finally, not planning for emergency stops (circuit breakers) means exploits can drain contracts without recourse.
Sample Answers (Junior / Mid / Senior)
Junior:
“I’d use Solidity 0.8+ for overflow safety and follow checks-effects-interactions to stop reentrancy. I’d also add nonReentrant guards from OpenZeppelin.”
Mid:
“I’d design small, modular contracts with access controls. For reentrancy, I’d use pull-payments and update state before external calls. I’d rely on SafeMath or built-in overflow checks, and run Slither + unit tests to catch issues.”
Senior:
“I’d enforce least privilege with RBAC, pausable circuit breakers, and nonReentrant modifiers. Automated pipelines run Slither, MythX, and Echidna fuzzing. I’d validate invariants with property-based tests and, for critical contracts, apply formal verification. Before mainnet deployment, I’d require two audits and set up on-chain monitoring with Forta. In production, anomalies trigger emergency pause until patched.”
Evaluation Criteria
Interviewers expect:
- Awareness of core vulnerabilities (reentrancy, overflow, access control).
- Knowledge of mitigation patterns (checks-effects-interactions, SafeMath, guards).
- Use of static/dynamic analysis tools (Slither, MythX, Echidna).
- Secure coding habits: modular design, minimized external calls, input validation.
- Deployment safeguards: testnets, bug bounties, circuit breakers.
- Real-world awareness of exploits (DAO, ERC20 bugs).
Weak answers focus only on “just use SafeMath” or “update Solidity.” Strong answers show layered defenses, tooling, audits, and continuous monitoring.
Preparation Tips
Set up a demo Solidity project. Write a contract vulnerable to reentrancy (Ether transfer before state update). Exploit it with a malicious contract to see funds drained. Then fix with checks-effects-interactions and nonReentrant. Add unit tests with Foundry or Hardhat to confirm. Create a contract vulnerable to overflow in Solidity 0.7; test SafeMath. Upgrade to 0.8 and observe built-in checks. Run Slither to detect issues; use Echidna for fuzz testing invariants. Add Ownable/AccessControl for role management. Deploy on testnet, simulate front-running with bots. Document findings and prepare a 60–90s explanation.
Real-world Context
The DAO hack (2016) is the classic reentrancy exploit—fixed today with CEI and nonReentrant. In 2018, an ERC20 integer overflow let attackers mint unlimited tokens by wrapping arithmetic. SafeMath or Solidity 0.8 auto-checks prevent this. A DeFi protocol once ignored call return values; funds were stuck when transfers failed silently. Another platform added circuit breakers after flash loan attacks, letting them pause contracts while patching. These real cases prove that vulnerabilities repeat unless systematically defended with patterns, libraries, audits, and monitoring.
Key Takeaways
- Always apply checks-effects-interactions and nonReentrant.
- Use Solidity ≥0.8 or SafeMath for overflow protection.
- Implement access control with roles and ownership.
- Rely on audits, fuzzing, and static analysis.
- Add circuit breakers and monitoring for runtime safety.
Practice Exercise
Scenario: You’re deploying a DeFi smart contract that handles deposits and withdrawals. A teammate warns of reentrancy and overflow risks.
Tasks:
- Write a vulnerable withdraw function: send Ether before updating balance. Simulate an attacker draining via recursive calls.
- Fix with checks-effects-interactions: update balances before transfer.
- Add a nonReentrant modifier and switch to pull-payments.
- Write a vulnerable arithmetic function in Solidity 0.7. Exploit it to overflow a balance.
- Upgrade to Solidity 0.8 and observe automatic revert.
- Add SafeMath in legacy code.
- Configure Slither and Echidna to scan and fuzz test.
- Add Ownable for admin-only functions.
- Deploy to a testnet, run transactions, and validate security.
Deliverable: A 60–90s pitch summarizing vulnerabilities found, how you exploited and fixed them, and which patterns/tools you now enforce.

