Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse_time_interval: Allow negative time in settings #722

Merged
merged 1 commit into from
Jun 23, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions src/core/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,24 +781,35 @@ int parse_uint(const char *nptr, char **endptr, int base, guint *number)
return TRUE;
}

static int parse_number_sign(const char *input, char **endptr, int *sign)
{
int sign_ = 1;

while (i_isspace(*input))
input++;

if (*input == '-') {
sign_ = -sign_;
input++;
}

*sign = sign_;
*endptr = (char *) input;
return TRUE;
}

static int parse_time_interval_uint(const char *time, guint *msecs)
{
const char *desc;
guint number;
int sign, len, ret, digits;
int len, ret, digits;

*msecs = 0;

/* max. return value is around 24 days */
number = 0; sign = 1; ret = TRUE; digits = FALSE;
number = 0; ret = TRUE; digits = FALSE;
while (i_isspace(*time))
time++;
if (*time == '-') {
sign = -sign;
time++;
while (i_isspace(*time))
time++;
}
for (;;) {
if (i_isdigit(*time)) {
char *endptr;
Expand Down Expand Up @@ -828,7 +839,6 @@ static int parse_time_interval_uint(const char *time, guint *msecs)
if (*time != '\0')
return FALSE;
*msecs += number * 1000; /* assume seconds */
*msecs *= sign;
return TRUE;
}

Expand Down Expand Up @@ -866,7 +876,6 @@ static int parse_time_interval_uint(const char *time, guint *msecs)
digits = FALSE;
}

*msecs *= sign;
return ret;
}

Expand Down Expand Up @@ -960,15 +969,18 @@ int parse_size(const char *size, int *bytes)
int parse_time_interval(const char *time, int *msecs)
{
guint msecs_;
int ret;
char *number;
int ret, sign;

parse_number_sign(time, &number, &sign);

ret = parse_time_interval_uint(time, &msecs_);
ret = parse_time_interval_uint(number, &msecs_);

if (msecs_ > (1U << 31)) {
return FALSE;
}

*msecs = msecs_;
*msecs = msecs_ * sign;
return ret;
}

Expand Down