From afd390aee313bf96e0ec1dc09ce6eb7118610641 Mon Sep 17 00:00:00 2001 From: tomasavola Date: Fri, 10 May 2024 11:06:37 -0300 Subject: [PATCH] Delete expect warning --- .../incorrect-exponentiation/Cargo.toml | 18 +++++++++++ .../remediated-example/Cargo.toml | 6 +--- .../remediated-example/src/lib.rs | 32 +++++++++++-------- .../vulnerable-example/src/lib.rs | 29 ++++++++++------- 4 files changed, 55 insertions(+), 30 deletions(-) create mode 100644 test-cases/incorrect-exponentiation/Cargo.toml diff --git a/test-cases/incorrect-exponentiation/Cargo.toml b/test-cases/incorrect-exponentiation/Cargo.toml new file mode 100644 index 00000000..24be4479 --- /dev/null +++ b/test-cases/incorrect-exponentiation/Cargo.toml @@ -0,0 +1,18 @@ +[workspace] +exclude = [".cargo", "target"] +members = ["incorrect-exponentiation-*/*"] +resolver = "2" +[workspace.dependencies] +soroban-sdk = { version = "20.3.2" } +[profile.release] +codegen-units = 1 +debug = 0 +debug-assertions = false +lto = true +opt-level = "z" +overflow-checks = true +panic = "abort" +strip = "symbols" +[profile.release-with-logs] +debug-assertions = true +inherits = "release" diff --git a/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/remediated-example/Cargo.toml b/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/remediated-example/Cargo.toml index ee9f0b0a..1c7d3486 100644 --- a/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/remediated-example/Cargo.toml +++ b/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/remediated-example/Cargo.toml @@ -1,16 +1,12 @@ [package] name = "incorrect-exponentiation-remediated-1" -version = "0.0.0" +version = "0.1.0" edition = "2021" - [lib] crate-type = ["cdylib"] - [dependencies] soroban-sdk = { workspace = true } - [dev-dependencies] soroban-sdk = { workspace = true, features = ["testutils"] } - [features] testutils = ["soroban-sdk/testutils"] diff --git a/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/remediated-example/src/lib.rs b/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/remediated-example/src/lib.rs index 53d48a5b..323aad7c 100644 --- a/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/remediated-example/src/lib.rs +++ b/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/remediated-example/src/lib.rs @@ -1,6 +1,6 @@ #![no_std] -use soroban_sdk::{contract, contractimpl, contracttype, Env}; +use soroban_sdk::{contract, contractimpl,contracterror, contracttype, Env}; #[contracttype] #[derive(Clone)] @@ -8,27 +8,33 @@ enum DataKey { Data, } +// Agrega el atributo #[contracterror] a la definiciĆ³n de IEError +#[contracterror] +#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] +#[repr(u32)] +pub enum IEError { + CouldntRetrieveData = 1, +} + #[contract] pub struct IncorrectExponentiation; #[contractimpl] impl IncorrectExponentiation { - pub fn set_data(e: Env, new_data: u128) { e.storage() .instance() .set::(&DataKey::Data, &new_data); } - pub fn exp_data_3(e: Env) -> u128 { - let data = e.storage() - .instance() - .get::(&DataKey::Data) - .expect("Data not found"); - - data.pow(3) + pub fn exp_data_3(e: Env) -> Result { + let data:Option = e.storage().instance().get(&DataKey::Data); + match data + { + Some(x) => return Ok(x.pow(3)), + None =>return Err(IEError::CouldntRetrieveData), + } } - } #[cfg(test)] @@ -47,10 +53,8 @@ mod tests { - client.set_data(&10_u128); + client.set_data(&3_u128); - assert_eq!(client.exp_data_3(), 1000); + assert_eq!(client.exp_data_3(), 27); } } - - diff --git a/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/vulnerable-example/src/lib.rs b/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/vulnerable-example/src/lib.rs index c5d98c8c..dbe8ee79 100644 --- a/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/vulnerable-example/src/lib.rs +++ b/test-cases/incorrect-exponentiation/incorrect-exponentiation-1/vulnerable-example/src/lib.rs @@ -1,6 +1,6 @@ #![no_std] -use soroban_sdk::{contract, contractimpl, contracttype, Env}; +use soroban_sdk::{contract, contractimpl,contracterror, contracttype, Env}; #[contracttype] #[derive(Clone)] @@ -8,27 +8,34 @@ enum DataKey { Data, } +// Agrega el atributo #[contracterror] a la definiciĆ³n de IEError +#[contracterror] +#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] +#[repr(u32)] +pub enum IEError { + CouldntRetrieveData = 1, +} + #[contract] pub struct IncorrectExponentiation; #[contractimpl] impl IncorrectExponentiation { - pub fn set_data(e: Env, new_data: u128) { e.storage() .instance() .set::(&DataKey::Data, &new_data); } - - pub fn exp_data_3(e: Env) -> u128 { - let mut data = e.storage() - .instance() - .get::(&DataKey::Data) - .expect("Data not found"); - - data ^= 3; - data + pub fn exp_data_3(e: Env) -> Result { + let data: Option = e.storage().instance().get(&DataKey::Data); + match data { + Some(mut x) => { + x ^= 3; + Ok(x) + }, + None => Err(IEError::CouldntRetrieveData), + } } }