From 4d3cf0aeafbb67a743f39ddb9ee9cc8ce9a07972 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 26 Sep 2018 13:59:53 +0100 Subject: [PATCH] Add comprehensive tests for invalid array indexing --- .../syntaxTests/array/length/bytes32_too_large.sol | 5 +++++ .../array/length/bytes32_too_large_multidim.sol | 5 +++++ .../syntaxTests/array/length/parameter_too_large.sol | 5 +++++ .../array/length/parameter_too_large_multidim.sol | 3 +++ .../array/length/{too_large.sol => uint_too_large.sol} | 0 .../syntaxTests/array/length/uint_too_large_multidim.sol | 5 +++++ .../indexing/array_multidim_overflow_index.sol | 6 ++++++ .../syntaxTests/indexing/array_multidim_rational.sol | 6 ++++++ .../syntaxTests/indexing/array_negative_index.sol | 8 ++++++++ .../syntaxTests/indexing/array_nontinteger_index.sol | 8 ++++++++ .../syntaxTests/indexing/fixedbytes_negative_index.sol | 9 +++++++++ .../syntaxTests/indexing/fixedbytes_noninteger_index.sol | 6 ++++++ 12 files changed, 66 insertions(+) create mode 100644 test/libsolidity/syntaxTests/array/length/bytes32_too_large.sol create mode 100644 test/libsolidity/syntaxTests/array/length/bytes32_too_large_multidim.sol create mode 100644 test/libsolidity/syntaxTests/array/length/parameter_too_large.sol create mode 100644 test/libsolidity/syntaxTests/array/length/parameter_too_large_multidim.sol rename test/libsolidity/syntaxTests/array/length/{too_large.sol => uint_too_large.sol} (100%) create mode 100644 test/libsolidity/syntaxTests/array/length/uint_too_large_multidim.sol create mode 100644 test/libsolidity/syntaxTests/indexing/array_multidim_overflow_index.sol create mode 100644 test/libsolidity/syntaxTests/indexing/array_multidim_rational.sol create mode 100644 test/libsolidity/syntaxTests/indexing/array_negative_index.sol create mode 100644 test/libsolidity/syntaxTests/indexing/array_nontinteger_index.sol create mode 100644 test/libsolidity/syntaxTests/indexing/fixedbytes_negative_index.sol create mode 100644 test/libsolidity/syntaxTests/indexing/fixedbytes_noninteger_index.sol diff --git a/test/libsolidity/syntaxTests/array/length/bytes32_too_large.sol b/test/libsolidity/syntaxTests/array/length/bytes32_too_large.sol new file mode 100644 index 000000000000..1742c80dfa00 --- /dev/null +++ b/test/libsolidity/syntaxTests/array/length/bytes32_too_large.sol @@ -0,0 +1,5 @@ +contract C { + bytes32[8**90] ids; +} +// ---- +// TypeError: (25-30): Invalid array length, expected integer literal or constant expression. diff --git a/test/libsolidity/syntaxTests/array/length/bytes32_too_large_multidim.sol b/test/libsolidity/syntaxTests/array/length/bytes32_too_large_multidim.sol new file mode 100644 index 000000000000..1344574c3023 --- /dev/null +++ b/test/libsolidity/syntaxTests/array/length/bytes32_too_large_multidim.sol @@ -0,0 +1,5 @@ +contract C { + bytes32[8**90][500] ids; +} +// ---- +// TypeError: (25-30): Invalid array length, expected integer literal or constant expression. diff --git a/test/libsolidity/syntaxTests/array/length/parameter_too_large.sol b/test/libsolidity/syntaxTests/array/length/parameter_too_large.sol new file mode 100644 index 000000000000..02e0a7cc6dbc --- /dev/null +++ b/test/libsolidity/syntaxTests/array/length/parameter_too_large.sol @@ -0,0 +1,5 @@ +contract C { + function f(bytes32[1263941234127518272] memory) public pure {} +} +// ---- +// TypeError: (26-61): Array is too large to be encoded. diff --git a/test/libsolidity/syntaxTests/array/length/parameter_too_large_multidim.sol b/test/libsolidity/syntaxTests/array/length/parameter_too_large_multidim.sol new file mode 100644 index 000000000000..49b855775980 --- /dev/null +++ b/test/libsolidity/syntaxTests/array/length/parameter_too_large_multidim.sol @@ -0,0 +1,3 @@ +contract C { + function f(bytes32[1263941234127518272][500] memory) public pure {} +} diff --git a/test/libsolidity/syntaxTests/array/length/too_large.sol b/test/libsolidity/syntaxTests/array/length/uint_too_large.sol similarity index 100% rename from test/libsolidity/syntaxTests/array/length/too_large.sol rename to test/libsolidity/syntaxTests/array/length/uint_too_large.sol diff --git a/test/libsolidity/syntaxTests/array/length/uint_too_large_multidim.sol b/test/libsolidity/syntaxTests/array/length/uint_too_large_multidim.sol new file mode 100644 index 000000000000..901bc28afb6f --- /dev/null +++ b/test/libsolidity/syntaxTests/array/length/uint_too_large_multidim.sol @@ -0,0 +1,5 @@ +contract C { + uint[8**90][500] ids; +} +// ---- +// TypeError: (22-27): Invalid array length, expected integer literal or constant expression. diff --git a/test/libsolidity/syntaxTests/indexing/array_multidim_overflow_index.sol b/test/libsolidity/syntaxTests/indexing/array_multidim_overflow_index.sol new file mode 100644 index 000000000000..7ce7fb24f01c --- /dev/null +++ b/test/libsolidity/syntaxTests/indexing/array_multidim_overflow_index.sol @@ -0,0 +1,6 @@ +contract C { + function f() public { + bytes[32] memory a; + a[8**90][8**90][1 - 8**90]; + } +} diff --git a/test/libsolidity/syntaxTests/indexing/array_multidim_rational.sol b/test/libsolidity/syntaxTests/indexing/array_multidim_rational.sol new file mode 100644 index 000000000000..24310c290c02 --- /dev/null +++ b/test/libsolidity/syntaxTests/indexing/array_multidim_rational.sol @@ -0,0 +1,6 @@ +contract C { + function f() public { + bytes[32] memory a; + a[8**90][8**90][8**90*0.1]; + } +} diff --git a/test/libsolidity/syntaxTests/indexing/array_negative_index.sol b/test/libsolidity/syntaxTests/indexing/array_negative_index.sol new file mode 100644 index 000000000000..019d023b56c7 --- /dev/null +++ b/test/libsolidity/syntaxTests/indexing/array_negative_index.sol @@ -0,0 +1,8 @@ +contract C { + function f() public { + bytes[32] memory a; + a[-1]; + } +} +// ---- +// TypeError: (67-69): Type int_const -1 is not implicitly convertible to expected type uint256. diff --git a/test/libsolidity/syntaxTests/indexing/array_nontinteger_index.sol b/test/libsolidity/syntaxTests/indexing/array_nontinteger_index.sol new file mode 100644 index 000000000000..7c0ac9fe24c4 --- /dev/null +++ b/test/libsolidity/syntaxTests/indexing/array_nontinteger_index.sol @@ -0,0 +1,8 @@ +contract C { + function f() public { + bytes[32] memory a; + a[888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888]; + } +} +// ---- +// TypeError: (67-178): Type int_const 8888...(103 digits omitted)...8888 is not implicitly convertible to expected type uint256. diff --git a/test/libsolidity/syntaxTests/indexing/fixedbytes_negative_index.sol b/test/libsolidity/syntaxTests/indexing/fixedbytes_negative_index.sol new file mode 100644 index 000000000000..914feaed9a83 --- /dev/null +++ b/test/libsolidity/syntaxTests/indexing/fixedbytes_negative_index.sol @@ -0,0 +1,9 @@ +contract C { + function f() public { + bytes32 b; + b[-1]; + } +} +// ---- +// TypeError: (58-60): Type int_const -1 is not implicitly convertible to expected type uint256. +// TypeError: (56-61): Out of bounds array access. diff --git a/test/libsolidity/syntaxTests/indexing/fixedbytes_noninteger_index.sol b/test/libsolidity/syntaxTests/indexing/fixedbytes_noninteger_index.sol new file mode 100644 index 000000000000..0821c39db770 --- /dev/null +++ b/test/libsolidity/syntaxTests/indexing/fixedbytes_noninteger_index.sol @@ -0,0 +1,6 @@ +contract C { + function f() public { + bytes32 b; + b[888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888]; + } +}