fix(cron): prevent some illegal expressions; add some tests
ci/woodpecker/pr/docs Pipeline was successful Details
ci/woodpecker/pr/lint Pipeline was successful Details
ci/woodpecker/pr/build Pipeline was successful Details
ci/woodpecker/pr/docker Pipeline was successful Details
ci/woodpecker/pr/man Pipeline was successful Details
ci/woodpecker/pr/test Pipeline was successful Details

Jef Roosens 2023-01-15 08:48:32 +01:00
parent 933d25a65c
commit 4ca4afddeb
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 84 additions and 13 deletions

View File

@ -67,9 +67,14 @@ cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max
uint8_t end = max;
uint8_t interval = 0;
if (s[0] == '*' && strlen(s) == 1) {
start = min;
interval = 1;
if (s[0] == '*') {
// A star character is only allowed on its own
if (s[1] == '\0' && dash_index == 0) {
start = min;
interval = 1;
} else {
return cron_parse_invalid_expression;
}
}else {
SAFE_ATOI(start, s, min, max);

View File

@ -18,6 +18,45 @@ pub struct C.cron_expression {
pub type Expression = C.cron_expression
// == returns whether the two expressions are equal by value.
fn (ce1 Expression) == (ce2 Expression) bool {
if ce1.month_count != ce2.month_count || ce1.day_count != ce2.day_count
|| ce1.hour_count != ce2.hour_count || ce1.minute_count != ce2.minute_count {
return false
}
for i in 0 .. ce1.month_count {
unsafe {
if ce1.months[i] != ce2.months[i] {
return false
}
}
}
for i in 0 .. ce1.day_count {
unsafe {
if ce1.days[i] != ce2.days[i] {
return false
}
}
}
for i in 0 .. ce1.hour_count {
unsafe {
if ce1.hours[i] != ce2.hours[i] {
return false
}
}
}
for i in 0 .. ce1.minute_count {
unsafe {
if ce1.minutes[i] != ce2.minutes[i] {
return false
}
}
}
return true
}
struct C.cron_simple_time {
year int
month int

View File

@ -34,13 +34,3 @@ fn test_next_simple() ! {
//// Overlap to next year
util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')!
}
fn test_leading_star() {
mut x := false
parse_expression('*5 8') or { x = true }
assert x
x = false
parse_expression('x 8') or { x = true }
assert x
}

View File

@ -0,0 +1,37 @@
module cron
fn test_not_allowed() {
mut res := false
parse_expression('4 *-7') or { res = true }
assert res
res = false
parse_expression('4 *-7/4') or { res = true }
assert res
res = false
parse_expression('4 7/*') or { res = true }
assert res
res = false
parse_expression('0 0 30 2') or { res = true }
assert res
}
fn test_leading_star() {
mut x := false
parse_expression('*5 8') or { x = true }
assert x
x = false
parse_expression('x 8') or { x = true }
assert x
}
fn test_auto_extend() ! {
ce1 := parse_expression('5 5')!
ce2 := parse_expression('5 5 *')!
ce3 := parse_expression('5 5 * *')!
assert ce1 == ce2 && ce2 == ce3
}