Skip to content

Commit

Permalink
Arrays indexes must be dereferenced if needed (#1115)
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Steuernagel <[email protected]>

Signed-off-by: Lucas Steuernagel <[email protected]>
  • Loading branch information
LucasSte committed Jan 5, 2023
1 parent 15f36f2 commit 3ca41d8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/sema/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5342,10 +5342,8 @@ fn array_subscript(
));
}

if index_ty.is_contract_storage() {
// make sure we load the index value from storage
index = index.cast(&index.loc(), index_ty.deref_any(), true, ns, diagnostics)?;
}
// make sure we load the index value if needed
index = index.cast(&index.loc(), index_ty.deref_any(), true, ns, diagnostics)?;

match array_ty.deref_any() {
Type::Bytes(_) | Type::Array(..) | Type::DynamicBytes => {
Expand Down
85 changes: 85 additions & 0 deletions tests/solana_tests/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1882,3 +1882,88 @@ fn dynamic_array_push_pop_loop() {
runtime.constructor(&[]);
runtime.function("test", &[]);
}

#[test]
fn double_index() {
let src = r#"
contract RH {
function calc(uint256[] memory separators, int256[] memory params) public pure returns (int256[4] memory) {
int256 stopLimit = params[separators[4]];
int256 contractedValueRatio = params[separators[6]];
return [stopLimit, contractedValueRatio, 3, 4];
}
}
"#;

let mut vm = build_solidity(src);
vm.constructor(&[]);

let separators = BorshToken::Array(vec![
BorshToken::Int {
width: 256,
value: BigInt::from(25u8),
},
BorshToken::Int {
width: 256,
value: BigInt::from(25u8),
},
BorshToken::Int {
width: 256,
value: BigInt::from(25u8),
},
BorshToken::Int {
width: 256,
value: BigInt::from(25u8),
},
BorshToken::Int {
width: 256,
value: BigInt::from(1u8),
},
BorshToken::Int {
width: 256,
value: BigInt::from(25u8),
},
BorshToken::Int {
width: 256,
value: BigInt::from(0u8),
},
]);

let params = BorshToken::Array(vec![
BorshToken::Int {
width: 256,
value: BigInt::from(80u8),
},
BorshToken::Int {
width: 256,
value: BigInt::from(98u8),
},
]);

let returns = vm.function("calc", &[separators, params]).unwrap();

assert_eq!(
returns,
BorshToken::FixedArray(vec![
BorshToken::Int {
width: 256,
value: BigInt::from(98u8),
},
BorshToken::Int {
width: 256,
value: BigInt::from(80u8),
},
BorshToken::Int {
width: 256,
value: BigInt::from(3u8),
},
BorshToken::Int {
width: 256,
value: BigInt::from(4u8),
},
])
);
}

0 comments on commit 3ca41d8

Please sign in to comment.