@@ -490,6 +490,8 @@ fn parse_bytes_only(s: &str) -> Result<u64, ParseError> {
490
490
/// 512. You can also use standard block size suffixes like `'k'` for
491
491
/// 1024.
492
492
///
493
+ /// If the number would be too large, return [`std::u64::MAX`] instead.
494
+ ///
493
495
/// # Errors
494
496
///
495
497
/// If a number cannot be parsed or if the multiplication would cause
@@ -512,12 +514,10 @@ fn parse_bytes_no_x(full: &str, s: &str) -> Result<u64, ParseError> {
512
514
let ( num, multiplier) = match ( s. find ( 'c' ) , s. rfind ( 'w' ) , s. rfind ( 'b' ) ) {
513
515
( None , None , None ) => match parser. parse_u64 ( s) {
514
516
Ok ( n) => ( n, 1 ) ,
517
+ Err ( ParseSizeError :: SizeTooBig ( _) ) => ( u64:: MAX , 1 ) ,
515
518
Err ( ParseSizeError :: InvalidSuffix ( _) | ParseSizeError :: ParseFailure ( _) ) => {
516
519
return Err ( ParseError :: InvalidNumber ( full. to_string ( ) ) )
517
520
}
518
- Err ( ParseSizeError :: SizeTooBig ( _) ) => {
519
- return Err ( ParseError :: MultiplierStringOverflow ( full. to_string ( ) ) )
520
- }
521
521
} ,
522
522
( Some ( i) , None , None ) => ( parse_bytes_only ( & s[ ..i] ) ?, 1 ) ,
523
523
( None , Some ( i) , None ) => ( parse_bytes_only ( & s[ ..i] ) ?, 2 ) ,
@@ -632,20 +632,30 @@ mod tests {
632
632
633
633
use crate :: parseargs:: parse_bytes_with_opt_multiplier;
634
634
635
+ const BIG : & str = "9999999999999999999999999999999999999999999999999999999999999" ;
636
+
637
+ #[ test]
638
+ fn test_parse_bytes_with_opt_multiplier_invalid ( ) {
639
+ assert ! ( parse_bytes_with_opt_multiplier( "123asdf" ) . is_err( ) ) ;
640
+ }
641
+
635
642
#[ test]
636
- fn test_parse_bytes_with_opt_multiplier ( ) {
643
+ fn test_parse_bytes_with_opt_multiplier_without_x ( ) {
637
644
assert_eq ! ( parse_bytes_with_opt_multiplier( "123" ) . unwrap( ) , 123 ) ;
638
645
assert_eq ! ( parse_bytes_with_opt_multiplier( "123c" ) . unwrap( ) , 123 ) ; // 123 * 1
639
646
assert_eq ! ( parse_bytes_with_opt_multiplier( "123w" ) . unwrap( ) , 123 * 2 ) ;
640
647
assert_eq ! ( parse_bytes_with_opt_multiplier( "123b" ) . unwrap( ) , 123 * 512 ) ;
641
- assert_eq ! ( parse_bytes_with_opt_multiplier( "123x3" ) . unwrap( ) , 123 * 3 ) ;
642
648
assert_eq ! ( parse_bytes_with_opt_multiplier( "123k" ) . unwrap( ) , 123 * 1024 ) ;
643
- assert_eq ! ( parse_bytes_with_opt_multiplier( "1x2x3" ) . unwrap( ) , 6 ) ; // 1 * 2 * 3
649
+ assert_eq ! ( parse_bytes_with_opt_multiplier( BIG ) . unwrap( ) , u64 :: MAX ) ;
650
+ }
644
651
652
+ #[ test]
653
+ fn test_parse_bytes_with_opt_multiplier_with_x ( ) {
654
+ assert_eq ! ( parse_bytes_with_opt_multiplier( "123x3" ) . unwrap( ) , 123 * 3 ) ;
655
+ assert_eq ! ( parse_bytes_with_opt_multiplier( "1x2x3" ) . unwrap( ) , 6 ) ; // 1 * 2 * 3
645
656
assert_eq ! (
646
657
parse_bytes_with_opt_multiplier( "1wx2cx3w" ) . unwrap( ) ,
647
658
2 * 2 * ( 3 * 2 ) // (1 * 2) * (2 * 1) * (3 * 2)
648
659
) ;
649
- assert ! ( parse_bytes_with_opt_multiplier( "123asdf" ) . is_err( ) ) ;
650
660
}
651
661
}
0 commit comments