mirror of
https://github.com/curl/curl.git
synced 2026-08-26 17:03:32 +03:00
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
This commit is contained in:
parent
016c6a6abb
commit
b9d25f9a6b
22 changed files with 214 additions and 110 deletions
|
|
@ -4,14 +4,12 @@ CURLX_SRCS = \
|
|||
../../lib/mprintf.c \
|
||||
../../lib/nonblock.c \
|
||||
../../lib/strtoofft.c \
|
||||
../../lib/timeval.c \
|
||||
../../lib/warnless.c
|
||||
|
||||
CURLX_HDRS = \
|
||||
../../lib/curlx.h \
|
||||
../../lib/nonblock.h \
|
||||
../../lib/strtoofft.h \
|
||||
../../lib/timeval.h \
|
||||
../../lib/warnless.h
|
||||
|
||||
USEFUL = \
|
||||
|
|
|
|||
|
|
@ -67,6 +67,8 @@ const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }};
|
|||
#endif /* w32api < 3.6 */
|
||||
#endif /* ENABLE_IPV6 && __MINGW32__*/
|
||||
|
||||
static struct timeval tvnow(void);
|
||||
|
||||
/* This function returns a pointer to STATIC memory. It converts the given
|
||||
* binary lump to a hex formatted string usable for output in logs or
|
||||
* whatever.
|
||||
|
|
@ -100,7 +102,7 @@ void logmsg(const char *msg, ...)
|
|||
char buffer[2048 + 1];
|
||||
FILE *logfp;
|
||||
int error;
|
||||
struct curltime tv;
|
||||
struct timeval tv;
|
||||
time_t sec;
|
||||
struct tm *now;
|
||||
char timebuf[20];
|
||||
|
|
@ -112,7 +114,7 @@ void logmsg(const char *msg, ...)
|
|||
return;
|
||||
}
|
||||
|
||||
tv = curlx_tvnow();
|
||||
tv = tvnow();
|
||||
if(!known_offset) {
|
||||
epoch_offset = time(NULL) - tv.tv_sec;
|
||||
known_offset = 1;
|
||||
|
|
@ -213,7 +215,7 @@ int wait_ms(int timeout_ms)
|
|||
#ifndef HAVE_POLL_FINE
|
||||
struct timeval pending_tv;
|
||||
#endif
|
||||
struct curltime initial_tv;
|
||||
struct timeval initial_tv;
|
||||
int pending_ms;
|
||||
int error;
|
||||
#endif
|
||||
|
|
@ -231,7 +233,7 @@ int wait_ms(int timeout_ms)
|
|||
Sleep(timeout_ms);
|
||||
#else
|
||||
pending_ms = timeout_ms;
|
||||
initial_tv = curlx_tvnow();
|
||||
initial_tv = tvnow();
|
||||
do {
|
||||
#if defined(HAVE_POLL_FINE)
|
||||
r = poll(NULL, 0, pending_ms);
|
||||
|
|
@ -245,7 +247,7 @@ int wait_ms(int timeout_ms)
|
|||
error = errno;
|
||||
if(error && (error != EINTR))
|
||||
break;
|
||||
pending_ms = timeout_ms - (int)curlx_tvdiff(curlx_tvnow(), initial_tv);
|
||||
pending_ms = timeout_ms - (int)timediff(tvnow(), initial_tv);
|
||||
if(pending_ms <= 0)
|
||||
break;
|
||||
} while(r == -1);
|
||||
|
|
@ -397,3 +399,102 @@ int strncasecompare(const char *first, const char *second, size_t max)
|
|||
|
||||
return raw_toupper(*first) == raw_toupper(*second);
|
||||
}
|
||||
|
||||
#if defined(WIN32) && !defined(MSDOS)
|
||||
|
||||
static struct timeval tvnow(void)
|
||||
{
|
||||
/*
|
||||
** GetTickCount() is available on _all_ Windows versions from W95 up
|
||||
** to nowadays. Returns milliseconds elapsed since last system boot,
|
||||
** increases monotonically and wraps once 49.7 days have elapsed.
|
||||
**
|
||||
** GetTickCount64() is available on Windows version from Windows Vista
|
||||
** and Windows Server 2008 up to nowadays. The resolution of the
|
||||
** function is limited to the resolution of the system timer, which
|
||||
** is typically in the range of 10 milliseconds to 16 milliseconds.
|
||||
*/
|
||||
struct timeval now;
|
||||
#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
|
||||
ULONGLONG milliseconds = GetTickCount64();
|
||||
#else
|
||||
DWORD milliseconds = GetTickCount();
|
||||
#endif
|
||||
now.tv_sec = (long)(milliseconds / 1000);
|
||||
now.tv_usec = (milliseconds % 1000) * 1000;
|
||||
return now;
|
||||
}
|
||||
|
||||
#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
|
||||
|
||||
static struct timeval tvnow(void)
|
||||
{
|
||||
/*
|
||||
** clock_gettime() is granted to be increased monotonically when the
|
||||
** monotonic clock is queried. Time starting point is unspecified, it
|
||||
** could be the system start-up time, the Epoch, or something else,
|
||||
** in any case the time starting point does not change once that the
|
||||
** system has started up.
|
||||
*/
|
||||
struct timeval now;
|
||||
struct timespec tsnow;
|
||||
if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
|
||||
now.tv_sec = tsnow.tv_sec;
|
||||
now.tv_usec = tsnow.tv_nsec / 1000;
|
||||
}
|
||||
/*
|
||||
** Even when the configure process has truly detected monotonic clock
|
||||
** availability, it might happen that it is not actually available at
|
||||
** run-time. When this occurs simply fallback to other time source.
|
||||
*/
|
||||
#ifdef HAVE_GETTIMEOFDAY
|
||||
else
|
||||
(void)gettimeofday(&now, NULL);
|
||||
#else
|
||||
else {
|
||||
now.tv_sec = (long)time(NULL);
|
||||
now.tv_usec = 0;
|
||||
}
|
||||
#endif
|
||||
return now;
|
||||
}
|
||||
|
||||
#elif defined(HAVE_GETTIMEOFDAY)
|
||||
|
||||
static struct timeval tvnow(void)
|
||||
{
|
||||
/*
|
||||
** gettimeofday() is not granted to be increased monotonically, due to
|
||||
** clock drifting and external source time synchronization it can jump
|
||||
** forward or backward in time.
|
||||
*/
|
||||
struct timeval now;
|
||||
(void)gettimeofday(&now, NULL);
|
||||
return now;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static struct timeval tvnow(void)
|
||||
{
|
||||
/*
|
||||
** time() returns the value of time in seconds since the Epoch.
|
||||
*/
|
||||
struct timeval now;
|
||||
now.tv_sec = (long)time(NULL);
|
||||
now.tv_usec = 0;
|
||||
return now;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
long timediff(struct timeval newer, struct timeval older)
|
||||
{
|
||||
timediff_t diff = newer.tv_sec-older.tv_sec;
|
||||
if(diff >= (LONG_MAX/1000))
|
||||
return LONG_MAX;
|
||||
else if(diff <= (LONG_MIN/1000))
|
||||
return LONG_MIN;
|
||||
return (long)(newer.tv_sec-older.tv_sec)*1000+
|
||||
(long)(newer.tv_usec-older.tv_usec)/1000;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
* | (__| |_| | _ <| |___
|
||||
* \___|\___/|_| \_\_____|
|
||||
*
|
||||
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||
*
|
||||
* This software is licensed as described in the file COPYING, which
|
||||
* you should have received as part of this distribution. The terms
|
||||
|
|
@ -25,6 +25,7 @@
|
|||
|
||||
char *data_to_hex(char *data, size_t len);
|
||||
void logmsg(const char *msg, ...);
|
||||
long timediff(struct timeval newer, struct timeval older);
|
||||
|
||||
#define TEST_DATA_PATH "%s/data/test%ld"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue