Assert Violation (SWC-110)

Description
The Solidity assert() function is used to check for invariants in the code. Invariants are conditions that should always hold true, and if they don’t, it indicates that something has gone wrong in the code. When the assert statement is executed, if the condition it checks is false, the contract execution is halted and any remaining gas is refunded to the caller.
A failing assert statement indicates that the contract has entered an unexpected or invalid state. This could mean that there is a bug in the contract code that allowed it to enter that state, or that there is an external factor causing the invalid state. It is important to investigate the reason for the failing assert statement and fix any bugs that may exist in the contract.
It is also possible that the assert statement is being used incorrectly. For example, it may be used to validate user inputs, which should be checked using require statements instead. In this case, the assert statement is not checking an invariant of the code, but rather an input that should have been validated before executing the contract. In such cases, the use of assert can result in a security vulnerability, as it could allow an attacker to exploit an input validation flaw in the contract. Therefore, it is important to use assert statements correctly and only to check for invariants in the code.
Remediation
To address SWC-110, one approach is to review the condition being checked in the assert() statement and determine whether it is actually an invariant or not. If it is not an invariant, it should be replaced with a require() statement. The require() statement is used to check inputs and conditions that must hold for the function to execute properly, but are not necessarily invariants.
If the condition is an invariant, then the assertion should not be reachable. If it is reachable, then there is likely a bug in the code that needs to be fixed. Properly functioning code should never reach a failing assert statement, so if it does, it indicates that there is a problem with the code that needs to be addressed.
Contract Samples
Code with a vulnerability
Code without a vulnerability
contract SimpleDSChief {
mapping(bytes32=>address) public slates;
mapping(address=>bytes32) public votes;
mapping(address=>uint256) public approvals;
mapping(address=>uint256) public deposits;
function lock(uint wad) public {
deposits[msg.sender] = add(deposits[msg.sender], wad);
addWeight(wad, votes[msg.sender]);
}
function free(uint wad) public {
deposits[msg.sender] = sub(deposits[msg.sender], wad);
subWeight(wad, votes[msg.sender]);
}
function voteYays(address yay) public returns (bytes32){
bytes32 slate = etch(yay);
voteSlate(slate);
return slate;
}
function etch(address yay) public returns (bytes32 slate) {
bytes32 hash = keccak256(abi.encodePacked(yay));
slates[hash] = yay;
return hash;
}
function voteSlate(bytes32 slate) public {
uint weight = deposits[msg.sender];
subWeight(weight, votes[msg.sender]);
votes[msg.sender] = slate;
addWeight(weight, votes[msg.sender]);
}
function addWeight(uint weight, bytes32 slate) internal {
address yay = slates[slate];
approvals[yay] = add(approvals[yay], weight);
}
function subWeight(uint weight, bytes32 slate) internal {
address yay = slates[slate];
approvals[yay] = sub(approvals[yay], weight);
}
function add(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x);
}
function sub(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x);
}
function checkAnInvariant() public {
bytes32 senderSlate = votes[msg.sender];
address option = slates[senderSlate];
uint256 senderDeposit = deposits[msg.sender];
assert(approvals[option] >= senderDeposit);
}
}
Tools for scaning SWC-110
1. Mythril – an open-source security analysis tool for Ethereum smart contracts that can detect various types of security vulnerabilities, including SWC-110.
2. Securify – a tool that analyzes Solidity smart contracts for security vulnerabilities, including SWC-110, by applying static analysis techniques.
3. SmartCheck – a security scanner that uses static analysis to identify security issues in Solidity code, including SWC-110.
4. Slither – an open-source static analysis tool for Solidity that can detect SWC-110 and other security vulnerabilities.
The Common Weakness Enumeration (CWE)
Mitigation for SWC-110
1. Use require() instead of assert() for input validation: While assert() is useful for detecting unexpected behavior and invariant violations, require() is a better choice for validating input conditions. require() should be used to validate input conditions where it is possible for the caller of the function to provide incorrect or malicious inputs.
2. Avoid integer overflows and underflows: One of the most common causes of assert() failures is integer overflows and underflows. To avoid these issues, you should always use safe arithmetic functions like SafeMath or the built-in Solidity overflow checks.
3. Check input values for expected range: Before performing any arithmetic or comparison operation on input values, you should check that they are within the expected range. This can help prevent unintended behavior caused by incorrect input values.
4. Use appropriate data types: Using the appropriate data types for your variables can also help prevent unintended behavior. For example, using uint256 instead of uint32 for a variable that may need to hold a large value can help prevent overflow issues.
5. Write comprehensive tests: It is important to write comprehensive tests to ensure that your smart contract functions as expected and to catch any unexpected behavior. Tests should include both positive and negative scenarios, including edge cases.
6. Use static analysis tools: Static analysis tools like Mythril and Securify can help detect potential vulnerabilities in your smart contract code, including SWC-110 issues.
Conclusion
SWC-110 is a vulnerability related to the misuse of the assert() statement in Solidity. The assert() statement is meant to check for invariants that should never be violated, and its failure should not be recoverable. However, if a reachable assert() statement fails, it could indicate either a bug that allows the contract to enter an invalid state or the incorrect use of the assert() statement to validate inputs.
To mitigate SWC-110, developers should consider whether the condition checked by the assert() statement is an invariant. If it is not, they should replace the assert() statement with a require() statement. If the exception is indeed caused by unexpected behavior of the code, the developers should fix the underlying bug(s) that allow the assertion to be violated.
To scan for this vulnerability, several static analysis tools can be used, such as Mythril, Slither, or Securify. Additionally, it is recommended to perform manual testing of the smart contract, following a well-defined testing plan.
Other Services
Insomnia Security Scanner
AI-powered web application security scanner by CQR. Automated vulnerability discovery, exploit verification, and detailed reporting for modern applications.
Learn moreInfrastructure Protection by CRYEYE
Security audits via CryEye provide enterprise information security, protecting the entire infrastructure.
Learn morePenetration Testing
Find vulnerabilities across your entire business infrastructure before hackers do! At penetration testing consulting, we will select pentest methods and other custom cybersecurity recommendations for your business.
Learn moreSocial Engineering
Simulate real-world phishing, vishing, and pretexting attacks to measure and improve your team's security awareness and response capabilities.
Learn morePerformance Testing
All kinds of load and performance testing of your system from the CQR online security company.
Learn moreAI-Powered Vulnerability Assessment
Leverage artificial intelligence to discover, prioritize, and remediate vulnerabilities across your digital assets faster and more accurately than traditional scanners.
Learn moreCloud Security Audit (AWS / GCP / Azure)
Comprehensive security review of your cloud environments — IAM policies, network controls, data exposure, and misconfigurations across all major cloud platforms.
Learn moreDevSecOps Integration
Embed security into every stage of your CI/CD pipeline. Automated SAST, DAST, SCA, and secret scanning so vulnerabilities are caught before they reach production.
Learn moreAPI Security Testing
In-depth testing of REST, GraphQL, and SOAP APIs for authentication flaws, authorization bypasses, injection vulnerabilities, and data leakage risks.
Learn moreMobile Application Penetration Testing
Manual and automated security testing for iOS and Android applications — reverse engineering, runtime analysis, traffic interception, and backend API assessment.
Learn moreIoT Security Assessment
Evaluate firmware, communication protocols, cloud backends, and physical interfaces of IoT devices to identify vulnerabilities before attackers do.
Learn moreBlockchain & Smart Contract Audit
Formal verification and manual code review of smart contracts on Ethereum, Solana, and other chains. Detect reentrancy, overflow, and logic flaws before deployment.
Learn moreRed Team Operations
Advanced adversary simulation using real attacker TTPs (MITRE ATT&CK) to test your detection, response, and overall security posture under realistic conditions.
Learn moreThreat Intelligence & Monitoring
Continuous monitoring of threat feeds, dark web, and attacker infrastructure to provide actionable intelligence specific to your organization and industry.
Learn moreZero Trust Architecture Review
Assess and design your Zero Trust security model — identity verification, micro-segmentation, least-privilege access, and continuous validation controls.
Learn moreCompliance Consulting (PCI DSS / SOC 2 / GDPR)
Expert guidance to achieve and maintain compliance with major security frameworks. Gap analysis, remediation roadmaps, and audit-readiness support.
Learn moreDark Web Monitoring
Continuous surveillance of dark web forums, marketplaces, and breach databases for leaked credentials, sensitive data, or mentions of your organization.
Learn morePhishing Simulation & Awareness Training
Controlled phishing campaigns combined with interactive security awareness training to build a human firewall across your entire organization.
Learn moreSupply Chain Security Audit
Assess third-party vendor risks, open-source dependencies, and software supply chain integrity to prevent attacks like SolarWinds and Log4Shell.
Learn moreContainer & Kubernetes Security
Security review of Docker images, Kubernetes clusters, RBAC policies, network policies, and runtime configurations to harden your container infrastructure.
Learn moreWeb Application Firewall (WAF) Deployment
Professional WAF setup, rule tuning, and ongoing management to block SQL injection, XSS, CSRF, and other OWASP Top 10 threats in real time.
Learn moreBug Bounty Program Management
Full lifecycle management of your bug bounty program — scope definition, researcher coordination, triage, validation, and remediation tracking.
Learn moreOSINT Investigation Services
Open-source intelligence gathering on individuals, organizations, and infrastructure. Ideal for pre-engagement recon, fraud investigation, and competitive analysis.
Learn moreDigital Forensics & Incident Response
Rapid response to security breaches — evidence collection, malware analysis, attacker timeline reconstruction, and actionable remediation recommendations.
Learn more