bfdd: Fix overflow possibility with time statements

If time ( a uint64_t ) is large enough doing division
and subtraction can still lead to situations where
the resulting number is greater than a uint32_t.
Just use uint32_t as an intermediate storage spot.
This is unlikely to every occur in a time frame
I could possibly care about but makes Coverity happy.

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
This commit is contained in:
Donald Sharp 2022-02-18 10:45:46 -05:00
parent 4d7aae38ab
commit 46da676a62

View File

@ -1424,7 +1424,7 @@ int strtosa(const char *addr, struct sockaddr_any *sa)
void integer2timestr(uint64_t time, char *buf, size_t buflen)
{
unsigned int year, month, day, hour, minute, second;
uint64_t year, month, day, hour, minute, second;
int rv;
#define MINUTES (60)
@ -1436,7 +1436,7 @@ void integer2timestr(uint64_t time, char *buf, size_t buflen)
year = time / YEARS;
time -= year * YEARS;
rv = snprintf(buf, buflen, "%u year(s), ", year);
rv = snprintfrr(buf, buflen, "%" PRIu64 " year(s), ", year);
buf += rv;
buflen -= rv;
}
@ -1444,7 +1444,7 @@ void integer2timestr(uint64_t time, char *buf, size_t buflen)
month = time / MONTHS;
time -= month * MONTHS;
rv = snprintf(buf, buflen, "%u month(s), ", month);
rv = snprintfrr(buf, buflen, "%" PRIu64 " month(s), ", month);
buf += rv;
buflen -= rv;
}
@ -1452,7 +1452,7 @@ void integer2timestr(uint64_t time, char *buf, size_t buflen)
day = time / DAYS;
time -= day * DAYS;
rv = snprintf(buf, buflen, "%u day(s), ", day);
rv = snprintfrr(buf, buflen, "%" PRIu64 " day(s), ", day);
buf += rv;
buflen -= rv;
}
@ -1460,7 +1460,7 @@ void integer2timestr(uint64_t time, char *buf, size_t buflen)
hour = time / HOURS;
time -= hour * HOURS;
rv = snprintf(buf, buflen, "%u hour(s), ", hour);
rv = snprintfrr(buf, buflen, "%" PRIu64 " hour(s), ", hour);
buf += rv;
buflen -= rv;
}
@ -1468,12 +1468,12 @@ void integer2timestr(uint64_t time, char *buf, size_t buflen)
minute = time / MINUTES;
time -= minute * MINUTES;
rv = snprintf(buf, buflen, "%u minute(s), ", minute);
rv = snprintfrr(buf, buflen, "%" PRIu64 " minute(s), ", minute);
buf += rv;
buflen -= rv;
}
second = time % MINUTES;
snprintf(buf, buflen, "%u second(s)", second);
snprintfrr(buf, buflen, "%" PRIu64 " second(s)", second);
}
const char *bs_to_string(const struct bfd_session *bs)