Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,18 @@ where
} else if s.starts_with('+') {
try_consume!(scan::number(&s[1..], 1, usize::MAX))
} else {
// if there is no explicit sign, we respect the original `width`
// Signed value with no sign — variable width (legacy behavior)
try_consume!(scan::number(s, 1, width))
}
} else {
try_consume!(scan::number(s, 1, width))
// Enforce exact width for unsigned values like %H, %M, %S
if s.len() < width {
return Err(TOO_SHORT);
}
let (val_str, rest) = s.split_at(width);
let val = val_str.parse::<i64>().map_err(|_| INVALID)?;
s = rest;
val
};
set(parsed, v)?;
}
Expand Down
20 changes: 20 additions & 0 deletions src/naive/time/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,26 @@ fn test_overflowing_offset() {
assert_eq!(t.overflowing_sub_offset(positive_offset).0, t - positive_offset);
}

#[test]
fn test_parse_from_str_enforces_exact_width() {
assert_eq!(
NaiveTime::parse_from_str("010203", "%H%M%S"),
Ok(NaiveTime::from_hms_opt(1, 2, 3).unwrap())
);

// Too short — should fail
assert!(NaiveTime::parse_from_str("01023", "%H%M%S").is_err());

// Too long — should fail
assert!(NaiveTime::parse_from_str("0102033", "%H%M%S").is_err());

// Another valid time
assert_eq!(
NaiveTime::parse_from_str("235959", "%H%M%S"),
Ok(NaiveTime::from_hms_opt(23, 59, 59).unwrap())
);
}

#[test]
#[cfg(feature = "rkyv-validation")]
fn test_rkyv_validation() {
Expand Down
Loading