Skip to content

Commit

Permalink
calendarspec: be more graceful with two kinds of calendar expressions
Browse files Browse the repository at this point in the history
This changes the calendarspec parser to allow expressions such as
"00:05..05", i.e. a range where start and end is the same. It also
allows expressions such as "00:1-2/3", i.e. where the repetition value
does not fit even once in the specified range. With this patch both
cases will now be optimized away, i.e. the range is removed and a fixed
value is used, which is functionally equivalent.

See systemd#15030 for an issue where the inability to parse such expressions
caused confusion.

I think it's probably better to accept these gracefully and optimizing
them away instead of refusing them with a plain EINVAL. With a tool such
as "systemd-analyze" calendar it should be easy to figure out the
normalized form with the redundant bits optimized away.
  • Loading branch information
poettering committed Apr 30, 2020
1 parent 6d0b218 commit e4cac01
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/shared/calendarspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ static void normalize_chain(CalendarComponent **c) {
if (i->stop > i->start && i->repeat > 0)
i->stop -= (i->stop - i->start) % i->repeat;

/* If a repeat value is specified, but it cannot even be triggered once, let's suppress
* it.
*
* Similar, if the stop value is the same as the start value, then let's just make this a
* non-repeating chain element */
if ((i->stop > i->start && i->repeat > 0 && i->start + i->repeat > i->stop) ||
i->start == i->stop) {
i->repeat = 0;
i->stop = -1;
}
}

if (n <= 1)
Expand Down Expand Up @@ -644,6 +654,12 @@ static int prepend_component(const char **p, bool usec, unsigned nesting, Calend

if (repeat == 0)
return -ERANGE;
} else {
/* If no repeat value is specified for the µs component, then let's explicitly refuse ranges
* below 1s because our default repeat granularity is beyond that. */

if (usec && stop >= 0 && start + repeat > stop)
return -EINVAL;
}

if (!IN_SET(*e, 0, ' ', ',', '-', '~', ':'))
Expand Down
4 changes: 2 additions & 2 deletions src/test/test-calendarspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ int main(int argc, char* argv[]) {
test_one("@1493187147 UTC", "2017-04-26 06:12:27 UTC");
test_one("@0", "1970-01-01 00:00:00 UTC");
test_one("@0 UTC", "1970-01-01 00:00:00 UTC");
test_one("*:05..05", "*-*-* *:05:00");
test_one("*:05..10/6", "*-*-* *:05:00");

test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000);
test_next("2016-03-27 03:17:00", "CET", 12345, 1459041420000000);
Expand Down Expand Up @@ -237,8 +239,6 @@ int main(int argc, char* argv[]) {
assert_se(calendar_spec_from_string("*~29", &c) < 0);
assert_se(calendar_spec_from_string("*~16..31", &c) < 0);
assert_se(calendar_spec_from_string("12..1/2-*", &c) < 0);
assert_se(calendar_spec_from_string("*:05..05", &c) < 0);
assert_se(calendar_spec_from_string("*:05..10/6", &c) < 0);
assert_se(calendar_spec_from_string("20/4:00", &c) < 0);
assert_se(calendar_spec_from_string("00:00/60", &c) < 0);
assert_se(calendar_spec_from_string("00:00:2300", &c) < 0);
Expand Down

0 comments on commit e4cac01

Please sign in to comment.