aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.c
diff options
context:
space:
mode:
authorLars Henriksen <LarsHenriksen@get2net.dk>2017-11-05 12:27:31 +0100
committerLukas Fleischer <lfleischer@calcurse.org>2018-08-25 10:03:28 +0200
commitc45da5f5ca550215588ba9483eef62f2a214addb (patch)
tree64745749595a055d62ca4cff34428cec2b766ac0 /src/utils.c
parent16d30327ba6047cb75fc6852020100bf7bfc6efa (diff)
downloadcalcurse-c45da5f5ca550215588ba9483eef62f2a214addb.tar.gz
calcurse-c45da5f5ca550215588ba9483eef62f2a214addb.zip
New support functions for input validation.
check_sec(), overflow_add(), overflow_mul() Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
index b90a9bf..8728dfb 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -789,6 +789,16 @@ int check_date(unsigned year, unsigned month, unsigned day)
}
/*
+ * 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;
+}