From daa69df447e167fe75d57a5bbbabee4d637218a4 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Mon, 9 Apr 2018 15:22:45 +0200 Subject: Error on invalid arithmetic with constant expressions. --- .../syntaxTests/constants/addmod_mulmod_rational.sol | 7 +++++++ test/libsolidity/syntaxTests/constants/addmod_zero.sol | 11 +++++++++++ test/libsolidity/syntaxTests/constants/division_by_zero.sol | 9 +++++++++ test/libsolidity/syntaxTests/constants/mod_div_rational.sol | 6 ++++++ test/libsolidity/syntaxTests/constants/mod_zero.sol | 9 +++++++++ test/libsolidity/syntaxTests/constants/mulmod_zero.sol | 11 +++++++++++ test/libsolidity/syntaxTests/signed_rational_modulus.sol | 8 ++++++++ 7 files changed, 61 insertions(+) create mode 100644 test/libsolidity/syntaxTests/constants/addmod_mulmod_rational.sol create mode 100644 test/libsolidity/syntaxTests/constants/addmod_zero.sol create mode 100644 test/libsolidity/syntaxTests/constants/division_by_zero.sol create mode 100644 test/libsolidity/syntaxTests/constants/mod_div_rational.sol create mode 100644 test/libsolidity/syntaxTests/constants/mod_zero.sol create mode 100644 test/libsolidity/syntaxTests/constants/mulmod_zero.sol create mode 100644 test/libsolidity/syntaxTests/signed_rational_modulus.sol (limited to 'test/libsolidity/syntaxTests') diff --git a/test/libsolidity/syntaxTests/constants/addmod_mulmod_rational.sol b/test/libsolidity/syntaxTests/constants/addmod_mulmod_rational.sol new file mode 100644 index 00000000..26712735 --- /dev/null +++ b/test/libsolidity/syntaxTests/constants/addmod_mulmod_rational.sol @@ -0,0 +1,7 @@ +contract C { + uint constant a = addmod(3, 4, 0.1); + uint constant b = mulmod(3, 4, 0.1); +} +// ---- +// TypeError: (48-51): Invalid type for argument in function call. Invalid implicit conversion from rational_const 1 / 10 to uint256 requested. +// TypeError: (89-92): Invalid type for argument in function call. Invalid implicit conversion from rational_const 1 / 10 to uint256 requested. diff --git a/test/libsolidity/syntaxTests/constants/addmod_zero.sol b/test/libsolidity/syntaxTests/constants/addmod_zero.sol new file mode 100644 index 00000000..18f7d64a --- /dev/null +++ b/test/libsolidity/syntaxTests/constants/addmod_zero.sol @@ -0,0 +1,11 @@ +contract c { + uint constant a1 = 0; + uint constant a2 = 1; + uint constant b1 = addmod(3, 4, 0); + uint constant b2 = addmod(3, 4, a1); + uint constant b3 = addmod(3, 4, a2 - 1); +} +// ---- +// TypeError: (88-103): Arithmetic modulo zero. +// TypeError: (128-144): Arithmetic modulo zero. +// TypeError: (169-189): Arithmetic modulo zero. diff --git a/test/libsolidity/syntaxTests/constants/division_by_zero.sol b/test/libsolidity/syntaxTests/constants/division_by_zero.sol new file mode 100644 index 00000000..bf6000ec --- /dev/null +++ b/test/libsolidity/syntaxTests/constants/division_by_zero.sol @@ -0,0 +1,9 @@ +contract c { + uint constant a1 = 0; + uint constant a2 = 1; + uint constant b1 = 7 / a1; + uint constant b2 = 7 / (a2 - 1); +} +// ---- +// TypeError: (88-94): Division by zero. +// TypeError: (119-131): Division by zero. diff --git a/test/libsolidity/syntaxTests/constants/mod_div_rational.sol b/test/libsolidity/syntaxTests/constants/mod_div_rational.sol new file mode 100644 index 00000000..f8b6ce31 --- /dev/null +++ b/test/libsolidity/syntaxTests/constants/mod_div_rational.sol @@ -0,0 +1,6 @@ +contract C { + fixed a1 = 0.1 % -0.4271087646484375; + fixed a2 = 0.1 % 0.4271087646484375; + fixed a3 = 0 / 0.123; + fixed a4 = 0 / -0.123; +} diff --git a/test/libsolidity/syntaxTests/constants/mod_zero.sol b/test/libsolidity/syntaxTests/constants/mod_zero.sol new file mode 100644 index 00000000..f5e4a23a --- /dev/null +++ b/test/libsolidity/syntaxTests/constants/mod_zero.sol @@ -0,0 +1,9 @@ +contract c { + uint constant a1 = 0; + uint constant a2 = 1; + uint constant b1 = 3 % a1; + uint constant b2 = 3 % (a2 - 1); +} +// ---- +// TypeError: (88-94): Modulo zero. +// TypeError: (119-131): Modulo zero. diff --git a/test/libsolidity/syntaxTests/constants/mulmod_zero.sol b/test/libsolidity/syntaxTests/constants/mulmod_zero.sol new file mode 100644 index 00000000..856d01eb --- /dev/null +++ b/test/libsolidity/syntaxTests/constants/mulmod_zero.sol @@ -0,0 +1,11 @@ +contract c { + uint constant a1 = 0; + uint constant a2 = 1; + uint constant b1 = mulmod(3, 4, 0); + uint constant b2 = mulmod(3, 4, a1); + uint constant b3 = mulmod(3, 4, a2 - 1); +} +// ---- +// TypeError: (88-103): Arithmetic modulo zero. +// TypeError: (128-144): Arithmetic modulo zero. +// TypeError: (169-189): Arithmetic modulo zero. diff --git a/test/libsolidity/syntaxTests/signed_rational_modulus.sol b/test/libsolidity/syntaxTests/signed_rational_modulus.sol new file mode 100644 index 00000000..b37d33d0 --- /dev/null +++ b/test/libsolidity/syntaxTests/signed_rational_modulus.sol @@ -0,0 +1,8 @@ +contract test { + function f() public pure { + fixed a = 0.42578125 % -0.4271087646484375; + fixed b = .5 % a; + fixed c = a % b; + a; b; c; + } +} -- cgit From 932915633bb7f5985023f80a8c704cf8c5f979fa Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Wed, 11 Apr 2018 23:20:40 +0200 Subject: Add tests for literal operations as well. --- test/libsolidity/syntaxTests/literalOperations/division_by_zero.sol | 5 +++++ .../syntaxTests/literalOperations/division_by_zero_complex.sol | 5 +++++ test/libsolidity/syntaxTests/literalOperations/mod_zero.sol | 5 +++++ test/libsolidity/syntaxTests/literalOperations/mod_zero_complex.sol | 5 +++++ 4 files changed, 20 insertions(+) create mode 100644 test/libsolidity/syntaxTests/literalOperations/division_by_zero.sol create mode 100644 test/libsolidity/syntaxTests/literalOperations/division_by_zero_complex.sol create mode 100644 test/libsolidity/syntaxTests/literalOperations/mod_zero.sol create mode 100644 test/libsolidity/syntaxTests/literalOperations/mod_zero_complex.sol (limited to 'test/libsolidity/syntaxTests') diff --git a/test/libsolidity/syntaxTests/literalOperations/division_by_zero.sol b/test/libsolidity/syntaxTests/literalOperations/division_by_zero.sol new file mode 100644 index 00000000..b52b4c51 --- /dev/null +++ b/test/libsolidity/syntaxTests/literalOperations/division_by_zero.sol @@ -0,0 +1,5 @@ +contract C { + uint constant a = 1 / 0; +} +// ---- +// TypeError: (35-40): Operator / not compatible with types int_const 1 and int_const 0 diff --git a/test/libsolidity/syntaxTests/literalOperations/division_by_zero_complex.sol b/test/libsolidity/syntaxTests/literalOperations/division_by_zero_complex.sol new file mode 100644 index 00000000..8cc3b6f2 --- /dev/null +++ b/test/libsolidity/syntaxTests/literalOperations/division_by_zero_complex.sol @@ -0,0 +1,5 @@ +contract C { + uint constant a = 1 / ((1+3)-4); +} +// ---- +// TypeError: (35-48): Operator / not compatible with types int_const 1 and int_const 0 diff --git a/test/libsolidity/syntaxTests/literalOperations/mod_zero.sol b/test/libsolidity/syntaxTests/literalOperations/mod_zero.sol new file mode 100644 index 00000000..1bbbc3fc --- /dev/null +++ b/test/libsolidity/syntaxTests/literalOperations/mod_zero.sol @@ -0,0 +1,5 @@ +contract C { + uint constant b3 = 1 % 0; +} +// ---- +// TypeError: (36-41): Operator % not compatible with types int_const 1 and int_const 0 diff --git a/test/libsolidity/syntaxTests/literalOperations/mod_zero_complex.sol b/test/libsolidity/syntaxTests/literalOperations/mod_zero_complex.sol new file mode 100644 index 00000000..4899cac3 --- /dev/null +++ b/test/libsolidity/syntaxTests/literalOperations/mod_zero_complex.sol @@ -0,0 +1,5 @@ +contract C { + uint constant b3 = 1 % (-4+((2)*2)); +} +// ---- +// TypeError: (36-52): Operator % not compatible with types int_const 1 and int_const 0 -- cgit From d42476e241489447e5dc4f5b1fafc8000e635fbc Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Thu, 12 Apr 2018 10:39:13 +0200 Subject: Add test to check ConstantEvaluator for pure non-rational functions. --- test/libsolidity/syntaxTests/constants/pure_non_rational.sol | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 test/libsolidity/syntaxTests/constants/pure_non_rational.sol (limited to 'test/libsolidity/syntaxTests') diff --git a/test/libsolidity/syntaxTests/constants/pure_non_rational.sol b/test/libsolidity/syntaxTests/constants/pure_non_rational.sol new file mode 100644 index 00000000..4b96f1c7 --- /dev/null +++ b/test/libsolidity/syntaxTests/constants/pure_non_rational.sol @@ -0,0 +1,11 @@ +// Tests that the ConstantEvaluator does not crash for pure non-rational functions. +// Currently it does not evaluate such functions, but this may change in the future +// causing a division by zero error for a. +contract C { + uint constant a = 1 / (uint(keccak256([0])[0]) - uint(keccak256([0])[0])); + uint constant b = 1 / uint(keccak256([0])); + uint constant c = uint(keccak256([0])); + uint[c] mem; +} +// ---- +// TypeError: (392-393): Invalid array length, expected integer literal or constant expression. -- cgit