File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -371,11 +371,18 @@ where
371
371
} else if s. starts_with ( '+' ) {
372
372
try_consume ! ( scan:: number( & s[ 1 ..] , 1 , usize :: MAX ) )
373
373
} else {
374
- // if there is no explicit sign, we respect the original `width`
374
+ // Signed value with no sign — variable width (legacy behavior)
375
375
try_consume ! ( scan:: number( s, 1 , width) )
376
376
}
377
377
} else {
378
- try_consume ! ( scan:: number( s, 1 , width) )
378
+ // Enforce exact width for unsigned values like %H, %M, %S
379
+ if s. len ( ) < width {
380
+ return Err ( TOO_SHORT ) ;
381
+ }
382
+ let ( val_str, rest) = s. split_at ( width) ;
383
+ let val = val_str. parse :: < i64 > ( ) . map_err ( |_| INVALID ) ?;
384
+ s = rest;
385
+ val
379
386
} ;
380
387
set ( parsed, v) ?;
381
388
}
Original file line number Diff line number Diff line change @@ -380,6 +380,26 @@ fn test_overflowing_offset() {
380
380
assert_eq ! ( t. overflowing_sub_offset( positive_offset) . 0 , t - positive_offset) ;
381
381
}
382
382
383
+ #[ test]
384
+ fn test_parse_from_str_enforces_exact_width ( ) {
385
+ assert_eq ! (
386
+ NaiveTime :: parse_from_str( "010203" , "%H%M%S" ) ,
387
+ Ok ( NaiveTime :: from_hms_opt( 1 , 2 , 3 ) . unwrap( ) )
388
+ ) ;
389
+
390
+ // Too short — should fail
391
+ assert ! ( NaiveTime :: parse_from_str( "01023" , "%H%M%S" ) . is_err( ) ) ;
392
+
393
+ // Too long — should fail
394
+ assert ! ( NaiveTime :: parse_from_str( "0102033" , "%H%M%S" ) . is_err( ) ) ;
395
+
396
+ // Another valid time
397
+ assert_eq ! (
398
+ NaiveTime :: parse_from_str( "235959" , "%H%M%S" ) ,
399
+ Ok ( NaiveTime :: from_hms_opt( 23 , 59 , 59 ) . unwrap( ) )
400
+ ) ;
401
+ }
402
+
383
403
#[ test]
384
404
#[ cfg( feature = "rkyv-validation" ) ]
385
405
fn test_rkyv_validation ( ) {
You can’t perform that action at this time.
0 commit comments