1
0
mirror of https://github.com/GOSTSec/sgminer synced 2025-01-11 07:17:58 +00:00

Prevent overflows in us_tdiff and ms_tdiff.

This commit is contained in:
Con Kolivas 2013-10-07 15:37:34 +11:00
parent 8037eb15ad
commit d5e9e08477

10
util.c
View File

@ -1071,13 +1071,19 @@ void cgsleep_us(int64_t us)
/* Returns the microseconds difference between end and start times as a double */ /* Returns the microseconds difference between end and start times as a double */
double us_tdiff(struct timeval *end, struct timeval *start) double us_tdiff(struct timeval *end, struct timeval *start)
{ {
return end->tv_sec * 1000000 + end->tv_usec - start->tv_sec * 1000000 - start->tv_usec; /* Sanity check. We should only be using this for small differences so
* limit the max to 60 seconds. */
if (unlikely(end->tv_sec - start->tv_sec > 60))
return 60000000;
return (end->tv_sec - start->tv_sec) * 1000000 + (end->tv_usec - start->tv_usec);
} }
/* Returns the milliseconds difference between end and start times */ /* Returns the milliseconds difference between end and start times */
int ms_tdiff(struct timeval *end, struct timeval *start) int ms_tdiff(struct timeval *end, struct timeval *start)
{ {
return end->tv_sec * 1000 + end->tv_usec / 1000 - start->tv_sec * 1000 - start->tv_usec / 1000; if (unlikely(end->tv_sec - start->tv_sec > 60))
return 60000;
return (end->tv_sec - start->tv_sec) * 1000 + (end->tv_usec - start->tv_usec) / 1000;
} }
/* Returns the seconds difference between end and start times as a double */ /* Returns the seconds difference between end and start times as a double */