token: Fix uint32_t to uint64_t conversion

Unfortunately, the units passed to
monotime_coarse_stamp_units_to_approx_msec() was always 0 due to a type
conversion.

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet
2018-04-16 15:02:51 -04:00
parent c7d3de216c
commit ae4e5b9824
3 changed files with 29 additions and 2 deletions
+3 -2
View File
@@ -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;
}
+3
View File
@@ -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 */