Skip to content

Commit

Permalink
Fixes vyperlang#767, overflow checking for multiplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacqueswww committed Apr 16, 2018
1 parent ae21ffa commit 0a94db2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions tests/parser/types/numbers/test_decimals.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,21 @@ def iarg() -> wei_value:
assert c.iarg() == 14

print('Passed fractional multiplication test')


def test_mul_overflow(t, assert_tx_failed, get_contract_with_gas_estimation, chain):
mul_code = """
@public
def _num_mul(x: decimal, y: int128) -> decimal:
return x * y
"""

c = get_contract_with_gas_estimation(mul_code)

t.s = chain
NUM_1 = 85070591730234615865843651857942052864.0
NUM_2 = 136112946768375385385349842973

assert_tx_failed(lambda: c._num_mul(NUM_1, NUM_2))
4 changes: 2 additions & 2 deletions vyper/parser/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ def arithmetic(self):
o = LLLnode.from_list(['with', 'r', right, ['with', 'l', left,
['with', 'ans', ['mul', 'l', 'r'],
['seq',
['assert', ['or', ['eq', ['sdiv', 'ans', 'l'], 'r'], ['not', 'l']]],
['assert', ['or', ['eq', ['sdiv', 'ans', 'l'], 'r'], ['iszero', 'l']]],
['sdiv', 'ans', DECIMAL_DIVISOR]]]]], typ=BaseType('decimal', new_unit), pos=getpos(self.expr))
elif (ltyp == 'int128' and rtyp == 'decimal') or (ltyp == 'decimal' and rtyp == 'int128'):
o = LLLnode.from_list(['with', 'r', right, ['with', 'l', left,
['with', 'ans', ['mul', 'l', 'r'],
['seq',
['assert', ['or', ['eq', ['sdiv', 'ans', 'l'], 'r'], ['not', 'l']]],
['assert', ['or', ['eq', ['sdiv', 'ans', 'l'], 'r'], ['iszero', 'l']]],
'ans']]]], typ=BaseType('decimal', new_unit), pos=getpos(self.expr))
else:
raise Exception("Unsupported Operation 'mul(%r, %r)'" % (ltyp, rtyp))
Expand Down

0 comments on commit 0a94db2

Please sign in to comment.