Optimize safe casting of signed numbers (#3565)

This commit is contained in:
Igor Żuk
2022-07-20 17:55:05 +02:00
committed by GitHub
parent d3ff81b37f
commit 580b7ab816
3 changed files with 97 additions and 96 deletions

View File

@ -114,9 +114,9 @@ const toIntDownCast = length => `\
*
* _Available since v${version('toInt(int)', length)}._
*/
function toInt${length}(int256 value) internal pure returns (int${length}) {
require(value >= type(int${length}).min && value <= type(int${length}).max, "SafeCast: value doesn't fit in ${length} bits");
return int${length}(value);
function toInt${length}(int256 value) internal pure returns (int${length} downcasted) {
downcasted = int${length}(value);
require(downcasted == value, "SafeCast: value doesn't fit in ${length} bits");
}
`;
/* eslint-enable max-len */