From 408a51e15614ca0c6cd80558adb20efe448d16d8 Mon Sep 17 00:00:00 2001 From: Douglas Christman Date: Thu, 24 Nov 2016 11:50:06 -0500 Subject: calendarspec: always interpret a missing time as 00:00:00 "*-*-*" is now equivalent to "*-*-* 00:00:00" (daily) rather than "*-*-* *:*:*" (every second). --- src/test/test-calendarspec.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/test') diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index 76f5819bb9..7357c522e0 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -179,6 +179,9 @@ int main(int argc, char* argv[]) { test_one("*-*~1 Utc", "*-*~01 00:00:00 UTC"); test_one("*-*~05,3 ", "*-*~03,05 00:00:00"); test_one("*-*~* 00:00:00", "*-*-* 00:00:00"); + test_one("Monday", "Mon *-*-* 00:00:00"); + test_one("Monday *-*-*", "Mon *-*-* 00:00:00"); + test_one("*-*-*", "*-*-* 00:00:00"); test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000); test_next("2016-03-27 03:17:00", "CET", 12345, 1459041420000000); @@ -199,6 +202,7 @@ int main(int argc, char* argv[]) { test_next("Mon 2017-05~07/1 UTC", "", 12345, 1496016000000000); assert_se(calendar_spec_from_string("test", &c) < 0); + assert_se(calendar_spec_from_string(" ", &c) < 0); assert_se(calendar_spec_from_string("", &c) < 0); assert_se(calendar_spec_from_string("7", &c) < 0); assert_se(calendar_spec_from_string("121212:1:2", &c) < 0); -- cgit v1.2.3-54-g00ecf From 04773cb50aca6484921c40bef3cbc0171c1e4786 Mon Sep 17 00:00:00 2001 From: Douglas Christman Date: Thu, 24 Nov 2016 12:21:37 -0500 Subject: calendarspec: reject strings that only contain a timezone This makes " UTC" an illegal date specification. --- src/basic/calendarspec.c | 8 +++++--- src/test/test-calendarspec.c | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'src/test') diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c index db11e55390..316db5c556 100644 --- a/src/basic/calendarspec.c +++ b/src/basic/calendarspec.c @@ -779,9 +779,6 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) { assert(p); assert(spec); - if (isempty(p)) - return -EINVAL; - c = new0(CalendarSpec, 1); if (!c) return -ENOMEM; @@ -820,6 +817,11 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) { } } + if (isempty(p)) { + r = -EINVAL; + goto fail; + } + if (strcaseeq(p, "minutely")) { r = const_chain(0, &c->microsecond); if (r < 0) diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index 7357c522e0..c539d16867 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -202,6 +202,7 @@ int main(int argc, char* argv[]) { test_next("Mon 2017-05~07/1 UTC", "", 12345, 1496016000000000); assert_se(calendar_spec_from_string("test", &c) < 0); + assert_se(calendar_spec_from_string(" utc", &c) < 0); assert_se(calendar_spec_from_string(" ", &c) < 0); assert_se(calendar_spec_from_string("", &c) < 0); assert_se(calendar_spec_from_string("7", &c) < 0); -- cgit v1.2.3-54-g00ecf From 6bae2fd4cdddd6c34f3d5e79906d8f6a75125922 Mon Sep 17 00:00:00 2001 From: Douglas Christman Date: Thu, 24 Nov 2016 14:04:13 -0500 Subject: calendarspec: reject open weekday ranges Forbid open ranges like "Tue.."; trailing commas are still OK. --- src/basic/calendarspec.c | 18 ++++++++++++------ src/test/test-calendarspec.c | 3 +++ 2 files changed, 15 insertions(+), 6 deletions(-) (limited to 'src/test') diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c index 316db5c556..eef4ed5240 100644 --- a/src/basic/calendarspec.c +++ b/src/basic/calendarspec.c @@ -372,9 +372,6 @@ static int parse_weekdays(const char **p, CalendarSpec *c) { for (;;) { unsigned i; - if (!first && **p == ' ') - return 0; - for (i = 0; i < ELEMENTSOF(day_nr); i++) { size_t skip; @@ -430,7 +427,7 @@ static int parse_weekdays(const char **p, CalendarSpec *c) { return -EINVAL; l = day_nr[i].nr; - *p += 1; + *p += 2; /* Support ranges with "-" for backwards compatibility */ } else if (**p == '-') { @@ -438,10 +435,19 @@ static int parse_weekdays(const char **p, CalendarSpec *c) { return -EINVAL; l = day_nr[i].nr; - } else + *p += 1; + + } else if (**p == ',') { l = -1; + *p += 1; + } + + /* Allow a trailing comma but not an open range */ + if (**p == 0 || **p == ' ') { + *p += strspn(*p, " "); + return l < 0 ? 0 : -EINVAL; + } - *p += 1; first = false; } } diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index c539d16867..93414c8508 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -143,6 +143,7 @@ int main(int argc, char* argv[]) { test_one("Wed-Wed,Wed *-1", "Wed *-*-01 00:00:00"); test_one("Wed..Wed,Wed *-1", "Wed *-*-01 00:00:00"); test_one("Wed, 17:48", "Wed *-*-* 17:48:00"); + test_one("Wednesday,", "Wed *-*-* 00:00:00"); test_one("Wed-Sat,Tue 12-10-15 1:2:3", "Tue..Sat 2012-10-15 01:02:03"); test_one("Wed..Sat,Tue 12-10-15 1:2:3", "Tue..Sat 2012-10-15 01:02:03"); test_one("*-*-7 0:0:0", "*-*-07 00:00:00"); @@ -213,6 +214,8 @@ int main(int argc, char* argv[]) { assert_se(calendar_spec_from_string("00:00:00.0..00.9", &c) < 0); assert_se(calendar_spec_from_string("2016~11-22", &c) < 0); assert_se(calendar_spec_from_string("*-*~5/5", &c) < 0); + assert_se(calendar_spec_from_string("Monday.. 12:00", &c) < 0); + assert_se(calendar_spec_from_string("Monday..", &c) < 0); test_timestamp(); test_hourly_bug_4031(); -- cgit v1.2.3-54-g00ecf From 9dfa81a00a9a7d4536f96848119c5ad40d9d72b4 Mon Sep 17 00:00:00 2001 From: Douglas Christman Date: Thu, 24 Nov 2016 12:47:55 -0500 Subject: calendarspec: reject strings with spurious spaces and signs strtoul() parses leading whitespace and an optional sign; check that the first character is a digit to prevent odd specifications like "00: 00: 00" and "-00:+00/-1". --- src/basic/calendarspec.c | 4 ++++ src/test/test-calendarspec.c | 3 +++ 2 files changed, 7 insertions(+) (limited to 'src/test') diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c index eef4ed5240..1555230e30 100644 --- a/src/basic/calendarspec.c +++ b/src/basic/calendarspec.c @@ -18,6 +18,7 @@ ***/ #include +#include #include #include #include @@ -458,6 +459,9 @@ static int parse_component_decimal(const char **p, bool usec, unsigned long *res char *ee = NULL; int r; + if (!isdigit(**p)) + return -EINVAL; + errno = 0; value = strtoul(*p, &ee, 10); if (errno > 0) diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index 93414c8508..873a4910d2 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -216,6 +216,9 @@ int main(int argc, char* argv[]) { assert_se(calendar_spec_from_string("*-*~5/5", &c) < 0); assert_se(calendar_spec_from_string("Monday.. 12:00", &c) < 0); assert_se(calendar_spec_from_string("Monday..", &c) < 0); + assert_se(calendar_spec_from_string("-00:+00/-5", &c) < 0); + assert_se(calendar_spec_from_string("00:+00/-5", &c) < 0); + assert_se(calendar_spec_from_string("2016- 11- 24 12: 30: 00", &c) < 0); test_timestamp(); test_hourly_bug_4031(); -- cgit v1.2.3-54-g00ecf From 3215e35c405278491f55fb486d349f132e93f516 Mon Sep 17 00:00:00 2001 From: Douglas Christman Date: Thu, 24 Nov 2016 14:34:17 -0500 Subject: calendarspec: make specifications with seconds wildcard reversible "*:*:*" is now formatted as "*:*:*" instead of "*:*:00/1" --- src/basic/calendarspec.c | 8 +++++++- src/test/test-calendarspec.c | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'src/test') diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c index 1555230e30..1de04feedf 100644 --- a/src/basic/calendarspec.c +++ b/src/basic/calendarspec.c @@ -286,6 +286,7 @@ static void format_chain(FILE *f, int space, const CalendarComponent *c, bool us } int calendar_spec_to_string(const CalendarSpec *c, char **p) { + CalendarComponent *cc; char *buf = NULL; size_t sz = 0; FILE *f; @@ -313,7 +314,12 @@ int calendar_spec_to_string(const CalendarSpec *c, char **p) { fputc(':', f); format_chain(f, 2, c->minute, false); fputc(':', f); - format_chain(f, 2, c->microsecond, true); + + cc = c->microsecond; + if (cc && !cc->value && cc->repeat == USEC_PER_SEC && !cc->next) + fputc('*', f); + else + format_chain(f, 2, c->microsecond, true); if (c->utc) fputs(" UTC", f); diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index 873a4910d2..d4bb4a1707 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -183,6 +183,7 @@ int main(int argc, char* argv[]) { test_one("Monday", "Mon *-*-* 00:00:00"); test_one("Monday *-*-*", "Mon *-*-* 00:00:00"); test_one("*-*-*", "*-*-* 00:00:00"); + test_one("*:*:*", "*-*-* *:*:*"); test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000); test_next("2016-03-27 03:17:00", "CET", 12345, 1459041420000000); -- cgit v1.2.3-54-g00ecf From 36ff0c979258844ca8a200f49d434997299416e6 Mon Sep 17 00:00:00 2001 From: Douglas Christman Date: Thu, 24 Nov 2016 15:51:07 -0500 Subject: calendarspec: allow whole second ranges Previously a string like "00:00:01..03" would fail to parse due to the ambiguity between a decimal point and the start of a range. --- src/basic/calendarspec.c | 5 +++++ src/test/test-calendarspec.c | 1 + 2 files changed, 6 insertions(+) (limited to 'src/test') diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c index 1de04feedf..4ef22dfffb 100644 --- a/src/basic/calendarspec.c +++ b/src/basic/calendarspec.c @@ -486,6 +486,10 @@ static int parse_component_decimal(const char **p, bool usec, unsigned long *res if (*e == '.') { unsigned add; + /* This is the start of a range, not a fractional part */ + if (e[1] == '.') + goto finish; + e++; r = parse_fractional_part_u(&e, 6, &add); if (r < 0) @@ -497,6 +501,7 @@ static int parse_component_decimal(const char **p, bool usec, unsigned long *res } } +finish: *p = e; *res = value; diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index d4bb4a1707..0bb29af545 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -177,6 +177,7 @@ int main(int argc, char* argv[]) { test_one("1..3-1..3 1..3:1..3", "*-01,02,03-01,02,03 01,02,03:01,02,03:00"); test_one("00:00:1.125..2.125", "*-*-* 00:00:01.125000,02.125000"); test_one("00:00:1.0..3.8", "*-*-* 00:00:01,02,03"); + test_one("00:00:01..03", "*-*-* 00:00:01,02,03"); test_one("*-*~1 Utc", "*-*~01 00:00:00 UTC"); test_one("*-*~05,3 ", "*-*~03,05 00:00:00"); test_one("*-*~* 00:00:00", "*-*-* 00:00:00"); -- cgit v1.2.3-54-g00ecf From 9904dc00e70c44762f63c05bce703f2a2a0e46bb Mon Sep 17 00:00:00 2001 From: Douglas Christman Date: Thu, 24 Nov 2016 15:41:04 -0500 Subject: calendarspec: make specifications with ranges reversible "*-*-01..03" is now formatted as "*-*-01..03" instead of "*-*-01,02,03" --- man/systemd.time.xml | 4 ++-- src/basic/calendarspec.c | 20 +++++++++++++++++++- src/test/test-calendarspec.c | 13 +++++++------ 3 files changed, 28 insertions(+), 9 deletions(-) (limited to 'src/test') diff --git a/man/systemd.time.xml b/man/systemd.time.xml index fb13ea52ff..c182d4f37a 100644 --- a/man/systemd.time.xml +++ b/man/systemd.time.xml @@ -273,7 +273,7 @@ Wed..Sat,Tue 12-10-15 1:2:3 → Tue..Sat 2012-10-15 01:02:03 monday *-12-* 17:00 → Mon *-12-* 17:00:00 Mon,Fri *-*-3,1,2 *:30:45 → Mon,Fri *-*-01,02,03 *:30:45 12,14,13,12:20,10,30 → *-*-* 12,13,14:10,20,30:00 - 12..14:10,20,30 → *-*-* 12,13,14:10,20,30:00 + 12..14:10,20,30 → *-*-* 12..14:10,20,30:00 mon,fri *-1/2-1,3 *:30:45 → Mon,Fri *-01/2-01,03 *:30:45 03-05 08:05:40 → *-03-05 08:05:40 08:05:40 → *-*-* 08:05:40 @@ -282,7 +282,7 @@ Wed..Sat,Tue 12-10-15 1:2:3 → Tue..Sat 2012-10-15 01:02:03 Sat,Sun 08:05:40 → Sat,Sun *-*-* 08:05:40 2003-03-05 05:40 → 2003-03-05 05:40:00 05:40:23.4200004/3.1700005 → 05:40:23.420000/3.170001 - 2003-02..04-05 → 2003-02,03,04-05 00:00:00 + 2003-02..04-05 → 2003-02..04-05 00:00:00 2003-03-05 05:40 UTC → 2003-03-05 05:40:00 UTC 2003-03-05 → 2003-03-05 00:00:00 03-05 → *-03-05 00:00:00 diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c index 4ef22dfffb..0caa1d4696 100644 --- a/src/basic/calendarspec.c +++ b/src/basic/calendarspec.c @@ -255,6 +255,8 @@ static void format_weekdays(FILE *f, const CalendarSpec *c) { } static void format_chain(FILE *f, int space, const CalendarComponent *c, bool usec) { + const CalendarComponent *n, *p; + assert(f); if (!c) { @@ -279,7 +281,23 @@ static void format_chain(FILE *f, int space, const CalendarComponent *c, bool us fprintf(f, "/%i.%06i", (int) (c->repeat / USEC_PER_SEC), (int) (c->repeat % USEC_PER_SEC)); } - if (c->next) { + p = c; + for (;;) { + n = p->next; + + if (!n || n->repeat || p->repeat) + break; + + if (n->value - p->value != (usec ? (int) USEC_PER_SEC : 1)) + break; + + p = n; + } + + if (p->value - c->value >= 2 * (usec ? (int) USEC_PER_SEC : 1)) { + fputs("..", f); + format_chain(f, space, p, usec); + } else if (c->next) { fputc(',', f); format_chain(f, space, c->next, usec); } diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index 0bb29af545..b3d1160ea7 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -149,8 +149,8 @@ int main(int argc, char* argv[]) { test_one("*-*-7 0:0:0", "*-*-07 00:00:00"); test_one("10-15", "*-10-15 00:00:00"); test_one("monday *-12-* 17:00", "Mon *-12-* 17:00:00"); - test_one("Mon,Fri *-*-3,1,2 *:30:45", "Mon,Fri *-*-01,02,03 *:30:45"); - test_one("12,14,13,12:20,10,30", "*-*-* 12,13,14:10,20,30:00"); + test_one("Mon,Fri *-*-3,1,2 *:30:45", "Mon,Fri *-*-01..03 *:30:45"); + test_one("12,14,13,12:20,10,30", "*-*-* 12..14:10,20,30:00"); test_one("mon,fri *-1/2-1,3 *:30:45", "Mon,Fri *-01/2-01,03 *:30:45"); test_one("03-05 08:05:40", "*-03-05 08:05:40"); test_one("08:05:40", "*-*-* 08:05:40"); @@ -173,11 +173,12 @@ int main(int argc, char* argv[]) { test_one("2016-03-27 03:17:00.4200005", "2016-03-27 03:17:00.420001"); test_one("2016-03-27 03:17:00/0.42", "2016-03-27 03:17:00/0.420000"); test_one("2016-03-27 03:17:00/0.42", "2016-03-27 03:17:00/0.420000"); - test_one("9..11,13:00,30", "*-*-* 09,10,11,13:00,30:00"); - test_one("1..3-1..3 1..3:1..3", "*-01,02,03-01,02,03 01,02,03:01,02,03:00"); + test_one("9..11,13:00,30", "*-*-* 09..11,13:00,30:00"); + test_one("1..3-1..3 1..3:1..3", "*-01..03-01..03 01..03:01..03:00"); test_one("00:00:1.125..2.125", "*-*-* 00:00:01.125000,02.125000"); - test_one("00:00:1.0..3.8", "*-*-* 00:00:01,02,03"); - test_one("00:00:01..03", "*-*-* 00:00:01,02,03"); + test_one("00:00:1.0..3.8", "*-*-* 00:00:01..03"); + test_one("00:00:01..03", "*-*-* 00:00:01..03"); + test_one("00:00:01/2,02..03", "*-*-* 00:00:01/2,02,03"); test_one("*-*~1 Utc", "*-*~01 00:00:00 UTC"); test_one("*-*~05,3 ", "*-*~03,05 00:00:00"); test_one("*-*~* 00:00:00", "*-*-* 00:00:00"); -- cgit v1.2.3-54-g00ecf