From 08700d99059ccfb742923adfd1acf02dbf62cd52 Mon Sep 17 00:00:00 2001 From: Martin Schenck Date: Fri, 26 Oct 2018 16:48:32 +0200 Subject: [PATCH 1/3] Updated SafeMath from the latest OpenZeppelin code https://github.com/OpenSTFoundation/mosaic-contracts/issues/378 Fixes #378 --- contracts/lib/SafeMath.sol | 98 +++++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/contracts/lib/SafeMath.sol b/contracts/lib/SafeMath.sol index 2b352f7d..a83133cc 100644 --- a/contracts/lib/SafeMath.sol +++ b/contracts/lib/SafeMath.sol @@ -1,6 +1,6 @@ -pragma solidity ^0.4.23; +pragma solidity ^0.4.24; -// Copyright 2017 OpenST Ltd. +// Copyright 2018 OpenST Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,90 +15,117 @@ pragma solidity ^0.4.23; // limitations under the License. // // ---------------------------------------------------------------------------- -// Common: SafeMath Library Implementation // // http://www.simpletoken.org/ // // Based on the SafeMath library by the OpenZeppelin team. -// Copyright (c) 2016 Smart Contract Solutions, Inc. +// Copyright (c) 2018 Smart Contract Solutions, Inc. // https://github.com/OpenZeppelin/zeppelin-solidity // The MIT License. // ---------------------------------------------------------------------------- /** - * @title SafeMath library. + * @title SafeMath library. * - * @notice Based on the SafeMath library by the OpenZeppelin team. + * @notice Based on the SafeMath library by the OpenZeppelin team. + * + * @dev Math operations with safety checks that revert on error. */ library SafeMath { - - /** Internal Functions */ + + /* Internal Functions */ /** - * @notice Internal pure function mul. + * @notice Multiplies two numbers, reverts on overflow. * - * @param a Unsigned integer multiplicand. - * @param b Unsigned integer multiplier. + * @param a Unsigned integer multiplicand. + * @param b Unsigned integer multiplier. * - * @return uint256 Product. + * @return uint256 Product. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { - uint256 c = a * b; + /* + * 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 + */ + if (a == 0) { + return 0; + } - assert(a == 0 || c / a == b); + uint256 c = a * b; + require( + c / a == b, + "Overflow when multiplying." + ); return c; } /** - * @notice Internal pure function div. + * @notice Integer division of two numbers truncating the quotient, reverts + * on division by zero. * - * @param a Unsigned integer dividend. - * @param b Unsigned integer divisor. + * @param a Unsigned integer dividend. + * @param b Unsigned integer divisor. * - * @return uint256 Quotient. + * @return uint256 Quotient. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { - // Solidity automatically throws when dividing by 0 + // Solidity only automatically asserts when dividing by 0. + require( + b > 0, + "Cannot do attempted division by less than or equal to zero." + ); uint256 c = a / b; - // assert(a == b * c + a % b); // There is no case in which this doesn't hold + // There is no case in which the following doesn't hold: + // assert(a == b * c + a % b); + return c; } /** - * @notice Internal pure function sub. + * @notice Subtracts two numbers, reverts on underflow (i.e. if subtrahend + * is greater than minuend). * - * @param a Unsigned integer minuend. - * @param b Unsigned integer subtrahend. + * @param a Unsigned integer minuend. + * @param b Unsigned integer subtrahend. * - * @return uint256 Difference. + * @return uint256 Difference. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { - assert(b <= a); + require( + b <= a, + "Underflow when subtracting." + ); + uint256 c = a - b; - return a - b; + return c; } /** - * @notice Internal pure function add. + * @notice Adds two numbers, reverts on overflow. * - * @param a Unsigned integer augend. - * @param b Unsigned integer addend. + * @param a Unsigned integer augend. + * @param b Unsigned integer addend. * - * @return uint256 Sum. + * @return uint256 Sum. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; - - assert(c >= a); + require( + c >= a, + "Overflow when adding." + ); return c; } /** - * @notice Divides two numbers and returns the remainder. + * @notice Divides two numbers and returns the remainder (unsigned integer + * modulo), reverts when dividing by zero. * * @param a Unsigned integer dividend. * @param b Unsigned integer divisor. @@ -106,7 +133,10 @@ library SafeMath { * @return uint256 Remainder. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { - assert(b != 0); + require( + b != 0, + "Cannot do attempted division by zero (in `mod()`)." + ); return a % b; } From f2c204b3f92e9424cbf6695c56aeda6ba30e08f8 Mon Sep 17 00:00:00 2001 From: Martin Schenck Date: Mon, 29 Oct 2018 11:59:49 +0100 Subject: [PATCH 2/3] Fixed indentation --- contracts/lib/SafeMath.sol | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/contracts/lib/SafeMath.sol b/contracts/lib/SafeMath.sol index a83133cc..5f93dbaf 100644 --- a/contracts/lib/SafeMath.sol +++ b/contracts/lib/SafeMath.sol @@ -124,14 +124,14 @@ library SafeMath { } /** - * @notice Divides two numbers and returns the remainder (unsigned integer - * modulo), reverts when dividing by zero. - * - * @param a Unsigned integer dividend. - * @param b Unsigned integer divisor. - * - * @return uint256 Remainder. - */ + * @notice Divides two numbers and returns the remainder (unsigned integer + * modulo), reverts when dividing by zero. + * + * @param a Unsigned integer dividend. + * @param b Unsigned integer divisor. + * + * @return uint256 Remainder. + */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { require( b != 0, From 08d50010aecf957e29a2242ce787954a638f9705 Mon Sep 17 00:00:00 2001 From: Martin Schenck Date: Mon, 29 Oct 2018 12:21:43 +0100 Subject: [PATCH 3/3] Reverted SafeMath pragma to 0.4.23 --- contracts/lib/SafeMath.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/lib/SafeMath.sol b/contracts/lib/SafeMath.sol index 5f93dbaf..530c4e25 100644 --- a/contracts/lib/SafeMath.sol +++ b/contracts/lib/SafeMath.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.4.24; +pragma solidity ^0.4.23; // Copyright 2018 OpenST Ltd. //