File tree Expand file tree Collapse file tree 4 files changed +29
-5
lines changed Expand file tree Collapse file tree 4 files changed +29
-5
lines changed Original file line number Diff line number Diff line change 1
1
# CHANGELOG
2
2
3
+ ** v0.4.2:**
4
+ - Fix u64 overflow
5
+
3
6
** v0.4.1:**
4
7
- Derive ` Clone ` trait [[ PR #6 ] ( https://github.com/sqids/sqids-rust/pull/6 )]
5
8
- Cargo update
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ description = "Generate short unique ids from numbers."
4
4
repository = " https://github.com/sqids/sqids-rust"
5
5
documentation = " https://docs.rs/sqids"
6
6
homepage = " https://sqids.org/rust"
7
- version = " 0.4.1 "
7
+ version = " 0.4.2 "
8
8
license = " MIT"
9
9
edition = " 2021"
10
10
readme = " README.md"
Original file line number Diff line number Diff line change @@ -251,7 +251,9 @@ impl Sqids {
251
251
252
252
let alphabet_without_separator: Vec < char > =
253
253
alphabet. iter ( ) . copied ( ) . skip ( 1 ) . collect ( ) ;
254
- ret. push ( self . to_number ( chunks[ 0 ] , & alphabet_without_separator) ) ;
254
+ if let Some ( value) = self . to_number ( chunks[ 0 ] , & alphabet_without_separator) {
255
+ ret. push ( value)
256
+ }
255
257
256
258
if chunks. len ( ) > 1 {
257
259
alphabet = Self :: shuffle ( & alphabet) ;
@@ -332,15 +334,19 @@ impl Sqids {
332
334
id. into_iter ( ) . collect ( )
333
335
}
334
336
335
- fn to_number ( & self , id : & str , alphabet : & [ char ] ) -> u64 {
337
+ fn to_number ( & self , id : & str , alphabet : & [ char ] ) -> Option < u64 > {
336
338
let mut result = 0 ;
337
339
338
340
for c in id. chars ( ) {
339
341
let idx = alphabet. iter ( ) . position ( |& x| x == c) . unwrap ( ) ;
340
- result = result * alphabet. len ( ) as u64 + idx as u64 ;
342
+ result = result * alphabet. len ( ) as u128 + idx as u128 ;
341
343
}
342
344
343
- result
345
+ if result <= u64:: MAX . into ( ) {
346
+ Some ( result. try_into ( ) . unwrap ( ) )
347
+ } else {
348
+ None
349
+ }
344
350
}
345
351
346
352
fn shuffle ( alphabet : & [ char ] ) -> Vec < char > {
Original file line number Diff line number Diff line change
1
+ use sqids:: * ;
2
+
3
+ #[ test]
4
+ fn decode_number_maximum_value ( ) {
5
+ let sqids = Sqids :: default ( ) ;
6
+ let numbers = sqids. decode ( "ABARpJzdz9" ) ;
7
+ assert_eq ! ( numbers, [ 9_007_199_254_740_991 ] ) ; // 2 ^ 53
8
+ }
9
+
10
+ #[ test]
11
+ fn decode_number_overflows ( ) {
12
+ let sqids = Sqids :: default ( ) ;
13
+ let numbers = sqids. decode ( "0J4AEXRN106Z0" ) ;
14
+ assert_eq ! ( numbers, Vec :: <u64 >:: new( ) ) ;
15
+ }
You can’t perform that action at this time.
0 commit comments