diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e7e49e..d44f483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ # CHANGELOG **v0.4.2:** -- Fix u64 overflow +- Fix u64 overflow panic [[PR #5](https://github.com/sqids/sqids-rust/pull/7)] +- Cargo update +- `cargo deny` update **v0.4.1:** - Derive `Clone` trait [[PR #6](https://github.com/sqids/sqids-rust/pull/6)] diff --git a/Cargo.toml b/Cargo.toml index b469efd..d1c7460 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ readme = "README.md" keywords = ["ids", "encode", "short", "sqids", "hashids"] [dependencies] -derive_builder = "0.20.0" -serde = "1.0.197" -serde_json = "1.0.114" -thiserror = "1.0.57" +derive_builder = "0.20.2" +serde = "1.0.216" +serde_json = "1.0.133" +thiserror = "2.0.8" diff --git a/deny.toml b/deny.toml index 6427659..f08bd99 100644 --- a/deny.toml +++ b/deny.toml @@ -1,36 +1,27 @@ -targets = [ - { triple = "x86_64-unknown-linux-gnu" }, - { triple = "aarch64-unknown-linux-gnu" }, - { triple = "x86_64-unknown-linux-musl" }, - { triple = "aarch64-apple-darwin" }, - { triple = "x86_64-apple-darwin" }, - { triple = "x86_64-pc-windows-msvc" }, -] +[graph] +all-features = true [advisories] -vulnerability = "deny" -unmaintained = "warn" -unsound = "deny" -yanked = "deny" -notice = "warn" +version = 2 ignore = [] [licenses] -unlicensed = "deny" -allow-osi-fsf-free = "either" -copyleft = "deny" -default = "warn" -unused-allowed-license = "deny" -confidence-threshold = 0.95 -allow = [] -exceptions = [] +version = 2 +allow = [ + "MIT", + "Apache-2.0", + "Unicode-3.0" +] -[sources] -unknown-registry = "deny" -unknown-git = "deny" +exceptions = [] [bans] multiple-versions = "allow" wildcards = "deny" -highlight = "simplest-path" -skip-tree = [] +deny = [] + +[sources] +unknown-registry = "deny" +unknown-git = "deny" +allow-registry = ["https://github.com/rust-lang/crates.io-index"] +allow-git = [] \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index f16f0bd..8dac92c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -335,18 +335,24 @@ impl Sqids { } fn to_number(&self, id: &str, alphabet: &[char]) -> Option { - let mut result = 0; + let mut result: u64 = 0; + let base = alphabet.len() as u64; for c in id.chars() { - let idx = alphabet.iter().position(|&x| x == c).unwrap(); - result = result * alphabet.len() as u128 + idx as u128; - } + let idx = alphabet.iter().position(|&x| x == c).unwrap() as u64; - if result <= u64::MAX.into() { - Some(result.try_into().unwrap()) - } else { - None + if let Some(new_result) = result.checked_mul(base) { + if let Some(final_result) = new_result.checked_add(idx) { + result = final_result; + } else { + return None; + } + } else { + return None; + } } + + Some(result) } fn shuffle(alphabet: &[char]) -> Vec { diff --git a/tests/decoding.rs b/tests/decoding.rs deleted file mode 100644 index ce3d1b7..0000000 --- a/tests/decoding.rs +++ /dev/null @@ -1,15 +0,0 @@ -use sqids::*; - -#[test] -fn decode_number_maximum_value() { - let sqids = Sqids::default(); - let numbers = sqids.decode("ABARpJzdz9"); - assert_eq!(numbers, [9_007_199_254_740_991]); // 2 ^ 53 -} - -#[test] -fn decode_number_overflows() { - let sqids = Sqids::default(); - let numbers = sqids.decode("0J4AEXRN106Z0"); - assert_eq!(numbers, Vec::::new()); -} diff --git a/tests/encoding.rs b/tests/encoding.rs index d6d70d5..9aace8e 100644 --- a/tests/encoding.rs +++ b/tests/encoding.rs @@ -118,3 +118,17 @@ fn decoding_invalid_character() { let numbers: Vec = vec![]; assert_eq!(sqids.decode("*"), numbers); } + +#[test] +fn decoding_number_maximum_value() { + let sqids = Sqids::default(); + let numbers = sqids.decode("ABARpJzdz9"); + assert_eq!(numbers, [9_007_199_254_740_991]); // 2 ^ 53 +} + +#[test] +fn decoding_number_overflows() { + let sqids = Sqids::default(); + let numbers = sqids.decode("0J4AEXRN106Z0"); // `https://github.com/sqids/sqids-rust/pull/7` + assert_eq!(numbers, Vec::::new()); +}