summaryrefslogtreecommitdiff
path: root/src/basic/calendarspec.c
diff options
context:
space:
mode:
authorDouglas Christman <DouglasChristman@gmail.com>2016-12-20 16:42:12 -0500
committerDouglas Christman <DouglasChristman@gmail.com>2016-12-20 17:52:55 -0500
commitc0aebb4b3b38caca64ae190f1d523edf46f03969 (patch)
tree804df8f381b3ea651a2bb6abb0eb883fa25961a4 /src/basic/calendarspec.c
parent9f702d00d61a9d7bceeb07eced01427a85193021 (diff)
calendarspec: improve overflow handling
Check if the parsed seconds value fits in an integer *after* multiplying by USEC_PER_SEC, otherwise a large value can trigger modulo by zero during normalization.
Diffstat (limited to 'src/basic/calendarspec.c')
-rw-r--r--src/basic/calendarspec.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c
index 3e249505a5..1b1acd3e0b 100644
--- a/src/basic/calendarspec.c
+++ b/src/basic/calendarspec.c
@@ -20,6 +20,7 @@
#include <alloca.h>
#include <ctype.h>
#include <errno.h>
+#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@@ -96,7 +97,7 @@ static void normalize_chain(CalendarComponent **c) {
* While we're counting the chain, also normalize `stop`
* so the length of the range is a multiple of `repeat`
*/
- if (i->stop > i->start)
+ if (i->stop > i->start && i->repeat > 0)
i->stop -= (i->stop - i->start) % i->repeat;
}
@@ -487,7 +488,7 @@ static int parse_weekdays(const char **p, CalendarSpec *c) {
}
}
-static int parse_component_decimal(const char **p, bool usec, unsigned long *res) {
+static int parse_component_decimal(const char **p, bool usec, int *res) {
unsigned long value;
const char *e = NULL;
char *ee = NULL;
@@ -502,8 +503,6 @@ static int parse_component_decimal(const char **p, bool usec, unsigned long *res
return -errno;
if (ee == *p)
return -EINVAL;
- if ((unsigned long) (int) value != value)
- return -ERANGE;
e = ee;
if (usec) {
@@ -530,6 +529,9 @@ static int parse_component_decimal(const char **p, bool usec, unsigned long *res
}
finish:
+ if (value > INT_MAX)
+ return -ERANGE;
+
*p = e;
*res = value;
@@ -556,9 +558,8 @@ static int const_chain(int value, CalendarComponent **c) {
}
static int prepend_component(const char **p, bool usec, CalendarComponent **c) {
- unsigned long start, stop = -1, repeat = 0;
+ int r, start, stop = -1, repeat = 0;
CalendarComponent *cc;
- int r;
const char *e;
assert(p);