sherlock-audit / 2023-02-blueberry-judging

12 stars 5 forks source link

Avci - divCeil gives strange results #367

Closed github-actions[bot] closed 1 year ago

github-actions[bot] commented 1 year ago

Avci

medium

divCeil gives strange results

Summary

divCeil gives strange results

Vulnerability Detail

divCeil(1001, 1000) returns 2 divCeil(1, 1000) returns 1

should return ceil amount only if above half

Impact

wrong dive results

Code Snippet

 function divCeil(uint256 a, uint256 b) internal pure returns (uint256) {
        return (a + b - 1) / b;
    }

https://github.com/sherlock-audit/2023-02-blueberry/blob/main/contracts/libraries/BBMath.sol#L7

Tool used

Manual Review

Recommendation

May use this modified function

function divCeil2(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0);
        uint256 c = a / b;
        if ( 2 * (a % b) > b ) c = c + 1;` 
        return c;
    }