From f563e76fdb4d4765ae07e772bb2ecf4759c98952 Mon Sep 17 00:00:00 2001 From: elch-yan Date: Sat, 20 Jul 2019 15:16:44 +0000 Subject: [PATCH] Imporvement: functions in SafeMath contract overloaded to accept custom error messages. --- contracts/math/SafeMath.sol | 79 ++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 5 deletions(-) diff --git a/contracts/math/SafeMath.sol b/contracts/math/SafeMath.sol index 932733e91d9..88a7ef98c82 100644 --- a/contracts/math/SafeMath.sol +++ b/contracts/math/SafeMath.sol @@ -24,8 +24,21 @@ library SafeMath { * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { + return add(a, b, "SafeMath: addition overflow"); + } + + /** + * @dev Returns the addition of two unsigned integers, reverting with custom message on + * overflow. + * + * Counterpart to Solidity's `+` operator. + * + * Requirements: + * - Addition cannot overflow. + */ + function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { uint256 c = a + b; - require(c >= a, "SafeMath: addition overflow"); + require(c >= a, errorMessage); return c; } @@ -40,7 +53,20 @@ library SafeMath { * - Subtraction cannot overflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { - require(b <= a, "SafeMath: subtraction overflow"); + return sub(a, b, "SafeMath: subtraction overflow"); + } + + /** + * @dev Returns the subtraction of two unsigned integers, reverting with custom message on + * overflow (when the result is negative). + * + * Counterpart to Solidity's `-` operator. + * + * Requirements: + * - Subtraction cannot overflow. + */ + function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { + require(b <= a, errorMessage); uint256 c = a - b; return c; @@ -56,6 +82,19 @@ library SafeMath { * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { + return mul(a, b, "SafeMath: multiplication overflow"); + } + + /** + * @dev Returns the multiplication of two unsigned integers, reverting with custom message on + * overflow. + * + * Counterpart to Solidity's `*` operator. + * + * Requirements: + * - Multiplication cannot overflow. + */ + function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 @@ -64,7 +103,7 @@ library SafeMath { } uint256 c = a * b; - require(c / a == b, "SafeMath: multiplication overflow"); + require(c / a == b, errorMessage); return c; } @@ -81,8 +120,23 @@ library SafeMath { * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { + return div(a, b, "SafeMath: division by zero"); + } + + /** + * @dev Returns the integer division of two unsigned integers. Reverts with custom message on + * division by zero. The result is rounded towards zero. + * + * Counterpart to Solidity's `/` operator. Note: this function uses a + * `revert` opcode (which leaves remaining gas untouched) while Solidity + * uses an invalid opcode to revert (consuming all remaining gas). + * + * Requirements: + * - The divisor cannot be zero. + */ + function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 - require(b > 0, "SafeMath: division by zero"); + require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold @@ -101,7 +155,22 @@ library SafeMath { * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { - require(b != 0, "SafeMath: modulo by zero"); + return mod(a, b, "SafeMath: modulo by zero"); + } + + /** + * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), + * Reverts with custom message when dividing by zero. + * + * Counterpart to Solidity's `%` operator. This function uses a `revert` + * opcode (which leaves remaining gas untouched) while Solidity uses an + * invalid opcode to revert (consuming all remaining gas). + * + * Requirements: + * - The divisor cannot be zero. + */ + function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { + require(b != 0, errorMessage); return a % b; } }