From: Loic Dachary Date: Mon, 27 Jan 2014 13:20:25 +0000 (+0100) Subject: histogram: fix histogram::get_position_micro overflow X-Git-Tag: v0.78~166^2~26 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=a5dabb58fab06843643eae7d932bd6919f1c3cf3;p=ceph.git histogram: fix histogram::get_position_micro overflow Convert the return values to uint64_t Signed-off-by: Loic Dachary --- diff --git a/src/common/histogram.h b/src/common/histogram.h index ceca6d90053ff..8d1ad8ea29a3b 100644 --- a/src/common/histogram.h +++ b/src/common/histogram.h @@ -82,11 +82,11 @@ public: /// @param v [in] value (non-negative) /// @param lower [out] pointer to lower-bound (0..1000000) /// @param upper [out] pointer to the upper bound (0..1000000) - int get_position_micro(int32_t v, unsigned *lower, unsigned *upper) { + int get_position_micro(int32_t v, uint64_t *lower, uint64_t *upper) { if (v < 0) return -1; unsigned bin = calc_bits_of(v); - unsigned lower_sum = 0, upper_sum = 0, total = 0; + uint64_t lower_sum = 0, upper_sum = 0, total = 0; for (unsigned i=0; iobs.oi.mtime != utime_t()) atime = ceph_clock_now(NULL).sec() - obc->obs.oi.mtime; if (atime < 0) diff --git a/src/test/common/histogram.cc b/src/test/common/histogram.cc index 6cbf648534db6..2fd3cfec2a7af 100644 --- a/src/test/common/histogram.cc +++ b/src/test/common/histogram.cc @@ -49,48 +49,58 @@ TEST(Histogram, Set) { TEST(Histogram, Position) { { pow2_hist_t h; - unsigned lb, ub; + uint64_t lb, ub; h.add(0); ASSERT_EQ(-1, h.get_position_micro(-20, &lb, &ub)); } { pow2_hist_t h; h.add(0); - unsigned lb, ub; + uint64_t lb, ub; h.get_position_micro(0, &lb, &ub); - ASSERT_EQ(0, lb); - ASSERT_EQ(1000000, ub); + ASSERT_EQ(0u, lb); + ASSERT_EQ(1000000u, ub); h.add(0); h.add(0); h.add(0); h.get_position_micro(0, &lb, &ub); - ASSERT_EQ(0, lb); - ASSERT_EQ(1000000, ub); + ASSERT_EQ(0u, lb); + ASSERT_EQ(1000000u, ub); } { pow2_hist_t h; h.add(1); h.add(1); - unsigned lb, ub; + uint64_t lb, ub; h.get_position_micro(0, &lb, &ub); - ASSERT_EQ(0, lb); - ASSERT_EQ(0, ub); + ASSERT_EQ(0u, lb); + ASSERT_EQ(0u, ub); h.add(0); h.get_position_micro(0, &lb, &ub); - ASSERT_EQ(0, lb); - ASSERT_EQ(333333, ub); + ASSERT_EQ(0u, lb); + ASSERT_EQ(333333u, ub); h.get_position_micro(1, &lb, &ub); - ASSERT_EQ(333333, lb); - ASSERT_EQ(1000000, ub); + ASSERT_EQ(333333u, lb); + ASSERT_EQ(1000000u, ub); } { pow2_hist_t h; - h.add(1); - h.add(10); - unsigned lb, ub; + h.h.resize(10, 0); + h.h[0] = 1; + h.h[5] = 1; + uint64_t lb, ub; h.get_position_micro(4, &lb, &ub); - ASSERT_EQ(500000, lb); - ASSERT_EQ(500000, ub); + ASSERT_EQ(500000u, lb); + ASSERT_EQ(500000u, ub); + } + { + pow2_hist_t h; + h.h.resize(10, 0); + h.h[0] = UINT_MAX; + h.h[5] = UINT_MAX; + uint64_t lb, ub; + ASSERT_EQ(500000u, lb); + ASSERT_EQ(500000u, ub); } } @@ -104,3 +114,13 @@ TEST(Histogram, Decay) { ASSERT_EQ(6, h.h[3]); ASSERT_EQ(4u, h.h.size()); } + +/* + * Local Variables: + * compile-command: "cd ../.. ; make -j4 && + * make unittest_histogram && + * valgrind --tool=memcheck --leak-check=full \ + * ./unittest_histogram + * " + * End: + */