1

When I use add() function from Solidity SafeMath library, I get infinite gas requirement in remix (and this reflects in transactions that wont go through).

Simplified example:

function someFunction() internal view returns (bool) {
    uint256 a = 23;
    uint256 b = 48;
    return (a.add(1) > b);
}

SafeMath is included.

What is wrong?

Here is paste of SafeMath included:

https://pastebin.com/C4FVyBFa

Version used:

pragma solidity ^0.4.24;

4
  • SafeMath isn't that big. Can we see the whole contract so we can replicate this? Commented Nov 14, 2018 at 4:07
  • This function literally doesn't depend on anything else.
    – tonino.j
    Commented Nov 14, 2018 at 4:09
  • Other functions don't have same issue, and classic addition fixes it.
    – tonino.j
    Commented Nov 14, 2018 at 4:10
  • edited question
    – tonino.j
    Commented Nov 14, 2018 at 4:18

1 Answer 1

0

I meant we really need to see the whole contract that is causing the issue so we might spot something.

Here's a slightly modified version of your function (so it's callable from outside) which is working as expected in a super basic shell of a contract.

// File: zeppelin-solidity/contracts/math/SafeMath.sol

/**
 * @title SafeMath
 * @dev Math operations with safety checks that throw on error
 */
library SafeMath {

  /**
  * @dev Multiplies two numbers, throws on overflow.
  */
  function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c) {
    // Gas optimization: this is cheaper than asserting 'a' not being zero, but the
    // benefit is lost if 'b' is also tested.
    // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
    if (_a == 0) {
      return 0;
    }

    c = _a * _b;
    assert(c / _a == _b);
    return c;
  }

  /**
  * @dev Integer division of two numbers, truncating the quotient.
  */
  function div(uint256 _a, uint256 _b) internal pure returns (uint256) {
    // assert(_b > 0); // Solidity automatically throws when dividing by 0
    // uint256 c = _a / _b;
    // assert(_a == _b * c + _a % _b); // There is no case in which this doesn't hold
    return _a / _b;
  }

  /**
  * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
  */
  function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
    assert(_b <= _a);
    return _a - _b;
  }

  /**
  * @dev Adds two numbers, throws on overflow.
  */
  function add(uint256 _a, uint256 _b) internal pure returns (uint256 c) {
    c = _a + _b;
    assert(c >= _a);
    return c;
  }
}

contract Try {

  using SafeMath for uint256;

  function someFunction() public view returns (bool) {
    uint256 a = 23;
    uint256 b = 48;
    return (a.add(1) > b);
  }

}

It returns false, as expected.

enter image description here

We need to see something that compiles but doesn't work as intended.

Hope it helps.

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.