summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Kaufmann <mail@michael-kaufmann.ch>2017-05-20 19:39:51 +0200
committerMichael Kaufmann <mail@michael-kaufmann.ch>2017-05-24 22:56:22 +0200
commit8ab22a74533acee61af31c48e75269822f408cb4 (patch)
tree24025f8450cf3b72eb5bf9316c1212b0a571363c
parentb4d87f54d60711e936fefae388d741b281eecdf0 (diff)
downloadcurl-8ab22a74533acee61af31c48e75269822f408cb4.tar.gz
time: fix type conversions and compiler warnings
Fix bugs and compiler warnings on systems with 32-bit long and 64-bit time_t. Reviewed-by: Daniel Stenberg Closes #1499
-rw-r--r--lib/asyn-ares.c10
-rw-r--r--lib/easy.c12
-rw-r--r--lib/hostip.c7
-rw-r--r--lib/timeval.c6
-rw-r--r--lib/timeval.h2
-rw-r--r--src/tool_util.c11
-rw-r--r--src/tool_util.h2
-rw-r--r--tests/libtest/lib1501.c25
-rw-r--r--tests/libtest/lib1507.c21
-rw-r--r--tests/libtest/lib1900.c5
-rw-r--r--tests/libtest/lib582.c4
-rw-r--r--tests/libtest/testutil.c15
-rw-r--r--tests/libtest/testutil.h2
13 files changed, 36 insertions, 86 deletions
diff --git a/lib/asyn-ares.c b/lib/asyn-ares.c
index 12f35e412..fb3d3fec0 100644
--- a/lib/asyn-ares.c
+++ b/lib/asyn-ares.c
@@ -373,7 +373,6 @@ CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
/* Wait for the name resolve query to complete. */
while(!result) {
struct timeval *tvp, tv, store;
- long timediff;
int itimeout;
int timeout_ms;
@@ -402,8 +401,13 @@ CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
result = CURLE_ABORTED_BY_CALLBACK;
else {
struct timeval now2 = Curl_tvnow();
- timediff = Curl_tvdiff(now2, now); /* spent time */
- timeout -= timediff?timediff:1; /* always deduct at least 1 */
+ time_t timediff = Curl_tvdiff(now2, now); /* spent time */
+ if(timediff <= 0)
+ timeout -= 1; /* always deduct at least 1 */
+ else if(timediff > timeout)
+ timeout = -1;
+ else
+ timeout -= (long)timediff;
now = now2; /* for next loop */
}
if(timeout < 0)
diff --git a/lib/easy.c b/lib/easy.c
index 46c0a9911..2b1ce9e8a 100644
--- a/lib/easy.c
+++ b/lib/easy.c
@@ -615,12 +615,18 @@ static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
}
}
- if(!ev->msbump)
+ if(!ev->msbump) {
/* If nothing updated the timeout, we decrease it by the spent time.
* If it was updated, it has the new timeout time stored already.
*/
- ev->ms += (long)curlx_tvdiff(after, before);
-
+ time_t timediff = curlx_tvdiff(after, before);
+ if(timediff > 0) {
+ if(timediff > ev->ms)
+ ev->ms = 0;
+ else
+ ev->ms -= (long)timediff;
+ }
+ }
}
else
return CURLE_RECV_ERROR;
diff --git a/lib/hostip.c b/lib/hostip.c
index 21baf6015..619ec84b5 100644
--- a/lib/hostip.c
+++ b/lib/hostip.c
@@ -596,7 +596,7 @@ int Curl_resolv_timeout(struct connectdata *conn,
/* Ignore the timeout when signals are disabled */
timeout = 0;
else
- timeout = timeoutms;
+ timeout = (timeoutms > LONG_MAX) ? LONG_MAX : (long)timeoutms;
if(!timeout)
/* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
@@ -688,10 +688,11 @@ clean_up:
the time we spent until now! */
if(prev_alarm) {
/* there was an alarm() set before us, now put it back */
- unsigned long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
+ unsigned long elapsed_secs = (unsigned long) (Curl_tvdiff(Curl_tvnow(),
+ conn->created) / 1000);
/* the alarm period is counted in even number of seconds */
- unsigned long alarm_set = prev_alarm - elapsed_ms/1000;
+ unsigned long alarm_set = prev_alarm - elapsed_secs;
if(!alarm_set ||
((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
diff --git a/lib/timeval.c b/lib/timeval.c
index 0d6036b81..bed44c573 100644
--- a/lib/timeval.c
+++ b/lib/timeval.c
@@ -141,9 +141,3 @@ double curlx_tvdiff_secs(struct timeval newer, struct timeval older)
(double)(newer.tv_usec-older.tv_usec)/1000000.0;
return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
}
-
-/* return the number of seconds in the given input timeval struct */
-time_t Curl_tvlong(struct timeval t1)
-{
- return t1.tv_sec;
-}
diff --git a/lib/timeval.h b/lib/timeval.h
index 09f8b3a20..33969354d 100644
--- a/lib/timeval.h
+++ b/lib/timeval.h
@@ -46,8 +46,6 @@ time_t curlx_tvdiff(struct timeval t1, struct timeval t2);
*/
double curlx_tvdiff_secs(struct timeval t1, struct timeval t2);
-time_t Curl_tvlong(struct timeval t1);
-
/* These two defines below exist to provide the older API for library
internals only. */
#define Curl_tvnow() curlx_tvnow()
diff --git a/src/tool_util.c b/src/tool_util.c
index 15b91d303..dab60f0bd 100644
--- a/src/tool_util.c
+++ b/src/tool_util.c
@@ -121,8 +121,8 @@ struct timeval tool_tvnow(void)
*/
long tool_tvdiff(struct timeval newer, struct timeval older)
{
- return (newer.tv_sec-older.tv_sec)*1000+
- (newer.tv_usec-older.tv_usec)/1000;
+ return (long)(newer.tv_sec-older.tv_sec)*1000+
+ (long)(newer.tv_usec-older.tv_usec)/1000;
}
/*
@@ -137,10 +137,3 @@ double tool_tvdiff_secs(struct timeval newer, struct timeval older)
(double)(newer.tv_usec-older.tv_usec)/1000000.0;
return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
}
-
-/* return the number of seconds in the given input timeval struct */
-long tool_tvlong(struct timeval t1)
-{
- return t1.tv_sec;
-}
-
diff --git a/src/tool_util.h b/src/tool_util.h
index 8f72d6261..d5b3898ff 100644
--- a/src/tool_util.h
+++ b/src/tool_util.h
@@ -45,12 +45,10 @@ long tool_tvlong(struct timeval t1);
#undef tvnow
#undef tvdiff
#undef tvdiff_secs
-#undef tvlong
#define tvnow() tool_tvnow()
#define tvdiff(a,b) tool_tvdiff((a), (b))
#define tvdiff_secs(a,b) tool_tvdiff_secs((a), (b))
-#define tvlong(a) tool_tvlong((a))
#endif /* HEADER_CURL_TOOL_UTIL_H */
diff --git a/tests/libtest/lib1501.c b/tests/libtest/lib1501.c
index cc442b529..8a6ef5172 100644
--- a/tests/libtest/lib1501.c
+++ b/tests/libtest/lib1501.c
@@ -31,22 +31,7 @@
/* 500 milliseconds allowed. An extreme number but lets be really conservative
to allow old and slow machines to run this test too */
-#define MAX_BLOCKED_TIME_US 500000
-
-/* return the number of microseconds between two time stamps */
-static int elapsed(struct timeval *before,
- struct timeval *after)
-{
- ssize_t result;
-
- result = (after->tv_sec - before->tv_sec) * 1000000 +
- after->tv_usec - before->tv_usec;
- if(result < 0)
- result = 0;
-
- return curlx_sztosi(result);
-}
-
+#define MAX_BLOCKED_TIME_MS 500
int test(char *URL)
{
@@ -80,7 +65,7 @@ int test(char *URL)
int maxfd = -99;
struct timeval before;
struct timeval after;
- int e;
+ long e;
timeout.tv_sec = 0;
timeout.tv_usec = 100000L; /* 100 ms */
@@ -105,10 +90,10 @@ int test(char *URL)
abort_on_test_timeout();
after = tutil_tvnow();
- e = elapsed(&before, &after);
- fprintf(stderr, "pong = %d\n", e);
+ e = tutil_tvdiff(after, before);
+ fprintf(stderr, "pong = %ld\n", e);
- if(e > MAX_BLOCKED_TIME_US) {
+ if(e > MAX_BLOCKED_TIME_MS) {
res = 100;
break;
}
diff --git a/tests/libtest/lib1507.c b/tests/libtest/lib1507.c
index 7ab305711..cd8500195 100644
--- a/tests/libtest/lib1507.c
+++ b/tests/libtest/lib1507.c
@@ -44,23 +44,6 @@ static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
return CURL_READFUNC_ABORT;
}
-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;
-}
-
-static long tvdiff(struct timeval newer, struct timeval older)
-{
- return (newer.tv_sec-older.tv_sec)*1000+
- (newer.tv_usec-older.tv_usec)/1000;
-}
-
int test(char *URL)
{
int res = 0;
@@ -93,7 +76,7 @@ int test(char *URL)
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
multi_add_handle(mcurl, curl);
- mp_start = tvnow();
+ mp_start = tutil_tvnow();
/* we start some action by calling perform right away */
curl_multi_perform(mcurl, &still_running);
@@ -137,7 +120,7 @@ int test(char *URL)
rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
- if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
+ if(tutil_tvdiff(tutil_tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
fprintf(stderr, "ABORTING TEST, since it seems "
"that it would have run forever.\n");
break;
diff --git a/tests/libtest/lib1900.c b/tests/libtest/lib1900.c
index 42c0ef1dd..b55f3b7d8 100644
--- a/tests/libtest/lib1900.c
+++ b/tests/libtest/lib1900.c
@@ -167,7 +167,6 @@ int test(char *URL)
for(;;) {
struct timeval interval;
struct timeval now;
- long int msnow, mslast;
fd_set rd, wr, exc;
int maxfd = -99;
long timeout;
@@ -177,9 +176,7 @@ int test(char *URL)
if(handlenum < num_handles) {
now = tutil_tvnow();
- msnow = now.tv_sec * 1000 + now.tv_usec / 1000;
- mslast = last_handle_add.tv_sec * 1000 + last_handle_add.tv_usec / 1000;
- if((msnow - mslast) >= urltime[handlenum]) {
+ if(tutil_tvdiff(now, last_handle_add) >= urltime[handlenum]) {
fprintf(stdout, "Adding handle %d\n", handlenum);
setup_handle(URL, m, handlenum);
last_handle_add = now;
diff --git a/tests/libtest/lib582.c b/tests/libtest/lib582.c
index 1eb9b02d5..d7e4dd1bf 100644
--- a/tests/libtest/lib582.c
+++ b/tests/libtest/lib582.c
@@ -174,8 +174,8 @@ static int getMicroSecondTimeout(struct timeval* timeout)
struct timeval now;
ssize_t result;
now = tutil_tvnow();
- result = (timeout->tv_sec - now.tv_sec) * 1000000 +
- timeout->tv_usec - now.tv_usec;
+ result = (ssize_t)((timeout->tv_sec - now.tv_sec) * 1000000 +
+ timeout->tv_usec - now.tv_usec);
if(result < 0)
result = 0;
diff --git a/tests/libtest/testutil.c b/tests/libtest/testutil.c
index b9c43de04..f3ad0ce58 100644
--- a/tests/libtest/testutil.c
+++ b/tests/libtest/testutil.c
@@ -111,10 +111,11 @@ struct timeval tutil_tvnow(void)
*/
long tutil_tvdiff(struct timeval newer, struct timeval older)
{
- return (newer.tv_sec-older.tv_sec)*1000+
- (newer.tv_usec-older.tv_usec)/1000;
+ return (long)(newer.tv_sec-older.tv_sec)*1000+
+ (long)(newer.tv_usec-older.tv_usec)/1000;
}
+
/*
* Same as tutil_tvdiff but with full usec resolution.
*
@@ -125,13 +126,5 @@ double tutil_tvdiff_secs(struct timeval newer, struct timeval older)
if(newer.tv_sec != older.tv_sec)
return (double)(newer.tv_sec-older.tv_sec)+
(double)(newer.tv_usec-older.tv_usec)/1000000.0;
- else
- return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
-}
-
-/* return the number of seconds in the given input timeval struct */
-long tutil_tvlong(struct timeval t1)
-{
- return t1.tv_sec;
+ return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
}
-
diff --git a/tests/libtest/testutil.h b/tests/libtest/testutil.h
index 0bc5e03b7..f2aeae642 100644
--- a/tests/libtest/testutil.h
+++ b/tests/libtest/testutil.h
@@ -40,8 +40,6 @@ long tutil_tvdiff(struct timeval t1, struct timeval t2);
*/
double tutil_tvdiff_secs(struct timeval t1, struct timeval t2);
-long tutil_tvlong(struct timeval t1);
-
#endif /* HEADER_CURL_LIBTEST_TESTUTIL_H */