valid_temp_time: simplify/correct overflow check

the logic for trying to detect the maximum value of time_t was broken;
since we target a lower maximum time anyway, just use that for the
overflow check
This commit is contained in:
Ed Kellett 2022-06-19 22:07:20 +01:00
parent 8e239de831
commit 4f46809305
2 changed files with 3 additions and 6 deletions

View file

@ -687,8 +687,6 @@ valid_temp_time(const char *p)
time_t result = 0;
long current = 0;
time_t max_time = (uintmax_t) (~(time_t)0) >> 1;
while (*p) {
char *endp;
int mul;
@ -726,7 +724,7 @@ valid_temp_time(const char *p)
current *= mul;
if (current > max_time - result)
if (current > MAX_TEMP_TIME - result)
return MAX_TEMP_TIME;
result += current;

View file

@ -60,15 +60,14 @@ static void valid_temp_time_invalid(void)
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);
snprintf(s, sizeof s, "%" PRIuMAX "m", UINTMAX_MAX / 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);
snprintf(s, sizeof s, "%" PRIuMAX "m%" PRIuMAX "m", UINTMAX_MAX / 60 - 1, UINTMAX_MAX / 60 - 1);
t = valid_temp_time(s);
is_int(52 * WEEK, t, MSG);
}