placeBid causes precision loss due to truncation, enabling bids that violate the 5% minimum increment ruleThis contract makes sure that each new bid must be at least 5% higher than the previous one. The logic for this is implemented in the BidBeastsNFTMarketPlace::placeBid function (lines 156-157):
The issue here is that the calculation (previousBidAmount / 100) * (100 + S_MIN_BID_INCREMENT_PERCENTAGE) uses division first, which truncates (integer division) and loses precision. This means that for certain bid amounts, the calculated requiredAmount can be lower than the intended 5% increase.
There's a simple rule the calculations should follow - Always Multiply Before Dividing. Although many developers follow this rule, "Hidden Precision Loss" can still occur, resulting from complex calculations across different functions or contracts. But that's not the case here, as the calculation is straightforward.
When we divide first, the fractional part is discarded completely. For example, if a = 5, b = 3, and c = 6, the bad approach gives (5/3) * 6 = 1 * 6 = 6 (lost precision)
The good approach maintains precision longer (correct (5 * 6) / 3 = 30 / 3 = 10 result).
Hence, the correct way to calculate the minimum required bid should be:
Likelihood: Medium
Occurs with certain bid amounts (not all)
More likely with smaller bids or awkward decimal amounts (e.g., 1.200000000000099999 ETH)
Impact: Medium
Minor Financial Loss: Financial loss is typically small (a few wei to small amounts).
Cumulative Effect: Small losses may accumulate over multiple auctions.
Rule Violation: Undermines the contract’s 5% bid increment requirement.
Add the following test test_bidIncrementPrecisionLoss in the test file:
Run the above test using the command:
The output we get:
Simply change the calculation to multiply first, then divide:
Integer division in requiredAmount truncates fractions, allowing bids slightly lower than intended.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.