From edf533f77df43b16a7b92f17af7a8930db01f293 Mon Sep 17 00:00:00 2001 From: Brendan Asselstine Date: Tue, 18 Jun 2019 09:56:39 -0700 Subject: [PATCH] Added suggested changes from Quantstamp audit feedback. - Using `require(...)` for testing input parameters and `assert(...)` for validating invariants - Error on L71 "Maximum" needed to be changed to "Minimum" --- contracts/ExponentLib.sol | 2 +- contracts/FixidityLib.sol | 24 ++++++++++++------------ contracts/LogarithmLib.sol | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/contracts/ExponentLib.sol b/contracts/ExponentLib.sol index 4930689..427e55f 100644 --- a/contracts/ExponentLib.sol +++ b/contracts/ExponentLib.sol @@ -18,7 +18,7 @@ library ExponentLib { pure returns (int256) { - assert(_x < 172 * FixidityLib.fixed1()); + require(_x < 172 * FixidityLib.fixed1()); int256 x = _x; int256 r = FixidityLib.fixed1(); while (x >= 10 * FixidityLib.fixed1()) { diff --git a/contracts/FixidityLib.sol b/contracts/FixidityLib.sol index fbc7a36..2a44c1a 100644 --- a/contracts/FixidityLib.sol +++ b/contracts/FixidityLib.sol @@ -68,7 +68,7 @@ library FixidityLib { } /** - * @notice Maximum value that can be converted to fixed point. Optimize for + * @notice Minimum value that can be converted to fixed point. Optimize for * deployment. * @dev Test minNewFixed() equals -(maxInt256()) / fixed1() * Hardcoded to 24 digits. @@ -151,8 +151,8 @@ library FixidityLib { pure returns (int256) { - assert(x <= maxNewFixed()); - assert(x >= minNewFixed()); + require(x <= maxNewFixed()); + require(x >= minNewFixed()); return x * fixed1(); } @@ -199,7 +199,7 @@ library FixidityLib { pure returns (int256) { - assert(_originDigits <= 38 && _destinationDigits <= 38); + require(_originDigits <= 38 && _destinationDigits <= 38); uint8 decimalDifference; if ( _originDigits > _destinationDigits ){ @@ -214,8 +214,8 @@ library FixidityLib { // decimalDifference = abs(_destinationDigits - _originDigits) // decimalDifference < 38 // 10**38 < 2**128-1 - assert(x <= maxInt256()/uint128(10)**uint128(decimalDifference)); - assert(x >= minInt256()/uint128(10)**uint128(decimalDifference)); + require(x <= maxInt256()/uint128(10)**uint128(decimalDifference)); + require(x >= minInt256()/uint128(10)**uint128(decimalDifference)); return x*(uint128(10)**uint128(decimalDifference)); } // _originDigits == digits()) @@ -271,9 +271,9 @@ library FixidityLib { pure returns (int256) { - assert(numerator <= maxNewFixed()); - assert(denominator <= maxNewFixed()); - assert(denominator != 0); + require(numerator <= maxNewFixed()); + require(denominator <= maxNewFixed()); + require(denominator != 0); int256 convertedNumerator = newFixed(numerator); int256 convertedDenominator = newFixed(denominator); return divide(convertedNumerator, convertedDenominator); @@ -420,7 +420,7 @@ library FixidityLib { * Test reciprocal(2*fixed1()*fixed1()) returns 0 // Testing how the fractional is truncated */ function reciprocal(int256 x) public pure returns (int256) { - assert(x != 0); + require(x != 0); return (fixed1()*fixed1()) / x; // Can't overflow } @@ -436,8 +436,8 @@ library FixidityLib { */ function divide(int256 x, int256 y) public pure returns (int256) { if (y == fixed1()) return x; - assert(y != 0); - assert(y <= maxFixedDivisor()); + require(y != 0); + require(y <= maxFixedDivisor()); return multiply(x, reciprocal(y)); } } diff --git a/contracts/LogarithmLib.sol b/contracts/LogarithmLib.sol index 8895185..0e15b55 100644 --- a/contracts/LogarithmLib.sol +++ b/contracts/LogarithmLib.sol @@ -49,7 +49,7 @@ library LogarithmLib { * Test ln(1) returns -82 */ function ln(int256 value) public pure returns (int256) { - assert(value >= 0); + require(value >= 0); int256 v = value; int256 r = 0; while (v <= FixidityLib.fixed1() / 10) {