summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2020-04-30 13:24:31 +0200
committerLennart Poettering <lennart@poettering.net>2020-05-05 08:57:14 +0200
commitc9c9f6f450f678a0d5e5b929ba238d4a5254bded (patch)
treeb93dc06eaa215dcae4cf40b1cea525c2d4ff5455
parent05851cb9df1305d53b99f1a0ee9d2acb7e1ffb52 (diff)
downloadsystemd-c9c9f6f450f678a0d5e5b929ba238d4a5254bded.tar.gz
calendarspec: be more graceful with two kinds of calendar expressions
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 #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.
-rw-r--r--src/shared/calendarspec.c16
-rw-r--r--src/test/test-calendarspec.c4
2 files changed, 18 insertions, 2 deletions
diff --git a/src/shared/calendarspec.c b/src/shared/calendarspec.c
index 084965e64e..b162224d9d 100644
--- a/src/shared/calendarspec.c
+++ b/src/shared/calendarspec.c
@@ -91,6 +91,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)
@@ -645,6 +655,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, ' ', ',', '-', '~', ':'))
diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c
index 9c2be7f445..9e2ae55ab6 100644
--- a/src/test/test-calendarspec.c
+++ b/src/test/test-calendarspec.c
@@ -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);
@@ -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);