File tree Expand file tree Collapse file tree 2 files changed +27
-5
lines changed
Expand file tree Collapse file tree 2 files changed +27
-5
lines changed Original file line number Diff line number Diff line change @@ -683,11 +683,17 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
683683 } else if matches. get_flag ( options:: BLOCK_SIZE_1M ) {
684684 SizeFormat :: BlockSize ( 1024 * 1024 )
685685 } else {
686- SizeFormat :: BlockSize ( read_block_size (
687- matches
688- . get_one :: < String > ( options:: BLOCK_SIZE )
689- . map ( AsRef :: as_ref) ,
690- ) ?)
686+ let block_size_str = matches. get_one :: < String > ( options:: BLOCK_SIZE ) ;
687+ let block_size = read_block_size ( block_size_str. map ( AsRef :: as_ref) ) ?;
688+ if block_size == 0 {
689+ return Err ( std:: io:: Error :: other ( format ! (
690+ "invalid --{} argument {}" ,
691+ options:: BLOCK_SIZE ,
692+ block_size_str. map_or( "???BUG" , |v| v) . quote( )
693+ ) )
694+ . into ( ) ) ;
695+ }
696+ SizeFormat :: BlockSize ( block_size)
691697 } ;
692698
693699 let traversal_options = TraversalOptions {
Original file line number Diff line number Diff line change @@ -1248,3 +1248,19 @@ fn test_du_no_deduplicated_input_args() {
12481248 . collect ( ) ;
12491249 assert_eq ! ( result_seq, [ "2\t d" , "2\t d" , "2\t d" ] ) ;
12501250}
1251+
1252+ #[ test]
1253+ fn test_du_blocksize_zero_do_not_panic ( ) {
1254+ let ts = TestScenario :: new ( util_name ! ( ) ) ;
1255+ let at = & ts. fixtures ;
1256+ at. write ( "foo" , "some content" ) ;
1257+ for block_size in [ "0" , "00" , "000" , "0x0" ] {
1258+ ts. ucmd ( )
1259+ . arg ( format ! ( "-B{block_size}" ) )
1260+ . arg ( "foo" )
1261+ . fails ( )
1262+ . stderr_only ( format ! (
1263+ "du: invalid --block-size argument '{block_size}'\n "
1264+ ) ) ;
1265+ }
1266+ }
You can’t perform that action at this time.
0 commit comments