Skip to content

Commit

Permalink
fix: Don't accept invalid step for Period (#122)
Browse files Browse the repository at this point in the history
* fix: Don't accept invalid intervals for periods

If the specifier is a period specifier, ensure that the interval is
larger than 0 (0 would be every 0th) and less than the max value for the
unit.

* fix: Move step check to appropriate function
  • Loading branch information
bombsimon authored Oct 29, 2024
1 parent c5d5589 commit cabee0f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,4 +661,34 @@ mod test {
let expression = "* * * ? * ?";
Schedule::from_str(expression).unwrap();
}

/// Issue #59
#[test]
fn test_reject_invalid_interval() {
for invalid_expression in [
"1-5/61 * * * * *",
"*/61 2 3 4 5 6",
"* */61 * * * *",
"* * */25 * * *",
"* * * */32 * *",
"* * * * */13 *",
"1,2,3/60 * * * * *",
"0 0 0 1 1 ? 2020-2040/2200",
] {
assert!(schedule(invalid_expression).is_err());
}

for valid_expression in [
"1-5/59 * * * * *",
"*/10 2 3 4 5 6",
"* */30 * * * *",
"* * */23 * * *",
"* * * */30 * *",
"* * * * */10 *",
"1,2,3/5 * * * * *",
"0 0 0 1 1 ? 2020-2040/10",
] {
assert!(schedule(valid_expression).is_ok());
}
}
}
10 changes: 10 additions & 0 deletions src/time_unit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,16 @@ where
"range step cannot be zero".to_string(),
))?,
RootSpecifier::Period(start, step) => {
if *step < 1 || *step > Self::inclusive_max() {
return Err(ErrorKind::Expression(format!(
"{} must be between 1 and {}. ('{}' specified.)",
Self::name(),
Self::inclusive_max(),
step,
))
.into());
}

let base_set = match start {
// A point prior to a period implies a range whose start is the specified
// point and terminating inclusively with the inclusive max
Expand Down

0 comments on commit cabee0f

Please sign in to comment.