Skip to content

Arithmetic Underflow

Description

The code is vulnerable to an arithmetic underflow, which occurs when the result of a subtraction operation is less than the minimum value that can be represented by the data type, causing it to wrap around to a large value.

Examples

Insecure Code

solidity
function test(uint256 x) public { uint256 result = x - 1; }

Secure Code

solidity
function test(uint256 x) public { require(x > 0); uint256 result = x - 1; }

Remediation

Add input validation to ensure that the subtraction operation does not result in an underflow. Consider using a library that provides safe arithmetic operations.

Rule Details

FieldValue
IDCODE-0250
CategoryCrypto
SeverityHIGH
CWECWE-191
ConfidenceLOW
ImpactHIGH
LikelihoodMEDIUM
ExploitabilityEASY
Tagsinteger underflow, arithmetic underflow
OWASPN/A

References