[tor-commits] [tor/master] Add -Wfloat-conversion for GCC >= 4.9

nickm at torproject.org nickm at torproject.org
Sat Jun 11 14:16:58 UTC 2016


commit 493499a3399f8a8532b4b2a80006c033e8f64c58
Author: Nick Mathewson <nickm at torproject.org>
Date:   Mon May 30 12:54:31 2016 -0400

    Add -Wfloat-conversion for GCC >= 4.9
    
    This caught quite a few minor issues in our unit tests and elsewhere
    in our code.
---
 configure.ac               |  1 +
 src/common/util.c          |  2 +-
 src/or/channel.c           |  2 +-
 src/or/config.c            |  4 ++--
 src/or/confparse.c         |  4 ++--
 src/or/routerlist.c        |  2 +-
 src/test/test_channeltls.c | 12 ++++++------
 src/test/test_tortls.c     |  7 ++++---
 src/test/test_util.c       | 14 +++++++-------
 9 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/configure.ac b/configure.ac
index f66d798..dc3a329 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1768,6 +1768,7 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [
 
   if test "x$have_gcc49" = "xyes"; then
      CFLAGS="$CFLAGS -Wdate-time"
+     CFLAGS="$CFLAGS -Wfloat-conversion"
   fi
 
   if test "x$have_gcc5" = "xyes"; then
diff --git a/src/common/util.c b/src/common/util.c
index 925431d..1546fd1 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -5530,7 +5530,7 @@ clamp_double_to_int64(double number)
    * representable integer for which this is not the case is INT64_MIN, but
    * it is covered by the logic below. */
   if (isfinite(number) && exp <= 63) {
-    return number;
+    return (int64_t)number;
   }
 
   /* Handle infinities and finite numbers with magnitude >= 2^63. */
diff --git a/src/or/channel.c b/src/or/channel.c
index f393939..75b16d7 100644
--- a/src/or/channel.c
+++ b/src/or/channel.c
@@ -4525,7 +4525,7 @@ channel_update_xmit_queue_size(channel_t *chan)
   if (chan->get_overhead_estimate) {
     overhead = chan->get_overhead_estimate(chan);
     if (overhead >= 1.0f) {
-      queued *= overhead;
+      queued = (uint64_t)(queued * overhead);
     } else {
       /* Ignore silly overhead factors */
       log_notice(LD_CHANNEL, "Ignoring silly overhead factor %f", overhead);
diff --git a/src/or/config.c b/src/or/config.c
index 0388360..13b2269 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -5782,7 +5782,7 @@ parse_dir_authority_line(const char *line, dirinfo_type_t required_type,
     } else if (!strcmpstart(flag, "weight=")) {
       int ok;
       const char *wstring = flag + strlen("weight=");
-      weight = tor_parse_double(wstring, 0, UINT64_MAX, &ok, NULL);
+      weight = tor_parse_double(wstring, 0, (double)UINT64_MAX, &ok, NULL);
       if (!ok) {
         log_warn(LD_CONFIG, "Invalid weight '%s' on DirAuthority line.",flag);
         weight=1.0;
@@ -5926,7 +5926,7 @@ parse_dir_fallback_line(const char *line,
     } else if (!strcmpstart(cp, "weight=")) {
       int ok;
       const char *wstring = cp + strlen("weight=");
-      weight = tor_parse_double(wstring, 0, UINT64_MAX, &ok, NULL);
+      weight = tor_parse_double(wstring, 0, (double)UINT64_MAX, &ok, NULL);
       if (!ok) {
         log_warn(LD_CONFIG, "Invalid weight '%s' on FallbackDir line.", cp);
         weight=1.0;
diff --git a/src/or/confparse.c b/src/or/confparse.c
index 4f446d0..3532b39 100644
--- a/src/or/confparse.c
+++ b/src/or/confparse.c
@@ -1238,7 +1238,7 @@ config_parse_units(const char *val, struct unit_table_t *u, int *ok)
 
   v = tor_parse_uint64(val, 10, 0, UINT64_MAX, ok, &cp);
   if (!*ok || (cp && *cp == '.')) {
-    d = tor_parse_double(val, 0, UINT64_MAX, ok, &cp);
+    d = tor_parse_double(val, 0, (double)UINT64_MAX, ok, &cp);
     if (!*ok)
       goto done;
     use_float = 1;
@@ -1255,7 +1255,7 @@ config_parse_units(const char *val, struct unit_table_t *u, int *ok)
   for ( ;u->unit;++u) {
     if (!strcasecmp(u->unit, cp)) {
       if (use_float)
-        v = u->multiplier * d;
+        v = (uint64_t)(u->multiplier * d);
       else
         v *= u->multiplier;
       *ok = 1;
diff --git a/src/or/routerlist.c b/src/or/routerlist.c
index 4d9d249..abd97bb 100644
--- a/src/or/routerlist.c
+++ b/src/or/routerlist.c
@@ -2178,7 +2178,7 @@ scale_array_elements_to_u64(uint64_t *entries_out, const double *entries_in,
   double scale_factor = 0.0;
   int i;
   /* big, but far away from overflowing an int64_t */
-#define SCALE_TO_U64_MAX ((int64_t) (INT64_MAX / 4))
+#define SCALE_TO_U64_MAX ((double) (INT64_MAX / 4))
 
   for (i = 0; i < n_entries; ++i)
     total += entries_in[i];
diff --git a/src/test/test_channeltls.c b/src/test/test_channeltls.c
index 04ae9a6..f5fa50c 100644
--- a/src/test/test_channeltls.c
+++ b/src/test/test_channeltls.c
@@ -185,7 +185,7 @@ test_channeltls_overhead_estimate(void *arg)
   const char test_digest[DIGEST_LEN] = {
     0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
     0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14 };
-  float r;
+  double r;
   channel_tls_t *tlschan = NULL;
 
   (void)arg;
@@ -212,25 +212,25 @@ test_channeltls_overhead_estimate(void *arg)
   tlschan->conn->bytes_xmitted = 128;
   tlschan->conn->bytes_xmitted_by_tls = 64;
   r = ch->get_overhead_estimate(ch);
-  tt_assert(fabsf(r - 1.0f) < 1E-12);
+  tt_assert(fabs(r - 1.0f) < 1E-12);
 
   tlschan->conn->bytes_xmitted_by_tls = 127;
   r = ch->get_overhead_estimate(ch);
-  tt_assert(fabsf(r - 1.0f) < 1E-12);
+  tt_assert(fabs(r - 1.0f) < 1E-12);
 
   /* Now middle of the range */
   tlschan->conn->bytes_xmitted_by_tls = 192;
   r = ch->get_overhead_estimate(ch);
-  tt_assert(fabsf(r - 1.5f) < 1E-12);
+  tt_assert(fabs(r - 1.5f) < 1E-12);
 
   /* Now above the 2.0f clamp */
   tlschan->conn->bytes_xmitted_by_tls = 257;
   r = ch->get_overhead_estimate(ch);
-  tt_assert(fabsf(r - 2.0f) < 1E-12);
+  tt_assert(fabs(r - 2.0f) < 1E-12);
 
   tlschan->conn->bytes_xmitted_by_tls = 512;
   r = ch->get_overhead_estimate(ch);
-  tt_assert(fabsf(r - 2.0f) < 1E-12);
+  tt_assert(fabs(r - 2.0f) < 1E-12);
 
  done:
   if (ch) {
diff --git a/src/test/test_tortls.c b/src/test/test_tortls.c
index 973e727..caaada8 100644
--- a/src/test/test_tortls.c
+++ b/src/test/test_tortls.c
@@ -8,6 +8,7 @@
 #ifdef _WIN32
 #include <winsock2.h>
 #endif
+#include <math.h>
 
 #ifdef __GNUC__
 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
@@ -1194,17 +1195,17 @@ test_tortls_get_write_overhead_ratio(void *ignored)
 
   total_bytes_written_over_tls = 0;
   ret = tls_get_write_overhead_ratio();
-  tt_int_op(ret, OP_EQ, 1.0);
+  tt_double_op(fabs(ret - 1.0), OP_LT, 1E-12);
 
   total_bytes_written_by_tls = 10;
   total_bytes_written_over_tls = 1;
   ret = tls_get_write_overhead_ratio();
-  tt_int_op(ret, OP_EQ, 10.0);
+  tt_double_op(fabs(ret - 10.0), OP_LT, 1E-12);
 
   total_bytes_written_by_tls = 10;
   total_bytes_written_over_tls = 2;
   ret = tls_get_write_overhead_ratio();
-  tt_int_op(ret, OP_EQ, 5.0);
+  tt_double_op(fabs(ret - 5.0), OP_LT, 1E-12);
 
  done:
   (void)0;
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 2726c31..05c7d60 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -1472,24 +1472,24 @@ test_util_strmisc(void *arg)
 
   {
   /* Test parse_double */
-  double d = tor_parse_double("10", 0, UINT64_MAX,&i,NULL);
+  double d = tor_parse_double("10", 0, (double)UINT64_MAX,&i,NULL);
   tt_int_op(1,OP_EQ, i);
   tt_assert(DBL_TO_U64(d) == 10);
-  d = tor_parse_double("0", 0, UINT64_MAX,&i,NULL);
+  d = tor_parse_double("0", 0, (double)UINT64_MAX,&i,NULL);
   tt_int_op(1,OP_EQ, i);
   tt_assert(DBL_TO_U64(d) == 0);
-  d = tor_parse_double(" ", 0, UINT64_MAX,&i,NULL);
+  d = tor_parse_double(" ", 0, (double)UINT64_MAX,&i,NULL);
   tt_int_op(0,OP_EQ, i);
-  d = tor_parse_double(".0a", 0, UINT64_MAX,&i,NULL);
+  d = tor_parse_double(".0a", 0, (double)UINT64_MAX,&i,NULL);
   tt_int_op(0,OP_EQ, i);
-  d = tor_parse_double(".0a", 0, UINT64_MAX,&i,&cp);
+  d = tor_parse_double(".0a", 0, (double)UINT64_MAX,&i,&cp);
   tt_int_op(1,OP_EQ, i);
-  d = tor_parse_double("-.0", 0, UINT64_MAX,&i,NULL);
+  d = tor_parse_double("-.0", 0, (double)UINT64_MAX,&i,NULL);
   tt_int_op(1,OP_EQ, i);
   tt_assert(DBL_TO_U64(d) == 0);
   d = tor_parse_double("-10", -100.0, 100.0,&i,NULL);
   tt_int_op(1,OP_EQ, i);
-  tt_int_op(-10.0,OP_EQ, d);
+  tt_double_op(fabs(d - -10.0),OP_LT, 1E-12);
   }
 
   {





More information about the tor-commits mailing list