summaryrefslogtreecommitdiff
path: root/lib/timeval.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2017-10-23 12:05:49 +0200
committerDaniel Stenberg <daniel@haxx.se>2017-10-25 09:54:37 +0200
commitb9d25f9a6b3ca791385b80a6a3c3fa5ae113e1e0 (patch)
treead56d8b7a703e96e9de0ef07bacab079b7169904 /lib/timeval.c
parent016c6a6abb525ffb9431a00a4eda6b70683f838e (diff)
downloadcurl-b9d25f9a6b3ca791385b80a6a3c3fa5ae113e1e0.tar.gz
timediff: return timediff_t from the time diff functions
... to cater for systems with unsigned time_t variables. - Renamed the functions to curlx_timediff and Curl_timediff_us. - Added overflow protection for both of them in either direction for both 32 bit and 64 bit time_ts - Reprefixed the curlx_time functions to use Curl_* Reported-by: Peter Piekarski Fixes #2004 Closes #2005
Diffstat (limited to 'lib/timeval.c')
-rw-r--r--lib/timeval.c55
1 files changed, 28 insertions, 27 deletions
diff --git a/lib/timeval.c b/lib/timeval.c
index d7207b3a2..6ed79ec60 100644
--- a/lib/timeval.c
+++ b/lib/timeval.c
@@ -24,7 +24,7 @@
#if defined(WIN32) && !defined(MSDOS)
-struct curltime curlx_tvnow(void)
+struct curltime Curl_tvnow(void)
{
/*
** GetTickCount() is available on _all_ Windows versions from W95 up
@@ -48,7 +48,7 @@ struct curltime curlx_tvnow(void)
#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
-struct curltime curlx_tvnow(void)
+struct curltime Curl_tvnow(void)
{
/*
** clock_gettime() is granted to be increased monotonically when the
@@ -86,7 +86,7 @@ struct curltime curlx_tvnow(void)
#elif defined(HAVE_GETTIMEOFDAY)
-struct curltime curlx_tvnow(void)
+struct curltime Curl_tvnow(void)
{
/*
** gettimeofday() is not granted to be increased monotonically, due to
@@ -103,7 +103,7 @@ struct curltime curlx_tvnow(void)
#else
-struct curltime curlx_tvnow(void)
+struct curltime Curl_tvnow(void)
{
/*
** time() returns the value of time in seconds since the Epoch.
@@ -116,6 +116,14 @@ struct curltime curlx_tvnow(void)
#endif
+#if SIZEOF_TIME_T < 8
+#define TIME_MAX INT_MAX
+#define TIME_MIN INT_MIN
+#else
+#define TIME_MAX 9223372036854775807LL
+#define TIME_MIN -9223372036854775807LL
+#endif
+
/*
* Make sure that the first argument is the more recent time, as otherwise
* we'll get a weird negative time-diff back...
@@ -125,17 +133,15 @@ struct curltime curlx_tvnow(void)
*
* @unittest: 1323
*/
-time_t curlx_tvdiff(struct curltime newer, struct curltime older)
+timediff_t Curl_timediff(struct curltime newer, struct curltime older)
{
-#if SIZEOF_TIME_T < 8
- /* for 32bit time_t systems, add a precaution to avoid overflow for really
- big time differences */
- time_t diff = newer.tv_sec-older.tv_sec;
- if(diff >= (0x7fffffff/1000))
- return 0x7fffffff;
-#endif
- return (newer.tv_sec-older.tv_sec)*1000+
- (int)(newer.tv_usec-older.tv_usec)/1000;
+ timediff_t diff = newer.tv_sec-older.tv_sec;
+ if(diff >= (TIME_MAX/1000))
+ return TIME_MAX;
+ else if(diff <= (TIME_MIN/1000))
+ return TIME_MIN;
+ return (timediff_t)(newer.tv_sec-older.tv_sec)*1000+
+ (timediff_t)(newer.tv_usec-older.tv_usec)/1000;
}
/*
@@ -145,18 +151,13 @@ time_t curlx_tvdiff(struct curltime newer, struct curltime older)
* Returns: the time difference in number of microseconds. For too large diffs
* it returns max value.
*/
-time_t Curl_tvdiff_us(struct curltime newer, struct curltime older)
+timediff_t Curl_timediff_us(struct curltime newer, struct curltime older)
{
- time_t diff = newer.tv_sec-older.tv_sec;
-#if SIZEOF_TIME_T < 8
- /* for 32bit time_t systems */
- if(diff >= (0x7fffffff/1000000))
- return 0x7fffffff;
-#else
- /* for 64bit time_t systems */
- if(diff >= (0x7fffffffffffffffLL/1000000))
- return 0x7fffffffffffffffLL;
-#endif
- return (newer.tv_sec-older.tv_sec)*1000000+
- (int)(newer.tv_usec-older.tv_usec);
+ timediff_t diff = newer.tv_sec-older.tv_sec;
+ if(diff >= (TIME_MAX/1000000))
+ return TIME_MAX;
+ else if(diff <= (TIME_MIN/1000000))
+ return TIME_MIN;
+ return (timediff_t)(newer.tv_sec-older.tv_sec)*1000000+
+ (timediff_t)(newer.tv_usec-older.tv_usec);
}