summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/address.c2
-rw-r--r--src/common/timers.c5
-rw-r--r--src/common/token_bucket.c5
-rw-r--r--src/common/token_bucket.h3
-rw-r--r--src/common/util.c13
-rw-r--r--src/common/util.h2
6 files changed, 27 insertions, 3 deletions
diff --git a/src/common/address.c b/src/common/address.c
index ca263425f0..a32df99107 100644
--- a/src/common/address.c
+++ b/src/common/address.c
@@ -1225,7 +1225,7 @@ tor_addr_keyed_hash(const struct sipkey *key, const tor_addr_t *addr)
/* LCOV_EXCL_START */
tor_fragile_assert();
return 0;
- /* LCOV_EXCL_END */
+ /* LCOV_EXCL_STOP */
}
}
diff --git a/src/common/timers.c b/src/common/timers.c
index a90817da1c..6f6236ed3b 100644
--- a/src/common/timers.c
+++ b/src/common/timers.c
@@ -64,6 +64,11 @@ struct timeout_cb {
* above TIMEOUT_MAX can also be super-inefficient. Choosing 5 here sets
* timeout_max to 2^30 ticks, or 29 hours with our value for USEC_PER_TICK */
#define WHEEL_NUM 5
+#if SIZEOF_VOID_P == 4
+/* On 32-bit platforms, we want to override wheel_bit, so that timeout.c will
+ * use 32-bit math. */
+#define WHEEL_BIT 5
+#endif
#include "src/ext/timeouts/timeout.c"
static struct timeouts *global_timeouts = NULL;
diff --git a/src/common/token_bucket.c b/src/common/token_bucket.c
index 5d97a920fb..747189e751 100644
--- a/src/common/token_bucket.c
+++ b/src/common/token_bucket.c
@@ -108,7 +108,7 @@ token_bucket_raw_dec(token_bucket_raw_t *bucket,
}
/** Convert a rate in bytes per second to a rate in bytes per step */
-static uint32_t
+STATIC uint32_t
rate_per_sec_to_rate_per_step(uint32_t rate)
{
/*
@@ -117,8 +117,9 @@ rate_per_sec_to_rate_per_step(uint32_t rate)
(rate / 1000) * to_approximate_msec(TICKS_PER_STEP). But to minimize
rounding error, we do it this way instead, and divide last.
*/
+ uint64_t units = (uint64_t) rate * TICKS_PER_STEP;
uint32_t val = (uint32_t)
- monotime_coarse_stamp_units_to_approx_msec(rate*TICKS_PER_STEP)/1000;
+ (monotime_coarse_stamp_units_to_approx_msec(units) / 1000);
return val ? val : 1;
}
diff --git a/src/common/token_bucket.h b/src/common/token_bucket.h
index 329b652f8e..fb5d9fc60a 100644
--- a/src/common/token_bucket.h
+++ b/src/common/token_bucket.h
@@ -10,6 +10,7 @@
#define TOR_TOKEN_BUCKET_H
#include "torint.h"
+#include "testsupport.h"
/** Largest allowable burst value for a token buffer. */
#define TOKEN_BUCKET_MAX_BURST INT32_MAX
@@ -109,6 +110,8 @@ token_bucket_rw_get_write(const token_bucket_rw_t *bucket)
* a power of two if you can. */
#define TICKS_PER_STEP 16
+STATIC uint32_t rate_per_sec_to_rate_per_step(uint32_t rate);
+
#endif
#endif /* TOR_TOKEN_BUCKET_H */
diff --git a/src/common/util.c b/src/common/util.c
index 041e7aee3d..b14b6f3979 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -572,6 +572,19 @@ add_laplace_noise(int64_t signal_, double random_, double delta_f,
return signal_ + noise;
}
+/* Helper: safely add two uint32_t's, capping at UINT32_MAX rather
+ * than overflow */
+uint32_t
+tor_add_u32_nowrap(uint32_t a, uint32_t b)
+{
+ /* a+b > UINT32_MAX check, without overflow */
+ if (PREDICT_UNLIKELY(a > UINT32_MAX - b)) {
+ return UINT32_MAX;
+ } else {
+ return a+b;
+ }
+}
+
/* Helper: return greatest common divisor of a,b */
static uint64_t
gcd64(uint64_t a, uint64_t b)
diff --git a/src/common/util.h b/src/common/util.h
index ae27e5f016..c0d20e1b22 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -176,6 +176,8 @@ int n_bits_set_u8(uint8_t v);
int64_t clamp_double_to_int64(double number);
void simplify_fraction64(uint64_t *numer, uint64_t *denom);
+uint32_t tor_add_u32_nowrap(uint32_t a, uint32_t b);
+
/* Compute the CEIL of <b>a</b> divided by <b>b</b>, for nonnegative <b>a</b>
* and positive <b>b</b>. Works on integer types only. Not defined if a+(b-1)
* can overflow. */