@@ -145,7 +145,12 @@ 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 i32::MAX to match GNU behavior, which uses int for width.
149+ // Without this cap, very large widths (e.g., %8888888888r) cause OOM.
150+ let width: usize = width_str
151+ . parse :: < usize > ( )
152+ . unwrap_or ( 0 )
153+ . min ( i32:: MAX as usize ) ;
149154 let explicit_width = !width_str. is_empty ( ) ;
150155 let modified = apply_modifiers ( & formatted, flags, width, spec, explicit_width) ;
151156 result. push_str ( & modified) ;
@@ -710,4 +715,13 @@ mod tests {
710715 "GNU: %_C should produce '19', not ' 19' (default width is 2, not 4)"
711716 ) ;
712717 }
718+
719+ #[ test]
720+ fn test_large_width_capped_to_i32_max ( ) {
721+ // Regression test: widths exceeding i32::MAX caused OOM.
722+ // GNU caps width to int (i32), so %8888888888r should use width i32::MAX.
723+ let value = "12:00:00 AM" ;
724+ let result = apply_modifiers ( value, "" , i32:: MAX as usize , "r" , true ) ;
725+ assert_eq ! ( result. len( ) , i32 :: MAX as usize ) ;
726+ }
713727}
0 commit comments