From cbc2acf83ea882c70ac99d34107013f89b437886 Mon Sep 17 00:00:00 2001 From: Alexander Arlt Date: Wed, 4 Aug 2021 14:32:37 -0500 Subject: [PATCH] Type Checker: Add size check for fixed point types. --- libsolidity/analysis/TypeChecker.cpp | 14 ++++++++++++++ .../syntaxTests/fixed_point/type_size.sol | 8 ++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/libsolidity/syntaxTests/fixed_point/type_size.sol diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 1ca8dd193..76d184fc1 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -608,6 +608,20 @@ bool TypeChecker::visit(VariableDeclaration const& _variable) } } + auto fixedPoint = dynamic_cast(_variable.annotation().type); + if (fixedPoint) + { + bigint fixedPointTypeSize = boost::multiprecision::pow(bigint(2), fixedPoint->numBits()); + bigint digitSize = boost::multiprecision::pow(bigint(10), fixedPoint->fractionalDigits()); + if (fixedPointTypeSize <= digitSize) + m_errorReporter.typeError( + 5108_error, + _variable.location(), + "Invalid fixed point type " + fixedPoint->toString(true) + ": 10^" + + std::to_string(fixedPoint->fractionalDigits()) + " does not fit in 2^" + + std::to_string(fixedPoint->numBits()) + " bits." + ); + } return false; } diff --git a/test/libsolidity/syntaxTests/fixed_point/type_size.sol b/test/libsolidity/syntaxTests/fixed_point/type_size.sol new file mode 100644 index 000000000..fa206c87f --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/type_size.sol @@ -0,0 +1,8 @@ +contract A +{ + function s128x18() public pure returns (ufixed16x16 x) { + x = 0; + } +} +// ---- +// TypeError 5108: (57-70): Invalid fixed point type ufixed16x16: 10^16 does not fit in 2^16 bits.