New support functions for input validation.

check_sec(), overflow_add(), overflow_mul()

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lars Henriksen
2017-11-05 12:27:31 +01:00
committed by Lukas Fleischer
parent 16d30327ba
commit c45da5f5ca
2 changed files with 39 additions and 0 deletions

View File

@@ -788,6 +788,16 @@ int check_date(unsigned year, unsigned month, unsigned day)
day <= days[month - 1] + (month == 2 && ISLEAP(year)) ? 1 : 0);
}
/*
* Check that a time in seconds is a valid calcurse date (ignoring hour:min:sec).
*/
int check_sec(time_t *time)
{
struct tm tm;
localtime_r(time, &tm);
return check_date(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
}
/*
* Convert a string containing a date into three integers containing the year,
* month and day.
@@ -1870,3 +1880,29 @@ int show_dialogs(void)
{
return (!quiet) && conf.system_dialogs;
}
/*
* Overflow check for addition with positive second term.
*/
int overflow_add(int x, int y, int *z)
{
if (y < 0)
return 1;
if (INT_MAX - y < x)
return 1;
*z = x + y;
return 0;
}
/*
* Overflow check for multiplication with positive terms.
*/
int overflow_mul(int x, int y, int *z)
{
if (x < 0 || y <= 0)
return 1;
if (INT_MAX / y < x)
return 1;
*z = x * y;
return 0;
}