Skip to content

Commit

Permalink
Correct test cases and detector documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nachogutman committed May 9, 2024
2 parents 10e8402 + a746740 commit 42807de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
6 changes: 3 additions & 3 deletions docs/docs/vulnerabilities/21-incorrect-exponentiation.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ In the following example, the `^` operand is being used for exponentiation. But
.get::<DataKey, u128>(&DataKey::Data)
.expect("Data not found");

data = data ^ 3;
return data;
data ^= 3;
data
}
```

Expand All @@ -39,7 +39,7 @@ A possible solution is to use the method `pow()`. But, if a XOR operation is wan
.get::<DataKey, u128>(&DataKey::Data)
.expect("Data not found");

return data.pow(3);
data.pow(3)
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ pub struct IncorrectExponentiation;

#[contractimpl]
impl IncorrectExponentiation {
pub fn init(e: Env) {
pub fn init(e: Env){
e.storage()
.instance()
.set::<DataKey, u128>(&DataKey::Data, &(255_u128.pow(2) - 1));
.set::<DataKey, u128>(&DataKey::Data, &((255_u128 ^ 2) - 1));
}

pub fn get_data(e: Env) -> Result<u128, IEError> {
let data = e.storage()
.instance()
Expand All @@ -35,6 +35,7 @@ impl IncorrectExponentiation {
None => return Err(IEError::CouldntRetrieveData)
}
}

}

#[cfg(test)]
Expand All @@ -45,13 +46,15 @@ mod tests {

#[test]
fn simple_test() {
let env = Env::default();
let contract_id = env.register_contract(None, IncorrectExponentiation);
let client = IncorrectExponentiationClient::new(&env, &contract_id);
env.mock_all_auths();
let _user = <Address as testutils::Address>::generate(&env);
let env = Env::default();
let contract_id = env.register_contract(None, IncorrectExponentiation);
let client = IncorrectExponentiationClient::new(&env, &contract_id);
env.mock_all_auths();
let _user = <Address as testutils::Address>::generate(&env);

client.init();
assert_eq!(client.get_data(), 65024);
}
}


Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![no_std]
#![allow(clippy::assign_op_pattern)]

use soroban_sdk::{contract, contractimpl, contracttype, contracterror, Env};

Expand Down Expand Up @@ -46,11 +45,11 @@ mod tests {

#[test]
fn simple_test() {
let env = Env::default();
let contract_id = env.register_contract(None, IncorrectExponentiation);
let client = IncorrectExponentiationClient::new(&env, &contract_id);
env.mock_all_auths();
let _user = <Address as testutils::Address>::generate(&env);
let env = Env::default();
let contract_id = env.register_contract(None, IncorrectExponentiation);
let client = IncorrectExponentiationClient::new(&env, &contract_id);
env.mock_all_auths();
let _user = <Address as testutils::Address>::generate(&env);

client.init();
assert_ne!(client.get_data(), 65024);
Expand Down

0 comments on commit 42807de

Please sign in to comment.