calendarspec: reject open weekday ranges

Forbid open ranges like "Tue.."; trailing commas are still OK.
This commit is contained in:
Douglas Christman 2016-11-24 14:04:13 -05:00
parent 04773cb50a
commit 6bae2fd4cd
2 changed files with 16 additions and 7 deletions

View file

@ -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
l = -1;
*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;
}
}

View file

@ -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();