From 1fcdacb424dd9203923a1c25db921b5329eb6c2e Mon Sep 17 00:00:00 2001 From: Ed Kellett Date: Fri, 18 Mar 2022 18:13:24 +0000 Subject: [PATCH] Test some edge cases of valid_temp_time --- ircd/s_newconf.c | 2 ++ tests/misc.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/ircd/s_newconf.c b/ircd/s_newconf.c index 31fcc959..0ee96e3e 100644 --- a/ircd/s_newconf.c +++ b/ircd/s_newconf.c @@ -697,6 +697,8 @@ valid_temp_time(const char *p) return -1; if (endp == p) return -1; + if (current < 0) + return -1; switch (*endp) { case '\0': /* No unit was given so send it back as minutes */ diff --git a/tests/misc.c b/tests/misc.c index 4a0e7141..e588266e 100644 --- a/tests/misc.c +++ b/tests/misc.c @@ -42,11 +42,44 @@ static void valid_temp_time1(void) is_int(52 * WEEK, t, MSG); } +static void valid_temp_time_invalid(void) +{ + time_t t; + t = valid_temp_time("-2w"); + is_int(-1, t, MSG); + + t = valid_temp_time("hello"); + is_int(-1, t, MSG); + + t = valid_temp_time("m"); + is_int(-1, t, MSG); + + t = valid_temp_time("1w-1w"); + is_int(-1, t, MSG); +} + +static void valid_temp_time_overflow(void) +{ + time_t max_time = (uintmax_t) (~(time_t)0) >> 1; + char s[100]; + time_t t; + + snprintf(s, sizeof s, "%" PRIuMAX "m", (uintmax_t) max_time / 60 + 2); + t = valid_temp_time(s); + is_int(52 * WEEK, t, MSG); + + snprintf(s, sizeof s, "%" PRIuMAX "m%" PRIuMAX "m", (uintmax_t) max_time / 60 - 1, (uintmax_t) max_time / 60 - 1); + t = valid_temp_time(s); + is_int(52 * WEEK, t, MSG); +} + int main(int argc, char *argv[]) { plan_lazy(); valid_temp_time1(); + valid_temp_time_invalid(); + valid_temp_time_overflow(); return 0; }