mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[Sol->Yul] Implement index access for storage arrays
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
contract C {
|
||||
uint[] storageArray;
|
||||
function test_indicies(uint256 len) public
|
||||
{
|
||||
storageArray.length = len;
|
||||
|
||||
for (uint i = 0; i < len; i++)
|
||||
storageArray[i] = i + 1;
|
||||
|
||||
for (uint i = 0; i < len; i++)
|
||||
require(storageArray[i] == i + 1);
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// test_indicies(uint256): 1 ->
|
||||
// test_indicies(uint256): 129 ->
|
||||
// test_indicies(uint256): 5 ->
|
||||
// test_indicies(uint256): 10 ->
|
||||
// test_indicies(uint256): 15 ->
|
||||
// test_indicies(uint256): 0xFF ->
|
||||
// test_indicies(uint256): 1000 ->
|
||||
// test_indicies(uint256): 129 ->
|
||||
// test_indicies(uint256): 128 ->
|
||||
// test_indicies(uint256): 1 ->
|
||||
@@ -0,0 +1,21 @@
|
||||
contract C {
|
||||
uint[] storageArray;
|
||||
function test_boundery_check(uint256 len, uint256 access) public returns
|
||||
(uint256)
|
||||
{
|
||||
storageArray.length = len;
|
||||
return storageArray[access];
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// test_boundery_check(uint256, uint256): 10, 11 -> FAILURE
|
||||
// test_boundery_check(uint256, uint256): 10, 9 -> 0
|
||||
// test_boundery_check(uint256, uint256): 1, 9 -> FAILURE
|
||||
// test_boundery_check(uint256, uint256): 1, 1 -> FAILURE
|
||||
// test_boundery_check(uint256, uint256): 10, 10 -> FAILURE
|
||||
// test_boundery_check(uint256, uint256): 256, 256 -> FAILURE
|
||||
// test_boundery_check(uint256, uint256): 256, 255 -> 0
|
||||
// test_boundery_check(uint256, uint256): 256, 0xFFFF -> FAILURE
|
||||
// test_boundery_check(uint256, uint256): 256, 2 -> 0
|
||||
@@ -0,0 +1,51 @@
|
||||
contract C {
|
||||
uint[] storageArray;
|
||||
function test_zeroed_indicies(uint256 len) public
|
||||
{
|
||||
storageArray.length = len;
|
||||
|
||||
for (uint i = 0; i < len; i++)
|
||||
storageArray[i] = i + 1;
|
||||
|
||||
if (len > 3)
|
||||
{
|
||||
storageArray.length = 3;
|
||||
|
||||
for (uint i = 3; i < len; i++)
|
||||
{
|
||||
assembly {
|
||||
mstore(0, storageArray_slot)
|
||||
let pos := add(keccak256(0, 0x20), i)
|
||||
|
||||
if iszero(eq(sload(pos), 0)) {
|
||||
revert(0, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
storageArray.length = 0;
|
||||
storageArray.length = len;
|
||||
|
||||
for (uint i = 0; i < len; i++)
|
||||
{
|
||||
require(storageArray[i] == 0);
|
||||
|
||||
uint256 val = storageArray[i];
|
||||
uint256 check;
|
||||
|
||||
assembly { check := iszero(val) }
|
||||
|
||||
require(check == 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// test_zeroed_indicies(uint256): 1 ->
|
||||
// test_zeroed_indicies(uint256): 5 ->
|
||||
// test_zeroed_indicies(uint256): 10 ->
|
||||
// test_zeroed_indicies(uint256): 15 ->
|
||||
// test_zeroed_indicies(uint256): 0xFF ->
|
||||
Reference in New Issue
Block a user