Recently I checking Buffer.sol smart contract and I found this Code

Link = https://github.com/ensdomains/buffer/blob/master/contracts/Buffer.sol

function init(buffer memory buf, uint capacity) internal pure returns(buffer memory) {
    if (capacity % 32 != 0) {
        capacity += 32 - (capacity % 32);
    // Allocate space for the buffer data
    buf.capacity = capacity;
    assembly {
        let ptr := mload(0x40)
        mstore(buf, ptr)
        mstore(ptr, 0)
        let fpm := add(32, add(ptr, capacity))
        if lt(fpm, ptr) {
            revert(0, 0)
        mstore(0x40, fpm)
    return buf;

Here the Code is correct but in many web3 contract I found that

 if lt(fpm, ptr) {
            revert(0, 0)

This Check in missing in many web3 contracts so I want to know what is the purpose of this check while Initializes a buffer with an initial capacity ( Init() function )

Thank You

1 Answer 1


It's just an overflow check.

When calculating let fpm := add(32, add(ptr, capacity)), it's possible that 32+prt+capacity ends up greater than 2**256; this overflow doesn't raise errors since we are inside a assembly block. Checking that fpm > ptr is sufficient to prevent that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.