@@ -145,7 +145,11 @@ fn format_with_modifiers(
145145 // Check if this specifier has modifiers
146146 if !flags. is_empty ( ) || !width_str. is_empty ( ) {
147147 // Apply modifiers to the formatted value
148- let width: usize = width_str. parse ( ) . unwrap_or ( 0 ) ;
148+ // Cap width to a reasonable maximum to prevent OOM.
149+ // GNU date appears to cap output at around 1000 characters,
150+ // so we'll use a similar limit.
151+ const MAX_WIDTH : usize = 1000 ;
152+ let width: usize = width_str. parse :: < usize > ( ) . unwrap_or ( 0 ) . min ( MAX_WIDTH ) ;
149153 let explicit_width = !width_str. is_empty ( ) ;
150154 let modified = apply_modifiers ( & formatted, flags, width, spec, explicit_width) ;
151155 result. push_str ( & modified) ;
@@ -710,4 +714,13 @@ mod tests {
710714 "GNU: %_C should produce '19', not ' 19' (default width is 2, not 4)"
711715 ) ;
712716 }
717+
718+ #[ test]
719+ fn test_large_width_capped ( ) {
720+ // Regression test: very large widths caused OOM.
721+ // We cap width to MAX_WIDTH (1000) to prevent memory issues.
722+ let value = "12:00:00 AM" ;
723+ let result = apply_modifiers ( value, "" , 1000 , "r" , true ) ;
724+ assert_eq ! ( result. len( ) , 1000 ) ;
725+ }
713726}
0 commit comments