diff options
author | Nick Mathewson <nickm@torproject.org> | 2016-06-30 11:04:13 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2016-06-30 11:04:13 -0400 |
commit | a31f55b16ba26c5c3720640bf27157fd7ab45c40 (patch) | |
tree | e7a0c9cce17c738848d851a5c6092369241c43d3 /src | |
parent | 4dc7b3ca2825741311b6b849ed109053c643ee23 (diff) | |
parent | 69535f12841aa8041863c81d1c6ac7eb7a95e0c6 (diff) | |
download | tor-a31f55b16ba26c5c3720640bf27157fd7ab45c40.tar.gz tor-a31f55b16ba26c5c3720640bf27157fd7ab45c40.zip |
Merge remote-tracking branch 'teor/bug19483-fix-v2'
Diffstat (limited to 'src')
-rw-r--r-- | src/common/util.c | 14 | ||||
-rw-r--r-- | src/test/test_util.c | 17 |
2 files changed, 27 insertions, 4 deletions
diff --git a/src/common/util.c b/src/common/util.c index 613e0001ea..fce99ee3f4 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -1451,11 +1451,14 @@ tv_udiff(const struct timeval *start, const struct timeval *end) * between 0 and TV_USEC_PER_SEC. */ udiff = secdiff*1000000 + ((int64_t)end->tv_usec - (int64_t)start->tv_usec); + /* Some compilers are smart enough to work out this is a no-op on L64 */ +#if SIZEOF_LONG < 8 if (udiff > (int64_t)LONG_MAX || udiff < (int64_t)LONG_MIN) { return LONG_MAX; - } else { - return (long)udiff; } +#endif + + return (long)udiff; } /** Return the number of milliseconds elapsed between *start and *end. @@ -1507,11 +1510,14 @@ tv_mdiff(const struct timeval *start, const struct timeval *end) ((int64_t)end->tv_usec - (int64_t)start->tv_usec + 500 + 1000000) / 1000 - 1000; + /* Some compilers are smart enough to work out this is a no-op on L64 */ +#if SIZEOF_LONG < 8 if (mdiff > (int64_t)LONG_MAX || mdiff < (int64_t)LONG_MIN) { return LONG_MAX; - } else { - return (long)mdiff; } +#endif + + return (long)mdiff; } /** diff --git a/src/test/test_util.c b/src/test/test_util.c index 89512920f1..c331a662a5 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -303,6 +303,23 @@ test_util_time(void *arg) tt_int_op(1005000L,OP_EQ, tv_udiff(&end, &start)); tt_int_op(1005L,OP_EQ, tv_mdiff(&end, &start)); + /* Negative tv_sec values, these will break on platforms where tv_sec is + * unsigned */ + + end.tv_sec = -10; + + tt_int_op(-15005000L,OP_EQ, tv_udiff(&start, &end)); + tt_int_op(-15005L,OP_EQ, tv_mdiff(&start, &end)); + tt_int_op(15005000L,OP_EQ, tv_udiff(&end, &start)); + tt_int_op(15005L,OP_EQ, tv_mdiff(&end, &start)); + + start.tv_sec = -100; + + tt_int_op(89995000L,OP_EQ, tv_udiff(&start, &end)); + tt_int_op(89995L,OP_EQ, tv_mdiff(&start, &end)); + tt_int_op(-89995000L,OP_EQ, tv_udiff(&end, &start)); + tt_int_op(-89995L,OP_EQ, tv_mdiff(&end, &start)); + /* Test that tv_usec values round away from zero when converted to msec */ start.tv_sec = 0; start.tv_usec = 0; |