Skip to content

Commit 063d481

Browse files
committed
Fix parsing fixed point values.
1 parent 948665f commit 063d481

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

test/libsolidity/util/BytesUtils.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,21 @@ bytes BytesUtils::convertNumber(string const& _literal)
9494
bytes BytesUtils::convertFixedPoint(string const& _literal, size_t& o_fractionalDigits)
9595
{
9696
size_t dotPos = _literal.find('.');
97-
string valueInteger = _literal.substr(0, dotPos);
98-
string valueFraction = _literal.substr(dotPos + 1);
99-
o_fractionalDigits = valueFraction.length();
97+
o_fractionalDigits = dotPos < _literal.size() ? _literal.size() - dotPos : 0;
98+
bool negative = !_literal.empty() && _literal.at(0) == '-';
99+
// remove decimal point
100+
string valueInteger = _literal.substr(0, dotPos) + _literal.substr(dotPos + 1);
101+
// erase leading zeros to avoid parsing as octal.
102+
while (!valueInteger.empty() && (valueInteger.at(0) == '0' || valueInteger.at(0) == '-'))
103+
valueInteger.erase(valueInteger.begin());
104+
if (valueInteger.empty())
105+
valueInteger = "0";
100106
try
101107
{
102-
return util::toBigEndian(u256(valueInteger + valueFraction));
108+
u256 value(valueInteger);
109+
if (negative)
110+
value = s2u(-u2s(value));
111+
return util::toBigEndian(value);
103112
}
104113
catch (std::exception const&)
105114
{

0 commit comments

Comments
 (0)